Last active
January 2, 2023 16:04
-
-
Save leesalminen/aabb3416f8658c89d56768c2afa7f930 to your computer and use it in GitHub Desktop.
Nginx CORS proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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