nginx proxy_pass 404 error, don't understand w

2019-01-23 12:13发布

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?

upstream backend{
    server localhost:8080;
}

 server {

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

    location / {
        root /html/dir;
    }
}

More info here

adept@HogWarts:/etc/nginx/sites-available$ curl -i localhost/api/authentication/check/user/email
HTTP/1.1 404 Not Found
Server: nginx/1.2.1
Date: Mon, 22 Apr 2013 22:49:03 GMT
Content-Length: 0
Connection: keep-alive

adept@HogWarts:/etc/nginx/sites-available$ curl -i localhost:8080/authentication/check/user/email
HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 22 Apr 2013 22:49:20 GMT
Transfer-Encoding: chunked

{"user":["false"],"emailAddress":["false"]}

标签: nginx
3条回答
唯我独甜
2楼-- · 2019-01-23 12:22

By some reason proxy_pass in Nginx 1.6.2 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;
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-23 12:27

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

查看更多
时光不老,我们不散
4楼-- · 2019-01-23 12:37

This

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

Needs to be this

location /api/ {
    proxy_pass http://backend/;
}
查看更多
登录 后发表回答