Nginx 反向代理返回 404 [英] Nginx reverse proxy return 404

查看:127
本文介绍了Nginx 反向代理返回 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Nginx 安装并运行,下面是 /etc/nginx/nginx.conf 的配置,我想将所有 /api/* 转发到我的 tomcat 服务器,它在端口 9100 的同一台服务器上运行(键入 http://myhost:9100/api/apps 有效),否则,在 '/usr/share/nginx/html' 下提供静态文件.现在我输入 http://myhost/api/apps 给出 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 / {
    }
}

推荐答案

proxy_pass 语句可以可选在将 URI 传递给上游之前对其进行修改.有关详细信息,请参阅本文档.

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

以这种形式:

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

URI /api/foo 被传递给 http://myserver/foo.

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

通过从 proxy_pass 语句中删除尾随的 /:

By deleting the trailing / from the proxy_pass statement:

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

URI /api/foo 现在被传递给 http://myserver/api/foo.

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

这篇关于Nginx 反向代理返回 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆