Multiple sites using nginx

July 20, 2014 22.5k views

I was able to set up nginx server blocks as per this tutorial. When I try to access the sites through the respective domain names I am directed to the same site. I'm not sure if this is an nginx issue or a dns issue.

2 comments
  • Paste your Vhost(s) from nginx.

  • I have two files:

    One:

    server {
        listen 80;
        listen [::]:80 default_server ipv6only=on;
    
        root /var/www/munderground.com/public_html;
        index index.html index.htm;
    
        # Make site accessible from http://localhost/
        server_name munderground.com www.munderground.com;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
    
        location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
        }
    

    Two:

    server {
        listen 80;
        listen [::]:80;
    
        root /var/www/verred.com/public_html;
        index index.html index.htm;
    
        # Make site accessible from http://localhost/
        server_name verred.com www.verred.com;
    
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
    
        location /doc/ {
            alias /usr/share/doc/;
            autoindex on;
            allow 127.0.0.1;
            allow ::1;
            deny all;
        }
    
3 Answers

Hi!

I see different content on each of those domains, so it looks like you got it working! It would be great if you could share what the root cause of your issue was and how you fixed it. That would help others who end up at this question.

Your configuration files look correct. One thing many people forget is that you need to restart Nginx after making any changes:

sudo service nginx restart
  • I did restart the service after every change I made.

    The last change I made was to remove [ default_server ipv6only=on ] in the listen property in the server 2 file. So I'm assuming this was my problem.

    Block one:

    server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;

    Block two:

    server {
    listen 80;
    listen [::]:80;

    I appreciate your response, thanks.

remove [ defaultserver ipv6only=on ] in the listen property in the server 2 file. Only one block should be designated as defaultserver.

Correct configuration:
Block one:

server {
listen 80;
listen [::]:80 default_server ipv6only=on;

Block two:

server {
listen 80;
listen [::]:80;

I have the same question; I have multiple domains redirecting to the same nginx server, but my PHP code handles each site differently.

When I have set up the one domain to point at my servers IP, I simply see the old site.

Any solutions on this? Is it simply to remove ipv6only=on?

Have another answer? Share your knowledge.