Skip to content

Instantly share code, notes, and snippets.

@leesalminen
Last active January 2, 2023 16:04
Show Gist options
  • Save leesalminen/aabb3416f8658c89d56768c2afa7f930 to your computer and use it in GitHub Desktop.
Save leesalminen/aabb3416f8658c89d56768c2afa7f930 to your computer and use it in GitHub Desktop.
Nginx CORS proxy
map $sent_http_access_control_allow_origin $cors {
default "";
"" "*";
}
location ~* /proxy/(?<pschema>https?):/(?<phost>[\w.]+)(?<puri>\/.*) {
set $adr $pschema://$phost;
rewrite .* $puri break;
resolver 8.8.8.8;
proxy_pass $adr;
add_header 'Access-Control-Allow-Origin' $cors;
add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS';
proxy_set_header Host $phost;
proxy_ssl_server_name on;
proxy_redirect off;
proxy_connect_timeout 1;
expires 30;
recursive_error_pages on;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirects;
}
location @handle_redirects {
resolver 8.8.8.8;
set $saved_redirect_location '$upstream_http_location';
add_header 'Access-Control-Allow-Origin' $cors;
add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS';
proxy_pass $saved_redirect_location;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment