evanp
By:
evanp

How do you host 2 sites from 1 nginx droplet?

August 6, 2017 237 views
Nginx Configuration Management Ubuntu 16.04

I'm trying to host 2 sites from 1 droplet. The droplet is Ubuntu 16.04 using nginx. I'm also using PHP 7.0

I own 2 domains that are pointed to the droplet. website1.com and website2.com.
website1.com uses Let's Encrypt.

These are my configs in sites-available.

/etc/nginx/sites-available/website1.com

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

        server_name website1.com www.website1.com;
        return 301 https://$server_name$request_uri;
}
server {
        # SSL configuration

        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        include snippets/ssl-website1.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/website1.com/html;
        index index.php index.html;
        error_page 404 /404.php;

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

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

               # With php7.0-cgi alone:
#               fastcgi_pass 127.0.0.1:9000;
               # With php7.0-fpm:
               fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

}

/etc/nginx/sites-available/website2.com

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

        root /var/www/website2.com/html;
        index index.php index.html;

        server_name website2.com www.website2.com;
        return 301 http://$server_name$request_uri;

        error_page 404 /404.php;

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

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

               # With php7.0-cgi alone:
#               fastcgi_pass 127.0.0.1:9000;
               # With php7.0-fpm:
               fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

}

Currently website1.com works perfectly, however website2.com is not accessible. Visiting website2.com in chrome displays a page that says:

This page isn’t working

website2.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

I've tried clearing my cookies, trying different browsers, devices, etc.

Also, when I visit the IP of the droplet in a browser, it goes straight to website1.com.

Every time I change a config, I restart nginx. There are no nginx errors either.

If there is any more information that would help please let me know.

Should I just delete this droplet and create 2 new droplets?

Thanks in advance.

2 Answers
UKn0Me August 6, 2017
Accepted Answer

I missed it at first but return 301 http://$server_name$request_uri; on line 9 of website2.com creates an infinite redirect, your site should work as expected upon removal and a reload of nginx.

Have you made a symlink from the /etc/nginx/sites-available/website2.com to /etc/nginx/sites-enabled/website2.com? Nginx only looks for configuration files in sites-enabled by default.

You can do this with

cd /etc/nginx/sites-enabled/
ln -s ../sites-available/website2.com website2.com
  • Yes, I already have a symlink to sites-enabled. Both configs appear in sites-enabled with the same contents as sites-available.
    Sorry for not including that.

Have another answer? Share your knowledge.