title: nginx redirects POST requests to GET request
body: <p>I have Rails 4.1 application with runs on <code>puma</code> web server. I use nginx as a proxy server. Several days ago everything worked very well. I updated my application, and suddenly some <code>POST</code> requests started to redirected to same url but as <code>GET</code> request. I've tried rollback to previous working versions, no success. </p>
  
  <p>I found very interesting behaviour. I tested my API with <code>curl</code>. </p>
  
  <ul>
  <li>If I did <code>POST</code> request to the url
  <code>http://myapp.com/tasks/easy_task/calculate/</code> it redirects to same url
  but as <code>GET</code> request.</li>
  <li>Then I did <code>POST</code>request to <code>http://myapp.com/</code>, returned 404</li>
  <li>Then I did <code>POST</code>request to <code>http://myapp.com/tasks</code>, returned 404</li>
  <li>Then I did <code>POST</code>request to <code>http://myapp.com/tasks/easy_task</code>, returned 404</li>
  <li>Then I did <code>POST</code>request to <code>http://myapp.com/tasks/easy_task/calculate</code>, returned 200. YAY!</li>
  </ul>
  
  <p>Same thing happened when I used chrome's app <code>Postman</code>. First it redirected, but after previous steps it works well.</p>
  
  <p>I use this app in my other application. I use <code>RestClient</code> to make http requests. When I try to make <code>POST</code> request it raises an exception <code>RestClient::MovedPermanently (301 Moved Permanently)</code>.</p>
  
  <ul>
  <li>I reinstalled <code>nginx</code> to <code>1.7.3</code>. </li>
  <li>Restarted server (virtual machine)</li>
  <li>re deployed my app, deployed previous versions</li>
  <li>no success :(</li>
  </ul>
  
  <p>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!</p>
  
  <p>Similar questions:
   - <a href="https://stackoverflow.com/questions/24028719/post-request-turns-into-get-request">POST request turns into GET request</a>
   - <a href="https://stackoverflow.com/questions/24028322/post-request-mysteriously-turn-into-get-request">POST request mysteriously turn into GET request</a></p>
  
  <p>nginx config:</p>
  
  <pre><code>$ 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;
  }
  </code></pre>
  
  <p>Puma</p>
  
  <pre><code>$ bundle exec puma -d -e production -b unix:///tmp/myapp.com-puma.sock --pidfile /home/projects/mycompany/myapp.com/shared/tmp/pids/puma.pid
  $
  </code></pre>
  
  <p>Example of access.log</p>
  
  <pre><code>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"
  </code></pre>