我遇到了一个问题,我需要将proxy_pass变为变量,但是当我将其变为变量时,它无法在后端服务器上正确解析
nginx -v
nginx version: nginx/1.6.2
这是工作中的一个:
server {
listen 443 ssl;
ssl on;
server_name api.hostname.com;
include ssl_params;
location = /v1 {
return 302 /v1/;
}
location /v1/ {
proxy_pass http://internal-api.hostname.com.us-east-1.elb.amazonaws.com/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
这是一个破碎的
server {
listen 443 ssl;
ssl on;
server_name api.hostname.com;
include ssl_params;
location = /v1 {
return 302 /v1/;
}
location /v1/ {
resolver 10.0.0.2;
set $backend "http://internal-api.hostname.com.us-east-1.elb.amazonaws.com/";
proxy_pass $backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
工作日https://api.hostname.com/v1/profile的后端结果是:
<-- GET /profile
--> GET /profile 200 16ms 349b
但在破碎的那个
<-- GET /
--> GET / 401 1ms 12b
我试过了:
set $backend "internal-api.hostname.com.us-east-1.elb.amazonaws.com";
proxy_pass http://$backend/
set $backend "internal-api.hostname.com.us-east-1.elb.amazonaws.com/";
proxy_pass http://$backend/
每当我在proxy_pass中使用变量时,它会将所有内容转发到后端的/(root),绝对不是我想要的。
答案 0 :(得分:2)
我遇到了完全相同的问题(需要变量,因为在AWS上内部IP更改)。经过两天的测试/调查,我认为已找到一种有效的配置(变量中没有/或proxy_pass,但有“重写中断”以删除后端请求上不需要的路径):
resolver 10.0.0.2;
set $backend "http://internal-api.hostname.com.us-east-1.elb.amazonaws.com";
location /v1/ {
proxy_pass $backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite ^/v1/(.*) /$1 break;
}
答案 1 :(得分:0)