Zend certified PHP/Magento developer

React/Express on Nginx/Ubuntu- React app works, Express gives 521 “bad gateway”

I’m attempting to run both an Express backend and React frontend from one Ubuntu server through Nginx, with Cloudflare handling SSL and DNS. My goal is to have the backend on api.nonsensefreerecipes.com, and the frontend on nonsensefreerecipes.com.

The front end works great, the backend never connects (usually 521 error).

My backend is running on port 8000, this is the Nginx file at /etc/nginx/sites-available/api.nonsensefreerecipes.com:

   server {
        listen 80;
        listen [::]:80;
        server_name api.nonsensefreerecipes.com www.api.nonsensefreerecipes.com;
        return 302 https://$server_name$request_uri;

}

server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ssl_certificate /etc/ssl/cert.pem;
        ssl_certificate_key /etc/ssl/key.pem;

        server_name api.nonsensefreerecipes.com www.api.nonsensefreerecipes.com;
        root /var/www/api.nonsensefreerecipes.com/nnr-dev/backend;

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


}

I’ve made a symbolic link of this config file to /etc/nginx/sites-enabled, of course.

Aside from the port being 8000 instead of 3000, this is the exact same config file that works perfectly for the React app. I also tried listen 8000 and listen 8443 with no luck.

The Cloudflare SSL covers the domain and the subdomain, and the certificate is valid on both. api.nonsensefreerecipes.com is secure, it just never connects.

The Express server lives at /var/www/api.nonsensefreerecipes.com/nnr-dev/backend, any variation of starting the server (i.e. npm start etc) runs the server (starting server..js... MongoDB Connected, etc) on the Ubuntu console, and I’ve verified this server runs great anywhere else. It’s definitely a Nginx problem.

Last note- the IP address for the server is 147.190.62.177 . Cloudflare is pushing both api.non… and non… to that IP address. From my limited understanding, adding :8000 to this should pull up the Express server, but that’s not working either.

I really appreciate any help, this is my first time trying to do this and I’ve been stuck for a few days now.