我遇到了需要将 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),这绝对不是我想要的。