0

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:

  1. exact name
  2. longest wildcard name starting with an asterisk
  3. longest wildcard name ending with an asterisk
  4. 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.

10
  • upstream: "http://127.0.0.1:8080/" implies you have a problem with env config (because nginx is passing to :3000) Commented 12 hours ago
  • That part is bothering me, too. I'm looking at the /etc/nginx/servers-available/default file right now and it's listening on 80 as the default_server, and it has proxy_pass http://127.0.0.1:8080/; in location / { ... }. 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? Commented 12 hours ago
  • 1
    1. Reverse proxying and redirecting are completely different things. 2. The nginx configuration snippet you've shown doesn't align with the error log message — especially the server: _ (rather than server: my.domain.name) and upstream: "http://127.0.0.1:8080/" (rather than upstream: "http://localhost:3000") parts. Surely some other server block is handling your request. (to be continued) Commented 12 hours ago
  • 1
    Check the output of the nginx -T command 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. Using try_files and proxy_pass directives 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? Commented 12 hours ago
  • 1
    I can imagine a few factors that might impact this (the built-in VPN and Opera edge servers being blocked by the hosting provider, DNS-over-HTTPS domain resolution failure, or an HTTP3/QUIC connection unsuccessfully being attempted first by default), but since nginx is clearly ruled out as a suspect, any further investigation seems to be off-topic for Stack Overflow. Commented 8 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.