July
July

Reputation: 913

Nginx reverse proxy return 404

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 / {
    }
}

Upvotes: 5

Views: 20630

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

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.

Upvotes: 15

Related Questions