0

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 Answer 1

0

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;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your try. The solution is not working, as I cannot restart ngnix after I change my conf with this informations. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. failed!
Were not you curious to see what the actual culprit was ? Why did not you execute journalctl -xe command as the message suggests ? Perhaps your nginX is older and does not support HTTP/2 ? Or maybe there is some syntax error in the config ? I did not actually test this config - just giving you ideas where to continue your troubleshooting. Do not expect that you will just copy/paste and it will solve your problem at zero time and with no efforts ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.