![nginx 反向代理用上游的 ip 地址替换 url](https://linux22.com/image/760936/nginx%20%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E7%94%A8%E4%B8%8A%E6%B8%B8%E7%9A%84%20ip%20%E5%9C%B0%E5%9D%80%E6%9B%BF%E6%8D%A2%20url.png)
我正在使用 nginx1.14.2作为具有以下配置的反向代理
server {
listen xxxxxxx:80;
server_name xxxxxx;
proxy_intercept_errors on;
location /nodejs {
proxy_pass https://nodejs.org;
proxy_cache cache;
proxy_cache_valid 200 301 302 30d;
proxy_cache_valid 404 1m;
expires 30d;
proxy_ssl_server_name on;
proxy_cache_use_stale error timeout invalid_header updating;
}
}
我越来越502错误的网关在浏览器上:在此处输入图片描述 在 nginx 错误日志中,我发现以下行:
2020/12/10 11:23:23 [error] 16462#16462: *1 connect() failed (110: Connection timed out) while connecting to upstream, client: xxxxxxx, server: xxxxxxxx, request: "GET /nodejs HTTP/1.1", upstream: "https://104.20.23.46:443/nodejs", host: "xxxxxxxx"
但是由于后端有代理,带有 IP 地址的 URL 被阻止。您知道如何强制 nginx 在上游使用域名代替 IP 地址吗?
谢谢。
编辑:我在 proxy_pass 配置中添加了以下行:
rewrite /nodejs/(.*) /dist break;
现在错误日志如下:
2020/12/10 12:22:37 [error] 16541#16541: *1 connect() failed (110: Connection timed out) while connecting to upstream, client: xxxxxxx, server: xxxxxxxx, request: "GET /nodejs HTTP/1.1", upstream: "https://104.20.22.46:443/dist", host: "xxxxxxx"
答案1
您需要这个指令:proxy_ssl_server_name on;
默认是关闭的。
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_server_name