nginx $request_method gets changed from POST to GET before logging due to internal redirects
NGINX Version: 1.10.3 on a Debain 9.1 OS with Jessie-Backports
nginx.conf
:
user www-data;
pid /var/run/nginx.pid;
worker_processes 1;
error_log /var/log/nginx/error.log;
events {
worker_connections 1024;
}
http {
log_format main 'request_method:$request_method request:$request';
server {
listen 80;
error_page 405 =200 $uri;
access_log /var/log/nginx/access.log main;
location / {
default_type text/html;
root /usr/share/nginx/html/;
include mime.types;
index index.html index.htm index.asp index.aspx index.php default.html default.htm default.asp default.aspx default.php;
}
error_page 403 /403.html;
error_page 400 401 402 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /400.html {
root /usr/share/nginx/error-pages/Apache/2.4.20;
}
location = /403.html {
root /usr/share/nginx/error-pages/Apache/2.4.20;
}
location = /404.html {
root /usr/share/nginx/error-pages/Apache/2.4.20;
}
location = /50x.html {
root /usr/share/nginx/error-pages/Apache/2.4.20;
}
}
}
When I POST
this server since nginx cannot serve static pages on POST
, I get a 405
response code. But I need to serve static page on a POST
request also.
So most workarounds on the internet suggested this snippet of code = error_page 405 =200 $uri;
This worked fine for the purpose of serving static content to a POST
request with response code 200
, BUT
Due to the $uri
, internal redirects of nginx causes the $request_method
to change from POST
to GET
and in the /var/log/nginx/access.log
, I get this:request_method:GET request:POST /Login.html HTTP/1.1\n
instead of request_method:POST request:POST /Login.html HTTP/1.1\n
Any workaround to solve this?
Solution 1:
error_page code ... [=[response]] uri
edits the URI and method for the current level.
To not change the URI and method, just enter another context (named location).
error_page 405 = @fallback; location @fallback { return 200 $uri; }