NginX's .conf file:
server {
listen 80;
listen [::]:80;
root /home/myUsername/serverDirectory;
index index.html index.htm;
server_name my.domain.name;
access_log /var/log/nginx/my.domain.name.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000; # Forward to Node.js server
proxy_ssl_session_reuse off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
A couple of extracts from the app.js file:
const app = express();
// Set port
const port = process.env.PORT || 8080;
// Set host
const host = process.env.HOST || 'localhost';
// -------- //
// Send index.html file to client
app.get('/index.html', (req, res) => {
res.sendFile(path.join(import.meta.dirname, 'index.html'));
});
// -------- //
// Start the server
app.listen(port, host, () => {
console.log(`Server listening at http://${host}:${port}`);
});
.env file:
HOST='localhost'
PORT=3000
This is the result of manually running app.js (although I keep it on pm2 unless I want to see this specifically):
Server listening at http://localhost:3000
/var/log/nginx/my.domain.name.log is completely empty, but /var/log/nginx/error.log has this repeated in it:
2025/11/30 21:07:28 [error] 2709#2709: *132 connect() failed (111: Connection refused) while connecting to upstream, client: [my.client.ip.address], server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "[my.host.ip.address]"
What I'm running in to is that when I have the node.js server listening on [my.host.ip.address] and I use http://[my.host.ip.address]:[port]/index.html I get my resource just fine, but when I try to forward through having NginX doing a reverse proxy, I get that the site refused to connect. As I am very new to NginX, I spent some few hours of time perusing https://nginx.org/en/docs, but I seem to be missing something.
Edit:
Ivan Shatsky's comments prompted me to check nginx -T and my snippet is included in the setup. I removed the try_files directive and ran nginx -t and nginx -s reload and I am still not getting to the correct location with NginX.
server {
listen 80;
listen [::]:80;
root /home/Username/ProjectDirectory;
index index.html index.htm;
server_name specificSubdomain.specificDomain.specificTLD;
access_log /var/log/nginx/specificSubdomain.specificDomain.specificTLD.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000; # Forward to Node.js server
proxy_ssl_session_reuse off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
}
}
This is the new file. It appears in the results of nginx -T above the default file.
After re-reading the material in the comments, I'm brought back to believing that my server block should be operating correctly since the order of precedence is:
- exact name
- longest wildcard name starting with an asterisk
- longest wildcard name ending with an asterisk
- first matching regular expression
and I am using an exact name. This was what I was attempting to convey in my comment, although it appears that I did a poor job in saying it.
upstream: "http://127.0.0.1:8080/"implies you have a problem withenvconfig (because nginx is passing to:3000)/etc/nginx/servers-available/defaultfile right now and it's listening on 80 as the default_server, and it hasproxy_pass http://127.0.0.1:8080/;inlocation / { ... }. My understanding, though, was that the NginX package was supposed to have been using the longer DNI first and then going to the default, so it shouldn't matter, right?server: _(rather thanserver: my.domain.name) andupstream: "http://127.0.0.1:8080/"(rather thanupstream: "http://localhost:3000") parts. Surely some other server block is handling your request. (to be continued)nginx -Tcommand to see which configuration snippets are included in the main conf and whether your snippet is being used at all. Read the "How nginx processes a request" documentation page and make sure you understand the default server concept. 3. Usingtry_filesandproxy_passdirectives together inside the same location is generally pointless and will lead to a non-working configuration. Why should you proxy the request to some other backend if the requested file is available to nginx locally, indeed?