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; } }
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"]}
Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.
The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.
To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.
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 directed 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
This
location /api { proxy_pass http://backend; }
Needs to be this
location /api/ { proxy_pass http://backend/; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With