我有 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;
}
}