nginx 将 POST 请求重定向到 GET 请求 [英] nginx redirects POST requests to GET request

查看:237
本文介绍了nginx 将 POST 请求重定向到 GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在 puma 网络服务器上运行的 Rails 4.1 应用程序.我使用 nginx 作为代理服务器.几天前,一切都运行良好.我更新了我的应用程序,突然一些 POST 请求开始重定向到相同的 url 但作为 GET 请求.我试过回滚到以前的工作版本,没有成功.

I have Rails 4.1 application with runs on puma web server. I use nginx as a proxy server. Several days ago everything worked very well. I updated my application, and suddenly some POST requests started to redirected to same url but as GET request. I've tried rollback to previous working versions, no success.

我发现了非常有趣的行为.我用 curl 测试了我的 API.

I found very interesting behaviour. I tested my API with curl.

  • 如果我对 url 做了 POST 请求http://myapp.com/tasks/easy_task/calculate/ 它重定向到相同的网址但作为 GET 请求.
  • 然后我对http://myapp.com/做了POST请求,返回404
  • 然后我对http://myapp.com/tasks做了POST请求,返回404
  • 然后我对http://myapp.com/tasks/easy_task做了POST请求,返回404
  • 然后我对http://myapp.com/tasks/easy_task/calculate做了POST请求,返回200.YAY!
  • If I did POST request to the url http://myapp.com/tasks/easy_task/calculate/ it redirects to same url but as GET request.
  • Then I did POSTrequest to http://myapp.com/, returned 404
  • Then I did POSTrequest to http://myapp.com/tasks, returned 404
  • Then I did POSTrequest to http://myapp.com/tasks/easy_task, returned 404
  • Then I did POSTrequest to http://myapp.com/tasks/easy_task/calculate, returned 200. YAY!

当我使用 chrome 的应用程序 Postman 时发生了同样的事情.首先它重定向,但在前面的步骤之后它运行良好.

Same thing happened when I used chrome's app Postman. First it redirected, but after previous steps it works well.

我在其他应用程序中使用此应用程序.我使用 RestClient 发出 http 请求.当我尝试发出 POST 请求时,它会引发异常 RestClient::MovedPermanently (301 Moved Permanently).

I use this app in my other application. I use RestClient to make http requests. When I try to make POST request it raises an exception RestClient::MovedPermanently (301 Moved Permanently).

  • 我将 nginx 重新安装到 1.7.3.
  • 重启服务器(虚拟机)
  • 重新部署我的应用程序,部署以前的版本
  • 没有成功:(

我在 stackoverflow 上发现了类似的问题,但没有一个给我解决这个问题的线索.我希望你能帮我解决这个问题.提前致谢!

I found similar questions on stackoverflow, but non of them gave me clue to fix this issue. I hope you can help me to solve this problem. Thanks in advance!

类似问题:- POST请求变成GET请求- POST请求神秘地变成了GET请求

nginx 配置:

$ cat /etc/nginx/sites-enabled/myapp.com.conf
# The file generated by Chef for mycompany

upstream myapp_mycompany_com {
  server unix:/tmp/myapp.com-puma.sock;
}

server {
  server_name  myapp.com;
  listen       80;

  access_log /var/log/nginx/myapp.com-access.log;
  error_log /var/log/nginx/myapp.com-error.log;

  root /home/projects/mycompany/myapp.com/current/public;

  gzip on;
  gzip_types text/plain text/xml application/xml application/xml+rss
             text/css text/javascript application/javascript application/json;

  error_page 551 =503 @maintenance;
  location @maintenance {
    rewrite ^(.*)$ /system/maintenance.html break;
  }
  set $maintenance 0;
  if (-f $document_root/system/maintenance.html) {
    set $maintenance 1;
  }

  if ($request_uri = /favicon.ico) {
    # Browsers will try to get favicon if it's not returned with 200ok status
    set $maintenance 0;
  }
  if ($maintenance) {
    # There can be several reasons for 503 error. We custom return 551 error
    # to ensure maintenance.html is only shown when it's really maintenance
    return 551;
  }

  rewrite ^/(.*)/$ /$1 permanent; # Truncate trailing slashes
  try_files $uri @rails;

  expires -1;

  location = /favicon.ico {
    try_files $uri =204;
    access_log off;
    log_not_found off;
  }

  location @rails {
    proxy_pass http://myapp_mycompany_com;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_intercept_errors on;
    expires -1;
  }

  error_page 500 502 503 504 /500.html;
  error_page 403 /403.html;
  error_page 404 /404.html;

  client_max_body_size 50M;
  keepalive_timeout 10;
}

彪马

$ bundle exec puma -d -e production -b unix:///tmp/myapp.com-puma.sock --pidfile /home/projects/mycompany/myapp.com/shared/tmp/pids/puma.pid
$

access.log 示例

Example of access.log

123.123.123.123 - - [11/Jul/2014:05:44:17 +0000] "POST /tasks/easy_task/calculate/ HTTP/1.1" 301 184 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"
123.123.123.123 - - [11/Jul/2014:05:44:17 +0000] "GET /tasks/easy_task/calculate HTTP/1.1" 404 713 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"

...

123.123.123.123 - - [11/Jul/2014:06:04:17 +0000] "POST / HTTP/1.1" 404 713 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"
123.123.123.123 - - [11/Jul/2014:06:04:26 +0000] "POST /tasks HTTP/1.1" 404 713 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"
123.123.123.123 - - [11/Jul/2014:06:04:36 +0000] "POST /tasks/easy_task HTTP/1.1" 404 713 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"
123.123.123.123 - - [11/Jul/2014:06:04:42 +0000] "POST /tasks/easy_task/calculate HTTP/1.1" 200 104 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2073.0 Safari/537.36"

推荐答案

我找到了解决方案.当我执行 POST 请求时,我使用以斜杠结尾的 url,例如 http://myapp.com/tasks/easy_task/calculate/

I've found solution. When I did POST request, I used url which ends with slash, like http://myapp.com/tasks/easy_task/calculate/

当我最后使用没有斜线的 url 时,比如 http://myapp.com/tasks/easy_task/calculate 一切正常!

When I used url without slash in the end, like http://myapp.com/tasks/easy_task/calculate everything works perfectly!

我认为是因为这个规则

rewrite ^/(.*)/$ /$1 permanent; # Truncate trailing slashes

我正在关闭这个问题.明天.

I am closing this issue. Tomorrow.

这篇关于nginx 将 POST 请求重定向到 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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