Last active
November 9, 2020 18:06
-
-
Save m4ttbrock/7337183 to your computer and use it in GitHub Desktop.
Nginx Nodejs CORS to subdomain
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
server { | |
listen 80; | |
server_name subdomain.example.com; | |
access_log /var/log/nginx/example.access.log; | |
location / { | |
if ($http_origin ~* (https?://.*\.example\.com(:[0-9]+)?)) { | |
set $cors "true"; | |
} | |
if ($request_method = 'OPTIONS') { | |
set $cors "${cors}options"; | |
} | |
if ($request_method = 'GET') { | |
set $cors "${cors}get"; | |
} | |
if ($request_method = 'POST') { | |
set $cors "${cors}post"; | |
} | |
if ($cors = "trueget") { | |
add_header 'Access-Control-Allow-Origin' "$http_origin"; | |
add_header 'Access-Control-Allow-Credentials' 'true'; | |
} | |
if ($cors = "truepost") { | |
add_header 'Access-Control-Allow-Origin' "$http_origin"; | |
add_header 'Access-Control-Allow-Credentials' 'true'; | |
} | |
if ($cors = "trueoptions") { | |
add_header 'Access-Control-Allow-Origin' "$http_origin"; | |
add_header 'Access-Control-Allow-Credentials' 'true'; | |
add_header 'Access-Control-Max-Age' 1728000; | |
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; | |
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since'; | |
add_header 'Content-Length' 0; | |
add_header 'Content-Type' 'text/plain charset=UTF-8'; | |
return 204; | |
} | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-NginX-Proxy true; | |
proxy_pass_header Set-Cookie; | |
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment; | |
proxy_pass http://127.0.0.1:8675; | |
proxy_redirect off; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great, it's working perfectly thanks :-)