Question

Subdomain and Nginx

  • Posted December 26, 2013

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!

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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! :-)

@Kamal Nasser Thanks! Just replied.

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!!

<b>“I … am trying to put the subdomain in the same config file.”</b> <br> <br>Why? What benefit does that serve? <br> <br><b>“Can I put it somewhere in here…”</b> <br> <br>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: <a href=“http://pastebin.com/dttKrpzK”>pastebin.com/dttKrpzK</a> (that is, stack 2 server blocks – one on top of the other).

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? <br> <br>server { <br> listen 80; <br> <br> root /var/www; <br> index index.php index.php index.htm; <br> <br> server_name mydomain.com www.mydomain.com; <br> <br> location / { <br> try_files $uri $uri/ /index.php?q=$uri&$args; <br> } <br> <br> location ~ .php$ { <br> try_files $uri =404; <br> fastcgi_pass unix:/var/run/php5-fpm.sock; <br> fastcgi_index index.php; <br> include fastcgi_params; <br> } <br> <br> location = /favicon.ico { <br> log_not_found off; <br> access_log off; <br> } <br> <br> location = /robots.txt { <br> allow all; <br> log_not_found off; <br> access_log off; <br> } <br> <br> location ~ /. { <br> deny all; <br> } <br> <br> location ~* /(?:uploads|files)/.*.php$ { <br> deny all; <br> } <br> }

Check out <a href=“https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3”>How To Set Up nginx Virtual Hosts (Server Blocks) on Ubuntu 12.04 LTS</a>.