我试图根据环境变量将请求代理到不同的目标.我的方法是将目标url放入自定义变量$ target并将其提供给proxy_pass.
但是使用带有proxy_pass的变量似乎不起作用.这个简单的配置导致nginx的"502 Bad Gateway"响应.
server {
listen 8080;
server_name myhost.example.com;
access_log /var/log/nginx/myhost.access.log;
location /proxy {
set $target http://proxytarget.example.com;
proxy_pass $target;
}
}
Run Code Online (Sandbox Code Playgroud)
没有变量的相同配置有效:
server {
listen 8080;
server_name myhost.example.com;
access_log /var/log/nginx/myhost.access.log;
location /proxy {
proxy_pass http://proxytarget.example.com;
}
}
Run Code Online (Sandbox Code Playgroud)
是不是真的不能以这种方式使用proxy_pass,或者我只是做错了什么?