NGINX Reverse Proxy durch Forward Proxy mit socat

2019-09-29 · christian · http, nginx

Wie bereits in einem früheren Beitrag beschrieben, kann man mit proxy_pass Anfragen durch einen Forward Proxy leiten, in dem man als Proxy URL einfach den Forward Proxy verwendet und via proxy_set_header den HOST header auf das eigentliche Ziel einstellt.

Im Testbetrieb hat sich nun aber heraus gestellt, dass dies nur mit HTTP Traffic funktioniert. Soll eine TLS Verbindung aufgebaut werden, schlägt dies fehl, da NGINX nicht in der Lage ist den PROXY CONNECT Prozess korrekt auszuführen.

Zur Rettung kommt hier das Tool socat, welches ganz offiziell und ohne Tricks einen Proxy Connect beherrscht:

socat TCP4-LISTEN:8443,reuseaddr,fork,bind=127.0.0.1 \
    PROXY:corporate-proxy.serverless.industries:webservice.example.com:443,proxyport=8080

Das ganze als systemd unit:

[Unit]
Description=Socat API Proxy
After=network.target

[Service]
ExecStart=/usr/bin/socat TCP4-LISTEN:8443,reuseaddr,fork,bind=127.0.0.1 PROXY:corporate-proxy.serverless.industries:webservice.example.com:443,proxyport=8080
Restart=on-failure
Type=simple

[Install]
WantedBy=multi-user.target
Alias=socat-api-proxy.service

In der NGINX Konfiguration wird dann der socat Port verwendet:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name webserviceproxy.serverless.industries;

    ssl_certificate /etc/ssl/http/bundle_serverless.industries.cert;
    ssl_certificate_key /etc/ssl/http/serverless.industries.key;

    location / {
        proxy_set_header Host webservice.example.com;
        proxy_pass https://localhost:8443;
    }
}

Quelle: https://stackoverflow.com/a/46824465


More


serverless.industries BG by Carl Lender (CC BY 2.0) Imprint & Privacy
0afe8347 2024-04-20 23:58
Mastodon via chaos.social Mastodon via einbeck.social