Question

How to securely connect to Nodejs backend server?

Hello,

I have been struggling 2 whole days already with this problem. I’ve set up a reactjs app, nodejs backend server and mysql database on the same droplet. I can visit the react site (www.xxx.com) without problems, my backend is also running. But I cannot connect frontend to backend, I always get “ERR_SSL_PROTOCOL_ERROR” (on my local windows machine, everything is working perfectly). It seems the backend server doesnt make a secure connection. When I do ip_address:8800?api-command, I get a response. So, it is working. I connect from FE to BE like this: “https://ip_address_droplet:8800”

I have to admit, I never used Nginx before, but this is my config file:

server {

        root /var/www/sitename.com/html/build;
        index index.html index.htm index.nginx-debian.html;
        server_name sitename.com;

        location / {
                try_files $uri $uri/ =404;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/sitename.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/sitename.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 nodejs.sitename.com;
	location / {
        proxy_pass http://localhost:8800; #whatever port your app runs on
        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 {
    if ($host = sitename.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        listen [::]:80 default_server;

        server_name sitename.com;
    return 404; # managed by Certbot


}

Can someone guide me into the right direction? What am I doing wrong?

Thx, Peter


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Thanks for your help! It put me in the right direction. And at the end, after following what you said, I found what was causing the biggest issue: the way I connected to the backend “https://ip_address_droplet:8800” When I remove :8800, all runs smoothly

thx

Bobby Iliev
Site Moderator
Site Moderator badge
August 22, 2023

Hi there,

The server block configuration for your nodejs.sitename.com site is not correct. You need to have the listener port in there as well and it is best to install an SSL certificate for this site too otherwise you will get a mixed content error.

What I would suggest is:

  • First define a server block for the nodejs subdomain for port 80, eg:
server {
	server_name nodejs.sitename.com;
       listen 80;
	location / {
        proxy_pass http://localhost:8800; #whatever port your app runs on
        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;
    }
}
  • After that issue an SSL certificate for this subdomain using certbot:

https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-20-04

The certbot command will automatically configure the server block for port 443 for you.

This should fix the issue that you are seeing.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel