'nginx returns 404 when using proxy pass

I have a following situation. I have a domain https://example.com pointing to the server where I host NGINX. I want to redirect it to the NodeJS app running on the same server hosting xxx.com. It's hosted on port 8064. It has an endpoint http://localhost:8064/subscribe that I need to use trough reverse proxy.

so my config looks like this:

location / {
      proxy_pass          http://localhost:8064/;
      try_files $uri $uri/ =404;
        }

However, When I try to access http://example.com I get to the NodeJS app, but when I try to access http://example.com/subscribe I get 404 error



Solution 1:[1]

You shouldn't use proxy_pass and try_files in the same location block. Unless you want to check that file is really existed first and then proxy the request. In your configuration nginx uses try_files and ignores response from proxy.

I'm gessing request without any path only works because you have some index.html in the root folder.

If you want to serve static files first and then proxy pass try something like this:

server {
    server_name _;
    root /var/www/site;
    location / {
        try_files $uri @proxy;
    }
    location @proxy {
        proxy_pass http://localhost:8064/;
    }
}

But I think it's kinda dangerous to combine location / {} with try_files because you have to be very careful and keep in root folder only those files you wouldn't mind to share with everyone.

With NodeJS app you usually use only proxy_pass and let it to serve static files itself. Or write more specific location + try_files like for *.css files only etc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grin