一、问题发现
笔者拥有一个刨根问底的倔脾气、
于是就一发不可收拾,非要搞明白到底是怎么回事。
环境: nginx作为代理服务器 SayHelloServlet
SayHelloServlet 访问路径 sayHello GET方法
原始访问路径:http://127.0.0.1:8080/app/sayHello?name=ldd
起因:调用一个Servlet 访问路径为 http://127.0.0.1/sayHello?name=ldd
结果:
HTTP Status 404 - /app/sayHello%3Fname=ldd
type Status report
message /app/sayHello%3Fname=ldd
description The requested resource is not available.
Apache Tomcat/8.0.28
于是,开始了问题剖析。
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format access '$remote_addr - $remote_user [$time_local] "$request_uri" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log access; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; upstream app{ server 127.0.0.1:8080; keepalive 1200; } #gzip on; server { listen 80; server_name localhost; #charset koi8-r; access_log logs/host.access.log access; location =/ { rewrite ^(.*)$ /app$1 last; } location / { rewrite ^(.*)$ /app$request_uri last; } location /app { access_log logs/app.log access; proxy_pass http://app; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } nginx.conf
如果有以下两种配置
配置一、 location / { rewrite ^(.*)$ /app$request_uri last; } 配置二、 location / { rewrite ^(.*)$ /app$1 last; }
配置三
location / { rewrite ^(.*)$ /app$request_uri redirect; }
配置四
location / { rewrite ^(.*)$ /app$request_uri permanent; }
很奇怪,若使用配置二、三、四则都能够访问正常;若使用配置一,则会出现上述错误。
而且,app.log中的日志
127.0.0.1 - - [15/Jul/2016:10:19:58 +0800] "/sayHello?name=ldd" 404 1040 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-"
并不会带/app/。
真是有太多疑问出现了。
二、引经据典
rewrite学习
这里又需要用到正则表达式的相关知识,不懂的话可以先学一下。
三、最终
最终还是不能够合理解释这个问题,先预存在这里,欢迎大家指教。