Skip to content

Instantly share code, notes, and snippets.

@zefer
Created February 18, 2011 13:29
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save zefer/833647 to your computer and use it in GitHub Desktop.
Save zefer/833647 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
# 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;
}
}
@cmer
Copy link

cmer commented Jun 3, 2013

Hi,

I am very interested in doing something similar. I was hoping to find a way to set all the hidden form fields (tokens, acl, aws key, etc) directly in nginx rather than in the html form.

Could you explain a bit more how you use this code on your end?

thanks!

@ankit-brijwasi
Copy link

Thanks! this saved my day

@zefer
Copy link
Author

zefer commented Dec 7, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment