Nginx rewrite data to publish

I need to save POST data to another URL

Rewriting works, but mail data is lost

need to send data from user_info.php to userhistory

location ~ user_info.php { rewrite ^/.* http://testing.com/userhistory permanent; } 

Data is lost. How to save data?

+5
source share
3 answers

Basically, you want to automatically redirect a POST request using 301 redirects.

However. such redirects are expressly prohibited by the HTTP Specification , which states that:

If the status code 301 is received in response to a request other than GET or HEAD, the user agent SHOULD NOT automatically redirect the request if it cannot be confirmed by the user, as this can change the conditions under which the request was issued.

The specifications also note that:

When automatically redirecting a POST request after receiving a status code 301, some existing HTTP / 1.0 user agents mistakenly change it to a GET request.

I believe that the second situation can happen, and that while the target server is waiting for POST data, it receives GET data.

Your options:

a. Change the code to work with GET data, or better yet, both POST and GET. IE, look for POST, and if not, try GET equivalents.

C. Try to make sure the code receives POST data while working with Spec.

You may be able to achieve selection B using the proxy_pass directive to process the request.

Sort of:

 location ~ user_info.php { proxy_pass http://testing.com/userhistory; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } 

Thus, the user is not technically redirected.

+2
source

In my conf I use try_files with regex

eg

 location /yourfolder/(?<specialRequest>.*) { try_files $uri /yourfolder/index.php?r=$specialRequest; return 307 https://$host/yourfolder/index.php?r=$specialRequest; // it also work } 
+2
source

You just need to write an Nginx rewrite rule with an HTTP status code of 307 or 308 :

 location ~ user_info.php { return 307 http://testing.com/userhistory; } 

The Http 307 or 308 status code should be used instead of 301 because it changes the request method from POST to GET. contact https://tools.ietf.org/id/draft-reschke-http-status-308-07.html#introduction

Also redirecting through return better than rewrite according to nginx doc: https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites

+1
source

Source: https://habr.com/ru/post/1210456/


All Articles