Last active
June 24, 2020 07:03
-
-
Save almet/2f5abb56d59592359c0113ab47946330 to your computer and use it in GitHub Desktop.
Nginx reverse proxy to add CORS support to an API. Tested and working with Firefox 68.9 and Chrome 80.0
This file contains hidden or 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
server { | |
server_name xxx | |
location / { | |
# Activer le proxy | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_pass https://yourapi; | |
proxy_redirect off; | |
proxy_buffers 32 16k; | |
proxy_busy_buffers_size 64k; | |
add_header 'Access-Control-Allow-Origin' '*' always; | |
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; | |
add_header 'Access-Control-Allow-Headers' 'User-Agent, Origin, X-Requested-With, Content-Type, Accept' always; | |
add_header 'Access-Control-Allow-Credentials' 'true' always; | |
if ($request_method = OPTIONS ) { | |
add_header 'Access-Control-Allow-Origin' '*' always; | |
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; | |
add_header 'Access-Control-Allow-Headers' 'User-Agent, Origin, X-Requested-With, Content-Type, Accept' always; | |
add_header 'Access-Control-Allow-Credentials' 'true' always; | |
add_header Content-Length 0; | |
add_header Content-Type text/plain; | |
return 200; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment