0

I have an NGINX server currently running a ReactJS app, pointed at the /build folder, with HTPPS up and running via NGINX.

Alongside it, I used to have a NodeJS app (website) running on port 8888 with a reverse proxy to make it accessible through port 80 by default. This app used to run in HTTP protocol.

The need for this NodeJS website to be served in HTTPS has risen and I'm having quite a lot of trouble getting the two apps to function together. It's always either 502 errors or SLL_do_handshake errors.

This is my actual config. The React app block is untouched, it worked perfectly before I started tinkering with the Node one.

server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# PLATFORM  - DEFAULT SERVER
server {
    server_name platform.domain.com;
    
    listen 80 default_server;

    ssl_certificate     /etc/ssl/certs/server.crt;
    ssl_certificate_key /etc/ssl/certs/server.key;

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

    error_page 404 /404.html;
    location = /404.html {}

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {}
}

# REACT APP
server {
    server_name dashboard.domain.com;
    
    listen 443 ssl;
    root /var/www/react-app/build;
    index index.html;

    include snippets/cert-params.conf;
    include snippets/ssl-params.conf;        

    location / {
      try_files $uri $uri/ /index.html;
    }
}

My goal is to maintain its current function: 2 separate apps, on the same machine/webserver, that I can redirect to each other as the user requires.

What am I missing? Most of the answers lead me to make NGINX handle the HTTPS part and leave the NodeJS app running on HTTP, but I really need to have the NodeJS running on HTTPS.

Thanks in advance!

1 Answer 1

1

As you want to handle both as https then you can combine block as below. on /api you can accept all node related requests. and you can keep / as your react path.

server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# Combine React and Node in same 443 block
server {
    server_name dashboard.domain.com;
    
    listen 443 ssl;
    
    # REACT APP
    root /var/www/react-app/build;
    index index.html;

    include snippets/cert-params.conf;
    include snippets/ssl-params.conf;        

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

It's actually better if you handle all https request using nginx first and then pass proxy to node server in simple http protocol as it's reduce ssl processing time on your node js side.

Sign up to request clarification or add additional context in comments.

4 Comments

Thing is, the Node app is an actual app, a website (I should probably edit my question). Will this still work?
And btw, my insistence on running HTTPS on the NodeJS side is solely because it needs to make requests other API's that are running HTTPS, and an HTTP app requesting something at an HTTPS domain doesn't seem to work, and rightfully so... NGINX handling the HTTPS, will make the app runing HTTP able to request something to an HTTPS domain?
From node.js backend side you can make request to HTTPS but if you initiate request from browser then you need HTTPS. It also works for website as well. instead of /api you just need to mention any name like web so that you can access your website domain/web.
Turns out the problem was on the NodeJS side, and my nginx config was almost all correct. Your answer was still helpfu though, so thank you very much for your time. Cheers mate.

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.