From 0d949f38e2c094f03cbd5b80e86f49358c6505c0 Mon Sep 17 00:00:00 2001 From: Lorel Date: Sun, 10 Jan 2016 14:00:29 +0100 Subject: [PATCH] add CORS support for nginx --- CHANGELOG.md | 4 +++ README.md | 4 +++ lib/capistrano/nginx_unicorn/version.rb | 2 +- .../nginx_unicorn/templates/nginx_conf.erb | 26 +++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fda0e3d..a1266c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### From 0.1.0 to 0.1.1 + +* Add support for CORS + ### From 0.0.8 to 0.1.0 * Separate nginx and unicorn tasks into the appropriate files. diff --git a/README.md b/README.md index 0efc361..4d54612 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,10 @@ set :nginx_ssl_certificate_key, "#{nginx_server_name}.key" # default value: `/etc/nginx/sites-available` set :nginx_config_path, "/etc/nginx/sites-available" +# nginx CORS config +# set to `*` for wide-open CORS +set :nginx_cors_allow_origin, "*" + # path, where unicorn pid file will be stored # default value: `"#{current_path}/tmp/pids/unicorn.pid"` set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid" diff --git a/lib/capistrano/nginx_unicorn/version.rb b/lib/capistrano/nginx_unicorn/version.rb index 0d4debf..fa70f84 100644 --- a/lib/capistrano/nginx_unicorn/version.rb +++ b/lib/capistrano/nginx_unicorn/version.rb @@ -1,5 +1,5 @@ module Capistrano module NginxUnicorn - VERSION = "0.1.0" + VERSION = "0.1.1" end end diff --git a/lib/generators/capistrano/nginx_unicorn/templates/nginx_conf.erb b/lib/generators/capistrano/nginx_unicorn/templates/nginx_conf.erb index 7d25198..54590d4 100644 --- a/lib/generators/capistrano/nginx_unicorn/templates/nginx_conf.erb +++ b/lib/generators/capistrano/nginx_unicorn/templates/nginx_conf.erb @@ -40,6 +40,32 @@ server { # limit_req zone=one; access_log <%= shared_path %>/log/nginx.access.log; error_log <%= shared_path %>/log/nginx.error.log; + + +<% if fetch(:nginx_cors_allow_origin) %> + if ($request_method = 'OPTIONS') { + add_header 'Access-Control-Allow-Origin' '<%= fetch(:nginx_cors_allow_origin) %>'; + add_header 'Access-Control-Allow-Credentials' 'true'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; + add_header 'Access-Control-Max-Age' 1728000; + add_header 'Content-Type' 'text/plain charset=UTF-8'; + add_header 'Content-Length' 0; + return 204; + } + if ($request_method = 'POST') { + add_header 'Access-Control-Allow-Origin' '<%= fetch(:nginx_cors_allow_origin) %>'; + add_header 'Access-Control-Allow-Credentials' 'true'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; + } + if ($request_method = 'GET') { + add_header 'Access-Control-Allow-Origin' '<%= fetch(:nginx_cors_allow_origin) %>'; + add_header 'Access-Control-Allow-Credentials' 'true'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; + } +<% end %> } location ^~ /assets/ {