我有 nginx 代理到应用服务器,具有以下配置:
location /app/ {
# send to app server without the /app qualifier
rewrite /app/(.*)$ /$1 break;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9001;
proxy_redirect http://localhost:9001 http://localhost:9000;
}
任何对 /app 的请求都会转到 :9001,而默认站点托管在 :9000。
GET 请求工作正常。但每当我向 /app/any/post/url 提交 POST 请求时,都会导致 404 错误。通过 GET /app/any/post/url 直接在浏览器中访问 URL 会按预期访问应用服务器。
我在网上找到了其他有类似问题的人,并补充道
proxy_set_header Host $http_host;
但这并没有解决我的问题。
没有记录任何错误,访问日志如下:
# For http://localhost:9000 (no proxying)
127.0.0.1 - - [08/Dec/2012:12:29:28 -0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
# For http://localhost:9000/app/get/priorities (GET proxy)
127.0.0.1 - - [08/Dec/2012:12:32:17 -0800] "GET /app/get/priorities HTTP/1.1" 200 77 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
# When posting to http://localhost:9000/app/create/priority:
127.0.0.1 - - [08/Dec/2012:12:33:50 -0800] "POST /app/create/priority/ HTTP/1.1" 404 188 "http://localhost:9000/form-test.html" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4"
任何见解都值得赞赏。
谢谢。
完整配置如下:
server {
listen 9000; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6
root /home/scott/src/ph-dox/html;
# root ../html; TODO: how to do relative paths?
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /app/ {
# rewrite here sends to app server without the /app qualifier
rewrite /app/(.*)$ /$1 break;
proxy_set_header Host $http_host;
proxy_pass http://localhost:9001;
proxy_redirect http://localhost:9001 http://localhost:9000;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
}
答案1
我的错,问题不是出在 nginx 上,而是出在我的应用服务器上。我使用的路由模块要求我明确指定请求方法(如果不是 get 方法),所以在发布时会抛出 404 错误,而直接访问浏览器网址时不会抛出错误。