我尝试将后端NodeJS Docker容器从端口映射到子域。为此,我尝试配置:
map $sub $prt {
include /var/www/ports.map;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /var/...;
ssl_certificate_key /var/...;
ssl_trusted_certificate /var/...;
server_name ~^(?P<sub>.+?)-api\.cnpgc\.embrapa\.br$;
location / {
add_header x-sub "$sub";
add_header x-prt "$prt";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
proxy_ssl_session_reuse off;
proxy_pass http://localhost:$prt;
}
}
但是,当尝试访问例如“ cria-certo-api.cnpgc.embrapa.br”时,出现错误“ 502 Bad Gateway”。如果我将最后一行从
proxy_pass http://localhost:$prt;
到
proxy_pass http://localhost:9080;
代理正常工作!因此,出现此错误是由于URL中存在变量“ $ prt”引起的。这没有道理。
我用行调试地图:
add_header x-sub "$sub";
add_header x-prt "$prt";
它也起作用。有帮助吗?