4

I am currently trying to run Nginx as a reverse proxy for a small Node application and serve up files for the core of a site.

E.g.

  • / Statically served files for root of website
  • /app/ Node app running on port 3000 with Nginx reverse proxy
server {
        listen 80;
        listen [::]:80;
        server_name  example.com www.example.com;

        root /var/www/example.com/html;
        index index.html index.htm;

        # Set path for access_logs
        access_log /var/log/nginx/access.example.com.log combined;

        # Set path for error logs
        error_log /var/log/nginx/error.example.com.log notice;

        # If set to on, Nginx will issue log messages for every operation
        # performed by the rewrite engine at the notice error level
        # Default value off
        rewrite_log on;

        # Settings for main website
        location / {
                try_files $uri $uri/ =404;
        }

        # Settings for Node app service
        location /app/ {
                # Header settings for application behind proxy
                proxy_set_header Host $host;

                # proxy_set_header X-NginX-Proxy true;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                # Proxy pass settings
                proxy_pass http://127.0.0.1:3000/;

                # Proxy redirect settings
                proxy_redirect off;

                # HTTP version settings
                proxy_http_version 1.1;

                # Response buffering from proxied server default 1024m
                proxy_max_temp_file_size 0;

                # Proxy cache bypass define conditions under the response will not be taken from cache
                proxy_cache_bypass $http_upgrade;
        }
}

This appeared to work at first glance, but what I have found over time is that I am being served 502 errors constantly on the Node app route. This applies to both the app itself, as well as static assets included in the app.

I've tried using various different variations of the above config, but nothing I can find seems to fix the issue. I had read of issues with SELinux, but this is currently not on the server in question.

Few additional bits of information; Server: Ubuntu 18.04.3 Nginx: nginx/1.17.5

2020/02/09 18:18:07 [error] 8611#8611: *44 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: x, server: example.com, request: "GET /app/assets/images/image.png HTTP/1.1", upstream: "http://127.0.0.1:3000/assets/images/image.png", host: "example.com", referrer: "http://example.com/overlay/"
2020/02/09 18:18:08 [error] 8611#8611: *46 connect() failed (111: Connection refused) while connecting to upstream, client: x, server: example.com, request: "GET /app/ HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "example.com"

Has anyone encountered similar issues, or knows what it is that I've done wrong?

Thanks in advance!

Mixehh
  • 71
  • 2

1 Answers1

0

It's may be because of your node router.It's better to share nodes code too. Anyway try put your main router and static route like app.use('/app', mainRouter); and see it make any sense?

Majid Sadrayi
  • 324
  • 2
  • 6