Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx reverse proxy return 404

Tags:

nginx

My Nginx installed and running, below is the config from /etc/nginx/nginx.conf , I want to forward all /api/* to my tomcat server, which is running on the same server at port 9100(type http://myhost:9100/api/apps works) , otherwise, serve static file under '/usr/share/nginx/html'. Now I type http://myhost/api/apps give an 404. What's the problem here?

upstream  myserver {
    server   localhost:9100 weight=1;
}

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;



    location ^~ /api/ {
       proxy_pass http://myserver/;
    }

    location / {
    }
}
like image 347
July Avatar asked Dec 15 '22 01:12

July


1 Answers

The proxy_pass statement may optionally modify the URI before passing it upstream. See this document for details.

In this form:

location ^~ /api/ {
    proxy_pass http://myserver/;
}

The URI /api/foo is passed to http://myserver/foo.

By deleting the trailing / from the proxy_pass statement:

location ^~ /api/ {
    proxy_pass http://myserver;
}

The URI /api/foo is now passed to http://myserver/api/foo.

like image 141
Richard Smith Avatar answered Jan 06 '23 04:01

Richard Smith