1
http {

    server {
        listen              4443 ssl;
        server_name         {{hostname}};
        port_in_redirect off;

        ssl_certificate     /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;

        location / {
            # https://stackoverflow.com/questions/32845674/setup-nginx-not-to-crash-if-host-in-upstream-is-not-found/32846603#32846603
            resolver 127.0.0.1 valid=30s;
            proxy_pass http://app1:8080;
        }
    }

    server {
        listen              4444 ssl;
        server_name         X.{{hostname}};
        port_in_redirect off;

        ssl_certificate     /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;

        location / {
            resolver 127.0.0.1 valid=30s;
            proxy_pass http://app1:8080/my/path/;
        }
    }

}

# http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html
# https://stackoverflow.com/questions/34741571/nginx-tcp-forwarding-based-on-hostname/40135151#40135151

stream {

    upstream app1 {
        server 127.0.0.1:4443;
    }


    server {
        listen 0.0.0.0:443;
        proxy_connect_timeout 10s;
        proxy_timeout 5m;

        proxy_pass $target;
        ssl_preread on;
    }


    access_log  /var/log/nginx/access.log basic;
    error_log  /var/log/nginx/error.log error;
}

This is my nginx configuration. This is part of a docker-compose setup. I proxy requests sent to https://hostname to the app1 container successfully.

Now my goal is to send requests sent to https://X.hostname to the app1 container, however to a different path.

For example:

Client sends request to https://A.hostname Nginx forwards it to https://app1/my/path/

However the path is not being preserved, it's just forwarding it to https://app1 Tried many different solutions available online, and none worked.

Michel
  • 11
  • 2
  • I believe you need to add the path on the end of `proxy_pass http://app1:8080` --> `proxy_pass http://app1:8080/my/path/` – flyingscot5 Aug 06 '21 at 09:24
  • @flyingscot5 but I want requests coming to {{hostname}} to actually go to http://app1:8080 But I want requests coming to X.{{hostname}} to go to http://app1:8080/my/path/ – Michel Aug 06 '21 at 12:39

0 Answers0