Skip to content

Instantly share code, notes, and snippets.

@qiujun8023
Last active September 11, 2020 09:27
Nginx 任意域名反向代理
map $host $proxy_upstream_mapped {
hostnames;
helm-charts-proxy.* 'https://kubernetes-charts.storage.googleapis.com';
gitlab-registry-proxy.* 'https://registry.gitlab.com';
quay-registry-proxy.* 'https://quay.io';
default 'https://httpbin.org';
}
server {
listen 80;
server_name *.example.com;
resolver 8.8.8.8;
location / {
proxy_pass $proxy_upstream_mapped;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirect;
}
location @handle_redirect {
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
}
server {
listen 80;
server_name localhost;
resolver 8.8.8.8 ipv6=off;
location / {
return 400;
}
location ~ ^(/proxy)?/(http|https)/(.*?)/ {
rewrite ^(/proxy)?/(http|https)/(.*?)/(.*) /$4 break;
#proxy_ssl_verify off;
proxy_ssl_server_name on;
proxy_set_header Host $3;
proxy_redirect off;
proxy_pass $2://$3;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirect;
}
location @handle_redirect {
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment