Ad

Serving Two Websites With The Same Nginx On The Same Ubuntu Server Redirect To Same Website

- 1 answer

I'm trying to serve two different websites on my Ubuntu16.04 server.

This is the Website1 configuration in /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 {
        listen 443 ssl;

        server_name website1.com www.website1.com;
        ssl_certificate /etc/nginx/ssl/website1.com.domain.crt;
        ssl_certificate_key /etc/nginx/ssl/website1.com.intermediate.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        location / {
                proxy_pass https://127.0.0.1:1337;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

And this is the Website2 configuration in /etc/nginx/sites-available/website2.com:

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

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

server {
        listen 443 ssl;

        server_name website2.com www.website2.com;
        ssl_certificate /etc/nginx/ssl/website2.com.domain.crt;
        ssl_certificate_key /etc/nginx/ssl/website2.com.intermediate.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

        location / {
                proxy_pass https://127.0.0.1:5000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

These two websites I'm trying to serve with nginx are two nodejs applications. One is listening on port 1337 (website1) and one on port 5000 (website2).

If I visit the url https://website1.com I access to the correct website, the website1.

If I visit the url https://website2.com I access to the wrong website, it shows me the website1, instead of the website2. (the url on the top bar is http://website2.com, with a warning for the SSL).

How can I serve two different websites using the same nginx service on the same Ubuntu server?

Ad

Answer

The redirect problem is due to the invalid SSL certificate. I'm trying to figure out how to make it valid.

So, the nginx configuration is ok.

Ad
source: stackoverflow.com
Ad