Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

add CORS support for nginx #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/nginx_unicorn/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Capistrano
module NginxUnicorn
VERSION = "0.1.0"
VERSION = "0.1.1"
end
end
26 changes: 26 additions & 0 deletions lib/generators/capistrano/nginx_unicorn/templates/nginx_conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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/ {
Expand Down