【问题标题】:Nginx Reverse Proxy 405 PUT ChromeNginx 反向代理 405 PUT Chrome
【发布时间】:2019-05-20 02:17:45
【问题描述】:

当我尝试通过 Nginx 反向代理向我的 Tomcat 服务器请求时收到 405 响应。

如果我直接向 tomcat 服务器请求,它可以从 Chrome 或 Firefox 成功运行。但是,如果我向 Nginx 代理请求,我会从 Chrome 获得 405,但可以在 Firefox 中使用。

我一直在我的 nginx.conf 中使用不同的配置进行测试 位置/foo/ { ... }

我试过了:

error_page 405 =200 $uri;

还有

 add_header "Allow" "GET, POST, HEAD, PUT, DELETE" always;
    add_header "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS" always;`

location / {
    dav_methods PUT DELETE;
    proxy_pass http://csprocure;
}

我用于重定向的 nginx.conf 如下:

upstream serverS {
   server xxx.xxx.xxx.xxx:yyyy;
}

server {

    listen 80;
    server_name $hostname;
    access_log  /var/log/nginx/access.log;
    error_log   /var/log/nginx/error.log;
    error_log   /dev/stdout info;
    access_log  /dev/stdout;

    location /foo/ {
        proxy_pass         http://serverS;
        proxy_redirect     http://xxx.xxx.xxx.xxx/ http://xxx.xxx.xxx.xxx:yyyy/;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   X-Forwarded-Proto: https;
    }

我可以在我的配置中添加什么来避免 Chrome 中的 405?

【问题讨论】:

  • 请将您的答案作为答案发布,而不是在问题中。
  • 只接受一个答案,而不是改变你的标题说它已经解决了。
  • @JoshLee 添加为答案,谢谢
  • @csmckelvey 我刚刚添加了一个答案,我已经接受了。谢谢。

标签: google-chrome tomcat put nginx-reverse-proxy http-status-code-405


【解决方案1】:

我一直在比较 Firefox 和 Chrome 之间的请求,并且 Chrome 端存在差异,在请求 Chrome 中,在导致错误的标头请求中添加“Origin”参数。

所以在我的 nginx.conf 中,我添加了参数以避免它在 location/ 下。

proxy_set_header Origin "";

所以我现在的位置配置块是:

location /foo/ {
    proxy_set_header Origin "";
    proxy_pass         http://serverS;
    proxy_redirect     http://xxx.xxx.xxx.xxx/ http://xxx.xxx.xxx.xxx:yyyy/;
    proxy_set_header   Host $host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Host $server_name;
    proxy_set_header   X-Forwarded-Proto: https;
}

【讨论】:

  • 我可以确认发生了这种情况。刚刚添加了替换和放置/删除再次开始工作。谢谢。
【解决方案2】:

可以在here找到一个很好的关于将HTTP方法列入白名单的在线解释

这对我有用:

add_header Allow "GET, POST, HEAD, PUT, DELETE" always;

if ( $request_method !~ ^(GET|POST|HEAD|PUT|DELETE)$ ) {
   return 405;
}

..显然,在测试更改之前,请确保检查配置 nginx -t 并重新加载配置 nginx -s reload

【讨论】:

  • 它对我不起作用。确实,标头存在问题,但我认为这不是 Nginx 阻止的,因为当我从 Firefox 浏览器请求时,一切正常。只有当请求来自 Chrome 时才会失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 2011-08-09
  • 2013-02-18
  • 2021-04-08
  • 2019-03-19
  • 2016-07-22
相关资源
最近更新 更多