Question

Subdomain and Nginx

Posted December 26, 2013 32k 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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
7 answers
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!!
@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! :-)