-
-
Save iver/4f9616631008ea3a27ba0b3e63135381 to your computer and use it in GitHub Desktop.
Example Nginx configuration for adding cross-origin resource sharing (CORS) support to reverse proxied APIs
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
# | |
# CORS header support | |
# | |
# One way to use this is by placing it into a file called "global/cors.conf" | |
# under your Nginx configuration directory and placing the following | |
# statement inside your **location** block(s): | |
# | |
# include global/cors.conf; | |
# | |
# As of Nginx 1.7.5, add_header supports an "always" parameter which | |
# allows CORS to work if the backend returns 4xx or 5xx status code. | |
# | |
# For more information on CORS, please see: http://enable-cors.org/ | |
# Forked from this Gist: https://gist.github.com/michiel/1064640 | |
# | |
# If you prefer to use map, you can use it as follow | |
# ------------------------------------------------------ | |
map $http_origin $cors { | |
# indicates all map values are hostnames and should be parsed as such | |
hostnames; | |
# default value | |
default 'false'; | |
# all your domains | |
localhost 'true'; | |
www.yourdomain.com 'true'; | |
www.yourother.com 'true'; | |
} | |
server { | |
# .... server configuration section | |
} | |
# -------------------------------------------------- | |
# Put this in /etc/nginx/global/cors.conf | |
# -------------------------------------------------- | |
set $cors ''; | |
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') { | |
set $cors 'true'; | |
} | |
# The above is used only if map is not used | |
# -------------------------------------------------- | |
if ($cors = 'true') { | |
add_header 'Access-Control-Allow-Origin' "$http_origin" always; | |
add_header 'Access-Control-Allow-Credentials' 'true' always; | |
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; | |
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always; | |
# required to be able to read Authorization header in frontend | |
#add_header 'Access-Control-Expose-Headers' 'Authorization' always; | |
} | |
if ($request_method = 'OPTIONS') { | |
# Tell client that this pre-flight info is valid for 20 days | |
add_header 'Access-Control-Max-Age' 1728000; | |
add_header 'Content-Type' 'text/plain charset=UTF-8'; | |
add_header 'Content-Length' 0; | |
return 204; | |
} | |
# Include the cors configuration in the location section | |
# ------------------------------------------------------ | |
server { | |
root /var/www/htdocs; | |
index index.php index.html index.htm; | |
server_name yourserver.com www.yourserver.com; | |
location / { | |
... | |
include global/cors.conf; | |
} | |
} | |
RESOURCES | |
* The map use: https://www.digitalocean.com/community/tutorials/how-to-use-nginx-s-map-module-on-ubuntu-16-04 | |
* The gist source: https://gist.github.com/Stanback/7145487 |
You are right! I forgot to update that part of the code. Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have repeated the critical vulnerability from Stanback's gist. Please re-read my comment: https://gist.github.com/Stanback/7145487#gistcomment-2044821