0
votes

I'm facing an issue on my nginx configuration :


Context:

  • I have a VPS, and multiple domains on it.
  • I host 2 web applications on it.
  • I use let's encrypt through certbot to handle https.

Each web app is composed of :

  • a static build for front-end served on one domain ( http redirects to https )
  • a https served node.js backend ( port :5000/graphql for app1 and :5001/graphql for app2 )

I can access to each backend if I specify the port, using any of the domain I have :

  • https//domain1:5000/graphql is ok ( :5000 is for the backend of app1 served on domain1 )
  • https//domain2:5000/graphql is also ok ( complaining not secure, since :5000 is for app1 )

My question:

How can I configure properly nginx to redirect all request to :5000 to https://domain1:5000 and all request on :5001 to https://domain2:5001 ? ( and doing the same for the other port)


My nginx conf

( both apps have same config expect for the domain and the port )

server {
        server_name domain1.com www.domain1.com;
        root /path/to/client/build
        index index.html;

        access_log /var/log/nginx/domain1.com.access.log;
        error_log /var/log/nginx/domain1.com.error.log;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost: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;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = www.domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = domain1.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        server_name domain1.com www.domain1.com;
    listen 80;
    return 404; # managed by Certbot

}

Thanks !

1

1 Answers

0
votes

Perhaps something like this (not tested - please experiment with the proxy_ssl_xxx directives from the manual):

server {
        server_name .domain1.com;
        root /path/to/client/build
        index index.html;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost: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;
        }


    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
        server_name .domain2.com;
        root /path/to/client/build
        index index.html;

        location / {
                try_files $uri /index.html;
        }
        location /graphql {
                proxy_pass https://localhost:5001;
                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;
        }


    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain2.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    server_name .domain1.com .domain2.com;
    listen 80;
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
  server_name .domain1.com;
  listen 5000 ssl;

  location / {
                proxy_pass https://localhost: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;
  }
}

server {
  server_name .domain2.com;
  listen 5001 ssl;

  location / {
                proxy_pass https://localhost:5001/;
                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;
  }
}