I've been struggling with this issue for almost two weeks now and need some help. I'm hosting a simple app on my server to test it online. To set it up properly, I decided to use Nginx as a reverse proxy for my Node.js application.
The GoalI want to be able to connect to my app using HTTPS (port 443) without having to specify any port in the URL. Currently, I can connect to the server on different ports (e.g., 8080 or 443), but I always need to include the port number explicitly. I would like to make this seamless and work as expected for HTTPS.
Here’s everything I’ve tried so far:
Network Configuration:
- I’ve opened the necessary ports on my router:
Public 80 -> Private 80Public 443 -> Private 443Public 8080 -> Private 8080
- I’ve opened the necessary ports on my router:
Node.js Express Server:
- My Express app is set up to listen on port 8080. Here’s my
server/index.js
file:const express = require('express');const routes = require('./routes');const path = require('path');const app = express();const httpPort = 8080;app.use(express.json());app.use('/api', routes);app.use(express.static(path.join(__dirname, "../client", "dist")));app.get('*', (req, res) => { res.sendFile(path.join(__dirname, "../client", "dist", "index.html"));});app.listen(httpPort, () => { console.log(`Server running on http://localhost:${httpPort}`);});
- My Express app is set up to listen on port 8080. Here’s my
Nginx Configuration:
- My Nginx is configured to:
- Redirect HTTP traffic (port 80) to HTTPS (port 443).
- Proxy traffic from port 443 to my Node.js app on port 8080.
- Here’s my Nginx configuration:
server { listen 80; server_name MY_SERVER_NAME; # Redirect HTTP to HTTPS return 301 https://$host$request_uri;}server { listen 443 ssl; server_name MY_SERVER_NAME; ssl_certificate /home/<user>/certificates/fullchain.pem; ssl_certificate_key /home/<user>/certificates/privkey.pem; location / { proxy_pass http://localhost:8080; 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; }}
- My Nginx is configured to:
Firewall Rules (UFW):
- All necessary ports (80, 443, 8080) are open:
To Action From443/tcp ALLOW Anywhere80/tcp ALLOW Anywhere8080/tcp ALLOW Anywhere
- All necessary ports (80, 443, 8080) are open:
Checking Sockets:
- When I start my Node.js server and check active sockets using
ss -tuln
, I can see:tcp LISTEN 0 511 0.0.0.0:443 0.0.0.0:*tcp LISTEN 0 511 *:8080 *:*
- When I start my Node.js server and check active sockets using
The Problem
Despite all this, I cannot connect to my app seamlessly on HTTPS (port 443). I still need to manually specify the port in the URL for the connection to work.
When I visit https://MY_SERVER_NAME
, the page takes a while to load and then I get the error: ERR_CONNECTION_CLOSED
. If I try to ping
or curl
the server, no packets are received.
Then :
https://MY_SERVER_NAME:443 => Not Working
https://MY_SERVER_NAME:80 => Not Working => redirect to my https://MY_SERVER_NAME:443
http://MY_SERVER_NAME:8080 => Working
It seems like the server isn't responding at all. Could it be related to the port or something else? Let me know if I should provide more details!