Nginx EJS app 502 bad gateway while normal pages are resolved

19 views Asked by At

I have an oracle linux 8, an nginx running on it. I have a 3 site folder structure:

/usr/share/nginx/html/my.site.com
/usr/share/nginx/html/my.othersite1.com
/usr/share/nginx/html/my.othersite2.com

The "othersites" are running properly, my.site.com is an express app, view engine is EJS, app listens to port 3000, page is a siple demo page. I start the app using pm2, status is ok, i can see the node process if i do ps -ef | node. If i spin up a local node server, the page loads. If I go by the IP(of my.site.com):3000, then the page loads. However if I try to open my.site.com, I get 502 Bad Gateway.

Nginx error log shows:

2024/03/07 17:10:26 [error] 571114#0: *1 open() "/usr/share/nginx/html/.env" failed (2: No such file or directory), client: xx.xxx.xxx.xxx, server: _, request: "GET /.env HTTP/1.1", host: "xxx.xxx.xxx.xy"
2024/03/07 17:10:59 [crit] 571114#0: *3 connect() to 127.0.0.1:3000 failed (13: Permission denied) while connecting to upstream, client: xx.xxx.xxx.xxx, server: my.site.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "my.site.com"
2024/03/07 17:10:59 [crit] 571114#0: *3 connect() to [::1]:3000 failed (13: Permission denied) while connecting to upstream, client: xx.xxx.xxx.xxx, server: my.site.com, request: "GET / HTTP/1.1", upstream: "http://[::1]:3000/", host: "my.site.com"
2024/03/07 17:10:59 [error] 571114#0: *3 no live upstreams while connecting to upstream, client: xx.xxx.xxx.xxx, server: my.site.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://localhost/favicon.ico", host: "my.site.com", referrer: "http://my.site.com/"

Structure of nginx config:

 /etc/nginx/conf.d/my.site.com.conf 
server {
    listen 80;
    listen [::]:80;
    server_name my.site.com;

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

The app structure:

ls -la /usr/share/nginx/html/my.site.com
favicon.ico
index.js
node_modules
package.json
package-lock.json
routes
views
ls -la /usr/share/nginx/html/my.site.com/views
index.ejs
ls -la /usr/share/nginx/html/my.site.com/routes
main.js

Any idea what's going on? Thanks!

1

There are 1 answers

0
aixgoa On

It turned out Oracle linux is similar to SElinux and the local http connection was blocked. The solution was: setsebool -P httpd_can_network_connect 1 -P

After that, I restarted nginx and all was working.