Created
October 15, 2021 06:40
-
-
Save acanalesg/c684d59d5eaeb586c6def035850967d8 to your computer and use it in GitHub Desktop.
Nginx reverse proxy CORS fix
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
# | |
# Run in the foreground locally | |
# nginx -p . -c nginx.conf | |
# | |
user nginx; | |
worker_processes auto; | |
error_log /var/log/nginx/error.log notice; | |
pid /var/run/nginx.pid; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' | |
'$status $body_bytes_sent "$http_referer" ' | |
'"$http_user_agent" "$http_x_forwarded_for"'; | |
access_log /var/log/nginx/access.log main; | |
sendfile on; | |
#tcp_nopush on; | |
keepalive_timeout 65; | |
#gzip on; | |
# include /etc/nginx/conf.d/*.conf; | |
server { | |
listen 8060; | |
location /score { | |
proxy_pass http://host.docker.internal:6789/score; | |
include cors_support; | |
} | |
} | |
} | |
# Con un fichero cors_support con el siguiente contenido: | |
# | |
# 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 | |
# | |
set $cors ''; | |
#if ($http_origin ~ '^http?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)') { | |
# set $cors 'true'; | |
#} | |
set $cors 'true'; | |
if ($cors = 'true') { | |
add_header 'Access-Control-Allow-Origin' "$http_origin" always; | |
#add_header 'Access-Control-Allow-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-Allow-Origin' "$http_origin" 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; | |
add_header 'Access-Control-Max-Age' 1728000; | |
add_header 'Content-Type' 'text/plain charset=UTF-8'; | |
add_header 'Content-Length' 0; | |
return 204; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
docker run --name nginx-proxy4 -p 127.0.0.1:8060:8060 -v /home/acg/nginx.conf:/etc/nginx/nginx.conf:ro -v /home/acg/cors_support:/etc/nginx/cors_support -d nginx