Nginx – Rewrite in Nginx Does not work for POST request

domain-name-systemload balancingnginxrewrite

I have two VPS(one is for fail over), both running Nginx as a Load balancer for backend servers. Also, I have Enabled and configured SSL for my domain(registered on GoDaddy.com) on Nginx.

Since I am using DNS A records to configure the domain for VPS fail over therefore I cannot enter port numbers(only IP number is allowed).So, by default the domain pings to port 80. But ssl listens on port 443.

For this I am using rewrite method in the server block of nginx but it doesn't work for the POST request. Although it works for a GET request .
I know that proxy_pass can be used with POST request but that is not the correct practice(Unsafe). I should be posting a request directly on HTTPS connection.

here is my nginx conf:

upstream tomcat_servers{
    least_conn;
    server 123.45.678.90:8080;
    server 124.345.78.23:8180;
    }

server {
   listen  80;
   server_name thedomain.com;
   rewrite ^(.*) https://$host$request_uri? permanent;
      }


 server {
   listen 443;
   server_name thedomain.com;

....ssl config.....


  location / {
         proxy_pass   http://tomcat_servers;                                                                                   
         proxy_http_version 1.1;
         proxy_connect_timeout 30s;
         proxy_read_timeout 30s;
         proxy_send_timeout 30s;
         proxy_next_upstream timeout;
         proxy_next_upstream error invalid_header http_500 http_502 http_404 http_503 http_504;
  }
 }

How should I configure to make it work with POST requests in the given scenario.

Thanks

Best Answer

You should not use redirect for post. If you have to, you should use 307 code for post. A end-user may be prompted about redirection.

W3C Specifications - RFC 2616

301 Moved Permanently If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request.

307 Temporary Redirect If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

Look also here