我想调用一个命名位置来处理反向代理指令。因此,我在虚拟主机部分尝试了以下几行:
upstream node_upstream {
server 127.0.0.1:8000;
keepalive 64;
}
server {
server_name my.host-name.com;
listen 80;
access_log /home/webhost/logs/my-host-name.access.log;
error_log /home/webhost/logs/my-host-name.error.log;
root /home/webhost/www/my.host-name.com/node-canary/public;
location / {
index index.html;
try_files $uri $uri.html $uri/index.html $uri/ @node;
}
location @node {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://node_upstream/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
这很简单,但运行它时,我得到了
nginx: [emerg] "proxy_pass" cannot have URI part in location
given by regular expression, or inside named location, or inside
"if" statement, or inside "limit_except" block in
/home/webhost/sites-enabled/my.host-name.com:29
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
对我来说这意味着我不能使用代理通行证在里面@节点命名位置。
因此,我认为我可以通过简单地将位于 @node 位置的指令移到根(即 /)位置来超越 nginx。外面的任何东西都保持不变。像这样:
server {
server_name my.host-name.com;
listen 80;
access_log /home/webhost/logs/my-host-name.access.log;
error_log /home/webhost/logs/my-host-name.error.log;
root /home/webhost/www/my.host-name.com/node-canary/public;
location / {
index index.html;
try_files $uri $uri.html $uri/index.html $uri/ $uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://node_upstream/;
proxy_redirect off;
proxy_read_timeout 240s;
}
}
但使用此设置,会发生一些奇怪的事情。当访问现有静态文件时,请求由代理连接(节点应用程序)应答 -而不是通过静态文件本身来响应。
这是请求返回有效响应的唯一情况。任何不匹配静态文件的路由都会返回 nginx 500 服务器错误。
这让我困惑不已,甚至震惊不已。
答案1
该错误信息"proxy_pass" cannot have URI part in ...
只是指/
您proxy_pass
语句中的尾随内容。
使用:
proxy_pass http://node_upstream;
代替:
proxy_pass http://node_upstream/;
看这个文件了解详情。