Skip to content

Instantly share code, notes, and snippets.

@edvaldoszy
Created August 3, 2020 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvaldoszy/1cbacbed50c90b5b96f44d8eba3c8967 to your computer and use it in GitHub Desktop.
Save edvaldoszy/1cbacbed50c90b5b96f44d8eba3c8967 to your computer and use it in GitHub Desktop.
CORS on NGINX
# https://enable-cors.org/server_nginx.html
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
index index.html index.htm;
location ~* "^/storage/(?<att>.+\.pdf)$" {
alias /usr/share/nginx/html/$att;
expires 1y;
add_header Cache-Control "public";
# Adiciona permissão de CORS para permitir pre-visualização no browser
# add_header Access-Control-Allow-Origin *;
# add_header Content-disposition "attachment";
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
# add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# 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;
# add_header 'Content-Disposition' 'filename="$att"';
return 204;
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range,Content-Disposition';
add_header 'Content-Disposition' 'filename="$att"';
}
}
# location / {
# add_header Access-Control-Allow-Origin *;
# root /usr/share/nginx/html;
# index index.html index.htm;
# }
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment