nginx proxy_pass 404 error, don't understand why

前端 未结 4 2159
梦毁少年i
梦毁少年i 2020-12-08 06:26

I am trying to pass off all calls to /api to my webservice but I keep getting 404s with the following config. Calls to / return index.html as expected. Does anyone know why?

相关标签:
4条回答
  • 2020-12-08 06:58

    Just want to remind others that the slash(backend*/*) after your proxy_pass url is very important!

    if the config is

    location /api/ {
        proxy_pass http://backend;
    }
    

    and you visit http://abc.xyz/api/endpoint, you would be direct to http://backend/api/endpoint;

    if the config is

    location /api/ {
        proxy_pass http://backend/;
    }
    

    and you visit http://abc.xyz/api/endpoint, you would be directed to http://backend/endpoint.

    That's the difference.

    For more, refer to: Nginx reverse proxy return 404

    0 讨论(0)
  • 2020-12-08 07:04

    I forgot to listen on PORT 80, fixed it.

    "http" part of nginx config, at: /etc/nginx/nginx.conf, is below:

    http {
        server {
            listen 192.111.111.11:80;
            location /path1/ {
                proxy_pass http://127.0.0.1:3000/path1/
            }
        }
    }
    

    Now, accessing
    http://192.111.111.11/path1/
    will get result of accessing
    http://127.0.0.1:3000/path1/

    NOTE:
    Replace the 192.111.111.11 with your IP address in the above.
    Run "ifconfig" command, the "inet addr" part will give your IP address

    0 讨论(0)
  • 2020-12-08 07:11

    This

    location /api {
        proxy_pass http://backend;
    }
    

    Needs to be this

    location /api/ {
        proxy_pass http://backend/;
    }
    
    0 讨论(0)
  • 2020-12-08 07:12

    By some reason proxy_pass in Nginx cuts header "Host" before passing to upstream, and request catches by default server, and even proxy_header_pass doesn't helps, so I've to explicitly set it:

    location / {
        proxy_set_header Host $host;
        proxy_pass  http://backend;
    }
    
    0 讨论(0)
提交回复
热议问题