Nginx reverse proxy replace url by ip address in upstream

nginxreverse-proxy

I'm using nginx 1.14.2 as a reverse proxy with following configuration

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;
        }
}

I'm getting 502 Bad gateway on browser: enter image description here
In nginx errors logs, i found following line:

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"

But since there is a proxy in the backend, the URL with IP address is blocked.
Have you an idea how to force nginx to use domainname in place of IP address in upstream

Thanks.

Edit:
I added following line to the proxy_pass config:

rewrite /nodejs/(.*) /dist  break;

Now the error log is like this :

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"

Best Answer

You need this directive: proxy_ssl_server_name on;
The default is off.

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ssl_server_name

Related Topic