Nginx reverse proxy return 404

13,978

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.

Share:
13,978

Related videos on Youtube

July
Author by

July

Updated on September 15, 2022

Comments

  • July
    July over 1 year

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