Question

Multiple Wordpress sites on single droplet with NGINX - problem with Server Block

Posted January 17, 2018 1.4k views
Nginx WordPress Ubuntu 16.04

I have two Wordpress sites installed, example.com and test.com.
Server is Nginx.
I configured my local /etc/hosts so that example.com, www.example.com, test.com and www.test.com - all point to the ip address of my VPS.
It works, but only partially.
The URL http://test.com works as expected, and I see the correct Wordpress site.
However, http://example.com sends me to the page directed by the default Server Block, and that’s not what I want.

Thank you for taking the time to read my question and working to help me.

For the reference, here are the 3 Server Blocks I have: default, example.com and test.com.

# default
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name 207.154.250.232;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;  
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}
# example.com
server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

    location / {
                try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
}
# test.com
server {
    listen 80;
    listen [::]:80;

    root /var/www/test.com/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name test.com www.test.com;

    location / {
                try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
            expires max;
            log_not_found off;
        }
}

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.

1 answer

Hello,

I’ve seen similar problems, you need to make sure that your document root are owned by the Nginx user so that Nginx could read the files in the document root.

You could also take a look at these steps here on how to troubleshoot common Nginx problems:

https://www.digitalocean.com/community/questions/how-to-troubleshoot-common-nginx-issues-on-linux-server

Regards,
Bobby

Submit an Answer