Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7072

NginX is failing to properly redirect to my node.js, although if I go to the IP:port directly, it works

$
0
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 portconst port = process.env.PORT || 8080;// Set hostconst host = process.env.HOST || 'localhost';// -------- //// Send index.html file to clientapp.get('/index.html', (req, res) => {    res.sendFile(path.join(import.meta.dirname, 'index.html'));});// -------- //// Start the serverapp.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.


Viewing all articles
Browse latest Browse all 7072

Trending Articles