Subdomain and Nginx

December 26, 2013 27.6k views
I am trying to make a subdomain on Nginx. I understand that I have to make a new server block but even after doing that I am still unable to access it. This is a copy of my working config file without the subdomain. http://pastebin.com/Ah9gri1u I am hoping to put everything in that one file. I created a new directory in /var/www/stage. Any help would be greatly appreciated!
7 Answers
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.
Thanks for your reply! I tried going through those steps but am trying to put the subdomain in the same config file. Can I put it somewhere in here or should I just make a new config file for the stage site?

server {
listen 80;

root /var/www;
index index.php index.php index.htm;

server_name mydomain.com www.mydomain.com;

location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location ~ /\. {
deny all;
}

location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
"I .. am trying to put the subdomain in the same config file."

Why? What benefit does that serve?

"Can I put it somewhere in here..."

You probably can; but, have you ever heard the saying "don't put all your eggs in one basket?" If you insist, however, try this: pastebin.com/dttKrpzK (that is, stack 2 server blocks -- one on top of the other).
I read in the comments of the article about putting it in one file but you make a good point. It'd be more organized if I separated them. Thanks for your help!!
by Etel Sverdlov
This tutorial shows you how to set up a new directory, how to grant the correct permissions, how to add content to a new page, how to turn on virtual hosting, and how to, if needed, set up local hosts. Virtual Hosts are used to run more than one website or domain off of a single server and IP address. When used with nginx, virtual hosts are called Server Blocks. This tutorial covers setting up server blocks on an Ubuntu server.
@Kamal Nasser Thanks! Just replied.

Edit /etc/nginx/sites-available/default and add the following:

server {
  listen 80;

  server_name www.example.com;
  root /var/www/example;
  index index.html index.htm index.php;              

  location / {
    try_files $uri $uri/ =404;
  }
}

server {
  listen 80;

  server_name blog.example.com;
  root /var/www/blog;
  index index.html index.htm index.php;              

  location / {
    try_files $uri $uri/ =404;
  }
}

Hope this helps! :-)

Have another answer? Share your knowledge.