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?
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
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
This
location /api {
proxy_pass http://backend;
}
Needs to be this
location /api/ {
proxy_pass http://backend/;
}
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;
}