why nginx rewrite post request from /login to //login?
- by jiangchengwu
There is a if statement, which will rewrite url when the client is Android. Everything ok.
But, something got strange.
Nginx will write post request /login to //login, even if the block of if statement is bank.
So I got a 404 page. As the jetty server only accept /login request.
Server conf:
location / {
proxy_pass http://localhost:8785/;
proxy_set_header Host $http_host;
proxy_set_header Remote-Addr $http_remote_addr;
proxy_set_header X-Real-IP $remote_addr;
if ( $http_user_agent ~ Android ){
# rewrite something, been commented
}
}
Debug info, origin log https://gist.github.com/3799021
...
2012/09/28 16:29:49 [debug] 26416#0: *1 http script regex: "Android"
2012/09/28 16:29:49 [notice] 26416#0: *1 "Android" matches "Android/1.0", client: 106.187.97.22, server: ireedr.com, request: "POST /login HTTP/1.1", host: "ireedr.com"
...
2012/09/28 16:29:49 [debug] 26416#0: *1 http proxy header:
"POST //login HTTP/1.0
Host: ireedr.com
X-Real-IP: 106.187.97.22
Connection: close
Accept-Encoding: identity, deflate, compress, gzip
Accept: */*
User-Agent: Android/1.0
"
...
2012/09/28 16:29:49 [debug] 26416#0: *1 HTTP/1.1 404 Not Found
Server: nginx/1.2.1
Date: Fri, 28 Sep 2012 08:29:49 GMT
Content-Type: text/html;charset=ISO-8859-1
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: must-revalidate,no-cache,no-store
Content-Encoding: gzip
...
Only when I commented the block in the configration file:
location / {
proxy_pass http://localhost:8785/;
proxy_set_header Host $http_host;
proxy_set_header Remote-Addr $http_remote_addr;
proxy_set_header X-Real-IP $remote_addr;
#if ( $http_user_agent ~ Android ){
#
#}
}
The client can get an 200 response.
Debug info, origin log https://gist.github.com/3799023
...
"POST /login HTTP/1.0
Host: ireedr.com
X-Real-IP: 106.187.97.22
Connection: close
Accept-Encoding: identity, deflate, compress, gzip
Accept: */*
User-Agent: Android/1.0
"
...
2012/09/28 16:27:19 [debug] 26319#0: *1 HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Fri, 28 Sep 2012 08:27:19 GMT
Content-Type: application/json;charset=UTF-8
Content-Length: 17
Connection: keep-alive
...
As the log:
2012/09/28 16:29:49 [notice] 26416#0: *1 "Android" matches "Android/1.0", client: 106.187.97.22, server: ireedr.com, request: "POST /login HTTP/1.1", host: "ireedr.com"
2012/09/28 16:29:49 [debug] 26416#0: *1 http script if
2012/09/28 16:29:49 [debug] 26416#0: *1 post rewrite phase: 4
2012/09/28 16:29:49 [debug] 26416#0: *1 generic phase: 5
2012/09/28 16:29:49 [debug] 26416#0: *1 generic phase: 6
2012/09/28 16:29:49 [debug] 26416#0: *1 generic phase: 7
2012/09/28 16:29:49 [debug] 26416#0: *1 access phase: 8
2012/09/28 16:29:49 [debug] 26416#0: *1 access phase: 9
2012/09/28 16:29:49 [debug] 26416#0: *1 access phase: 10
2012/09/28 16:29:49 [debug] 26416#0: *1 post access phase: 11
2012/09/28 16:29:49 [debug] 26416#0: *1 try files phase: 12
2012/09/28 16:29:49 [debug] 26416#0: *1 posix_memalign: 0000000001E798F0:4096 @16
2012/09/28 16:29:49 [debug] 26416#0: *1 http init upstream, client timer: 0
2012/09/28 16:29:49 [debug] 26416#0: *1 epoll add event: fd:13 op:3 ev:80000005
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: "Host: "
2012/09/28 16:29:49 [debug] 26416#0: *1 http script var: "ireedr.com"
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: "
"
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: ""
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: ""
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: "X-Real-IP: "
2012/09/28 16:29:49 [debug] 26416#0: *1 http script var: "106.187.97.22"
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: "
"
2012/09/28 16:29:49 [debug] 26416#0: *1 http script copy: "Connection: close
"
2012/09/28 16:29:49 [debug] 26416#0: *1 http proxy header: "Accept-Encoding: identity, deflate, compress, gzip"
2012/09/28 16:29:49 [debug] 26416#0: *1 http proxy header: "Accept: */*"
2012/09/28 16:29:49 [debug] 26416#0: *1 http proxy header: "User-Agent: Android/1.0"
2012/09/28 16:29:49 [debug] 26416#0: *1 http proxy header:
"POST //login HTTP/1.0
Host: ireedr.com
X-Real-IP: 106.187.97.22
Connection: close
Accept-Encoding: identity, deflate, compress, gzip
Accept: */*
User-Agent: Android/1.0
"
...
Maybe post rewrite phase had rewrite the request.
Anybody can help me to solve this problem or know why nginx do that ?
Much appreciated.