-
-
Save jyyan/9930e909b87007c8a19856ba65cfed7b to your computer and use it in GitHub Desktop.
My nginx config to allow CORS (cross-site) uploads to Amazon S3, with added config e.g. timeouts & security
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
# DO NOT RESPOND TO REQUESTS OTHER THAN yourdomain.com | |
server { | |
listen 80 default; | |
server_name _; | |
return 444; | |
} | |
# FILE UPLOADS | |
server { | |
listen 80; | |
server_name yourdomain.com www.yourdomain.com; | |
access_log /var/log/nginx/s3uploadproxy.access.log; | |
error_log /var/log/nginx/s3uploadproxy.error.log; | |
proxy_read_timeout 10; | |
proxy_connect_timeout 10; | |
# allow big files... | |
client_max_body_size 30M; | |
# and slow uploads | |
proxy_send_timeout 1200; | |
# Deny illegal Host headers | |
if ($host !~* ^(yourdomain.com|www.yourdomain.com)$ ) { | |
return 444; | |
} | |
# dissalow methods | |
if ($request_method !~ ^(OPTIONS|POST)$ ) { | |
# empty response | |
return 444; | |
} | |
location / { | |
# CORS PRE-FLIGHT REQUESTS | |
if ($request_method = 'OPTIONS') { | |
more_set_headers 'Access-Control-Allow-Origin: *'; | |
more_set_headers 'Access-Control-Allow-Methods: POST, OPTIONS'; | |
more_set_headers 'Access-Control-Max-Age: 1728000'; | |
more_set_headers 'Content-Type: text/plain; charset=UTF-8'; | |
#more_set_headers 'Access-Control-Allow-Headers: '; | |
return 200; | |
} | |
# FILE UPLOADS | |
if ($request_method = 'POST') { | |
more_set_headers 'Access-Control-Allow-Origin: *'; | |
proxy_pass http://your-s3-bucket; | |
} | |
} | |
# 204 (No Content) for favicon.ico | |
location = /favicon.ico { | |
#empty_gif; | |
return 204; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment