隞乩gist.github.com舀reverse proxied APIs蝭靘:: ?; G3 ?6 U4 \) {# s. W
6 y7 y4 ?, ]) Q! I7 E/ {. ]
( d$ f4 H+ P: @# CORS header support
; f7 S! G1 _5 w/ m' R4 U#/ x4 _5 j% E8 `
# One way to use this is by placing it into a file called "cors_support"
3 E9 H2 J0 @0 w# }+ a' q* v5 h" B# under your Nginx configuration directory and placing the following
9 Z6 ~5 v+ P" \7 |5 [; e9 s* f# statement inside your **location** block(s):" E; H8 [) b' T0 e
#
, z |: A6 D) w" S' [# include cors_support;
: h. w% D( H; Z9 E H#! e" g$ s0 O* z; v0 W( ?0 z
# As of Nginx 1.7.5, add_header supports an "always" parameter which
9 ?- w! z, O2 I8 i$ _# allows CORS to work if the backend returns 4xx or 5xx status code.$ |4 o" e. y$ M( C1 A
#+ K" r: B/ f" ^2 e5 s
# For more information on CORS, please see: http://enable-cors.org/2 C' N/ `& x2 m* W$ [
# Forked from this Gist: https://gist.github.com/michiel/1064640
$ ^; }5 N2 M% y/ `9 a#
5 L" P; K" K, a4 f
/ I! D1 @9 _; u; x* Z% j% qset $cors '';% D) M8 z; ?& m; o
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {
: J" E! Q) D4 O X4 Z; E* _ set $cors 'true';) C4 Y- L, w% r' I- F
}
! c8 ^$ N& H# f" R% h3 z# K' c6 \$ P- @/ Q1 X: y0 x
if ($cors = 'true') {! j4 a, v. E% a$ Y
add_header 'Access-Control-Allow-Origin' "$http_origin" always;
* |( |' B$ w' J2 v& S0 p add_header 'Access-Control-Allow-Credentials' 'true' always;
2 T3 d Z6 s) }, O, F# ~ add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
( e" q- _+ V% G7 C5 Q% b/ v' V add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
+ v0 [4 d4 V; f/ R$ H # required to be able to read Authorization header in frontend1 E4 ?0 Q5 u+ i# g9 B6 C) ^, c& w
#add_header 'Access-Control-Expose-Headers' 'Authorization' always;$ A9 H+ B3 l. X$ E
}9 Z! \1 O) C, Z0 j
7 x' O: ^" f, a6 X3 M: b- M/ r3 z- A3 a
if ($request_method = 'OPTIONS') {
$ S2 T* e' W/ Z5 c* ` # Tell client that this pre-flight info is valid for 20 days2 z2 l& K8 |' q- ? b
add_header 'Access-Control-Max-Age' 1728000;
) v! E* I9 p+ m add_header 'Content-Type' 'text/plain charset=UTF-8';
3 o' L% a9 y2 c4 A/ R5 \: X add_header 'Content-Length' 0;
; N# Z; X' d; i! d& ^ return 204;
: Y. ]% n3 k" i3 q v9 t3 f5 A& ?9 f3 c! x} https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:
, ^- v4 d z3 C, O& Jif ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) { return 444;
# C! l" Z% d, X, G) O: k}) W; a j" `. |6 ?
set $origin $http_origin;- @: z8 g6 R( J+ @
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {6 q) v# N- M4 z2 w6 A
set $origin 'https://default.yourdom.zone';) V' m4 x; r% K9 Y9 ^
}+ F, y6 g$ W* N! m* v: W6 J
if ($request_method = 'OPTIONS') {
, ?! j. j F7 l) i$ A3 y2 c) ~' k add_header 'Access-Control-Allow-Origin' "$origin" always;
! Q# Y' C& X3 ?! x4 [8 T$ M add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;. B! y! X$ c' u9 Z7 {
add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
$ Q% p% |# e7 ], h add_header 'Access-Control-Allow-Credentials' 'true' always;
: ` [. m/ s c1 K' I3 i add_header Access-Control-Max-Age 1728000; #20 days
5 ~1 C4 f% } @- B- ]& U* ^ add_header Content-Type 'text/plain charset=UTF-8';# A& Y; e, t* _* i" _
add_header Content-Length 0;7 x# f9 }7 P; h! s8 T( s( N
return 204;, p2 y7 H6 f: |# M+ c: F# i
}5 |% q) C; ], `! P! _3 q
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
, X# S# a7 B/ q3 Y$ m e add_header Access-Control-Allow-Origin "$origin" always;
5 ~9 |5 n" `1 A1 ~/ I+ q# c add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;$ V6 `8 C' H' J( Z4 J; D a; E
add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
# x6 y, p* H9 E0 Q9 C8 w+ ? add_header Access-Control-Allow-Credentials true always;9 r( y7 O6 [2 B. J6 ~
} Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:# based on https://gist.github.com/4165271/8 o, e: `( h j( n _7 |9 Y
#$ w1 H7 U$ n" x8 E* I1 a) @9 [/ m; ~
# Slightly tighter CORS config for nginx
" }9 R& |' A- j4 r! V) n- j#+ A" x; U: |( A" H/ Z# B. S
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs4 n! t: S4 _: @; ^' X: {, C. ~
#
$ o/ w, I: C- E4 M) x# Despite the W3C guidance suggesting that a list of origins can be passed as part of, j% ~3 t& o4 G; B, U; |- F' \+ m
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
, A7 T; B2 n. r# don't seem to play nicely with this.9 V7 T% n: f6 I! C: @ z
#
1 Z' h7 }6 V6 n) M: i6 E2 s; Q# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting9 J& E2 |8 ^; R% s
# method to control access instead.- G' V, }, w, _& v2 J9 f* Z
#$ Z1 |( V7 k5 ?9 q) c
# NB: This relies on the use of the 'Origin' HTTP Header.
" ~+ i- n! e9 J: A/ @ ~ `$ ` @% `
) e- F% S+ Q2 s% W9 O2 v' alocation / {. U, n w+ m, H7 @! w
8 w: |7 t) l; B# e0 O+ u0 S' I% M if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {& g I3 @* z0 ^% D( F
set $cors "true";; {: B# l: j; l0 R2 Q
}
* ^5 S' E' e0 g( f+ X7 u
( w2 L( m! L4 N5 o # Nginx doesn't support nested If statements. This is where things get slightly nasty.$ ~) u b, w7 ]& F% o
# Determine the HTTP request method used
$ k8 r6 E3 y, r3 {2 Q; } if ($request_method = 'OPTIONS') {' m L O7 I2 ^$ Y: n4 g% U1 L0 o2 B
set $cors "${cors}options";
5 E0 G9 K& F& m. { }3 t* I0 X+ c1 `7 f
if ($request_method = 'GET') {
* J7 r+ y5 ^9 t8 U set $cors "${cors}get";3 A7 U* R$ Y: Y
}" z. V0 M9 x" h& _
if ($request_method = 'POST') {2 s* f, W, W4 k8 W" m3 _( A% g3 f
set $cors "${cors}post";2 r/ m. Y9 ]0 q: T
}0 _3 y* I) f6 C, s
' Z( f; F' V2 g; Y if ($cors = "true") {
2 Q9 C1 g4 X. u( ]6 @ # Catch all incase there's a request method we're not dealing with properly
1 {& Y4 f- i3 b8 O3 e: N add_header 'Access-Control-Allow-Origin' "$http_origin";
+ `$ v& X) ]8 @) ^ }
$ ?. v* T0 r! y
% |! F D4 p. A4 u! M: h) y2 p/ g if ($cors = "trueget") {
9 o) i; D" v$ c( Q add_header 'Access-Control-Allow-Origin' "$http_origin";& r0 `( ]5 m. u* n4 D$ l% P
add_header 'Access-Control-Allow-Credentials' 'true';: @5 w. b [7 @% E
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
8 y- @( s9 f$ R, p1 g add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
4 q. l5 a% `% @' L$ {- P+ C }2 C* j, A# O* L" g4 s4 @( `
' J4 i! e9 E8 I0 h4 D0 S" b' C if ($cors = "trueoptions") {
# P/ x5 q- `7 d6 U) n add_header 'Access-Control-Allow-Origin' "$http_origin";# p* J8 d8 o j2 z
; A& M' v" j& i #+ Y) n5 E7 {8 d, ?9 j
# Om nom nom cookies" P8 S# {3 L* t4 o( B% w% ?
#
0 t9 r# X- @4 q1 ^ add_header 'Access-Control-Allow-Credentials' 'true';( h0 W, C/ p# l' b" |/ W0 n
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';& l8 K7 X, o' M6 i& i5 @, @/ K
' d& ]- O4 J4 L# J; d( m #
5 b3 d& {* i" ~4 p5 p0 r7 c/ T # Custom headers and headers various browsers *should* be OK with but aren't( M- v5 h" ]" g; Z; e L
#
) R6 W: `8 ^2 R# h/ R3 m$ e6 w) T add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" C! z3 Y$ z$ h. S& V
F9 y& I3 V3 x) n7 u5 X. ^
#
3 d% E- p( s5 w4 e: ~5 i # Tell client that this pre-flight info is valid for 20 days/ U0 c5 w. _0 I6 P: i
#
9 J/ M( Z; e ]2 ~% B7 H! _ add_header 'Access-Control-Max-Age' 1728000;
5 H) @8 J6 X% P add_header 'Content-Type' 'text/plain charset=UTF-8';
6 G, q. b+ I0 z# e' H add_header 'Content-Length' 0;
/ z) ?0 L" L. l2 u' [3 U7 b: b return 204;
5 R, T2 b9 P% V8 v) g/ N0 Q1 @ }, j! D1 t" e" [) Q
/ a. q' q4 L3 Q" K4 m) @
if ($cors = "truepost") {
+ s4 r8 J' B' D add_header 'Access-Control-Allow-Origin' "$http_origin";5 Y* Q# M; g" T! L+ U1 R
add_header 'Access-Control-Allow-Credentials' 'true';
" }; O5 d$ M: l: C: u3 X4 l add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
+ \0 E9 P! L* i0 h: Q. I add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';- l% \6 y! t5 a% S+ h, ?& T
}
0 t+ {/ g! N7 O& f" m9 V1 W3 k! J8 E: e/ E% E
} : \) _3 d1 \0 F, V- S/ n: W* W
! w3 X) u, U1 s& M/ f; C2 ` |
|