tid=101068&以下是gist.github.com支援reverse proxied APIs的範例:
0 d- I5 K1 Q1 p+ E% U6 n% s7 `4 i/ C+ O7 [
! H3 ~: ^) h$ n6 G( R0 b3 j( r2 p# CORS header support: Y! D T& r5 ?& A
#) d" m, a+ I0 v
# One way to use this is by placing it into a file called "cors_support"+ U% M2 C, o7 v5 i
# under your Nginx configuration directory and placing the following
8 n+ F0 ]0 H8 W$ t, q2 a# ~# statement inside your **location** block(s):. w3 p1 B D! Q
#$ z7 |; ^# `( r9 \' S: e
# include cors_support;
# S$ ?! {3 C. D* s* p# {#/ [$ k2 _9 N. y; m% O l) T
# As of Nginx 1.7.5, add_header supports an "always" parameter which
0 a! H! T( o c% f2 e# allows CORS to work if the backend returns 4xx or 5xx status code." H3 m/ d& t3 z0 V* y x; H
#
) e: S$ J: ], H' I# For more information on CORS, please see: http://enable-cors.org/
& K# m- e$ i P; H# Forked from this Gist: https://gist.github.com/michiel/1064640
I& k& V9 ]1 y/ p#
+ A0 H6 B- t. @! v" d
1 n; M) `* J) c4 Tset $cors '';
9 j, V7 k2 M9 p. i8 Mif ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {
4 s' G4 @4 i$ Q1 z& m6 B1 C2 q set $cors 'true';8 `- i$ j/ y: }3 g8 \5 O' t
}' y, n4 |* ]9 \- u) k; i9 H
8 b! R7 N% ^ O5 J6 Aif ($cors = 'true') {
& p/ t# Z5 x! i6 S add_header 'Access-Control-Allow-Origin' "$http_origin" always;1 z- Q6 s6 Z/ h' H( |
add_header 'Access-Control-Allow-Credentials' 'true' always;/ k; {5 W$ S3 t: `7 w* O) s( ]
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;% e$ Q3 V) Z) n( _! m9 c" z l
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;# w1 n0 w" ^1 ^; ~0 e; L! E3 T
# required to be able to read Authorization header in frontend
7 r8 w) [- g3 o% B% ]7 a$ V) k #add_header 'Access-Control-Expose-Headers' 'Authorization' always;3 m" ~* c0 ?9 K# }; s% ?
}
( E l8 f1 E( M B2 }% l/ n! A3 }5 _! |6 k8 |
if ($request_method = 'OPTIONS') {
1 `9 B, [$ g+ Z* C: L # Tell client that this pre-flight info is valid for 20 days
" J( B0 v [; s4 E add_header 'Access-Control-Max-Age' 1728000;
8 S* q a! Y1 k7 q/ c* S- O add_header 'Content-Type' 'text/plain charset=UTF-8';
) L5 U" _. x; m add_header 'Content-Length' 0;
, H$ o2 H+ s, W* O! l return 204;: c* A2 K: i2 x
} https://gist.github.com/Stanback/7145487#file-nginx-conf 的討論範例:( ]; [! h" T# D% G" ~5 i# P
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) { return 444;
+ X5 K+ X4 |0 F! t- ?1 g4 t}
' Y5 ?' s( U, S5 [3 h9 pset $origin $http_origin; N* @& T+ o, P- G& _1 Q! F2 z+ h# E
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {0 p% n- x- u% s
set $origin 'https://default.yourdom.zone';' }3 L2 F j' O# s1 z
}
' s" s+ }4 ^& _/ Cif ($request_method = 'OPTIONS') {8 l. D- b2 ?& h/ t- l
add_header 'Access-Control-Allow-Origin' "$origin" always;8 K, Y6 r2 I$ r2 F _5 C
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;8 t0 o" i5 d' ^# Z+ d
add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;1 D0 h7 L- B7 j1 G# J3 _
add_header 'Access-Control-Allow-Credentials' 'true' always;& ?) p8 d; m4 e: Z
add_header Access-Control-Max-Age 1728000; #20 days
) m$ ~6 C; @# k8 F1 d' J0 u) F2 O5 f4 z add_header Content-Type 'text/plain charset=UTF-8';! b: i0 a$ U# s
add_header Content-Length 0;5 b8 ?: j5 D: e e6 n+ q( r: ~, ]! o1 U
return 204;
0 s7 n" V% e, `}( p( m: u4 W) K
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {/ P/ v8 a' R- a, L
add_header Access-Control-Allow-Origin "$origin" always;
: M9 Y, I2 E6 I E" X/ W Z' v add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
* s: X. T2 }( T" v add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
" ]0 @2 l' n0 } add_header Access-Control-Allow-Credentials true always;0 k7 D& j/ p4 O3 C. M7 `3 y
} Access-Control-Allow-Origin Multiple Origin Domains? 的例子:# based on https://gist.github.com/4165271/
6 _$ ~2 K- b- x( |/ Z#
1 E! Y: Y9 k$ I. t6 H& r% s2 M0 L# Slightly tighter CORS config for nginx. ?7 ]7 E1 l' |8 g) Q' d
#; j( @0 n' ^! f
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs* W8 M& A2 `- M& L
#
+ K+ [5 K8 G) D0 n$ V+ b8 D% x# Despite the W3C guidance suggesting that a list of origins can be passed as part of
2 u4 ~/ [$ }1 P _+ K% P# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
' B2 D' R: d9 ^8 s' U# don't seem to play nicely with this.1 o- I4 ~9 A$ G: V1 v6 _# ^$ f
#7 D( K1 w( P5 N& o' e; ]
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting7 {2 P/ F; N# w5 Z8 N
# method to control access instead.$ }' H+ R8 v' }5 d6 Z
#
- @$ O6 z0 W+ g# NB: This relies on the use of the 'Origin' HTTP Header.
5 p. ~& \ `. Z$ Z* w/ c9 C
- y: y* p2 S6 |/ Glocation / {
# k R, g! M, N& ~
; J& m7 g* |, `; d7 o, k if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {, o* |# M, k$ c5 m# i: r3 D6 c" Q
set $cors "true";
& z, H/ G% q4 l+ M/ z }
o0 F9 l; q7 f: F) x# ^' ]& k: M$ f
# Nginx doesn't support nested If statements. This is where things get slightly nasty.
7 b( B! i& R0 v; {9 u # Determine the HTTP request method used
. U& e+ M0 R, r* h4 C4 Z if ($request_method = 'OPTIONS') {
$ t1 {3 t5 m7 V/ S+ e0 M set $cors "${cors}options";5 y- s+ [! g! V: p
}
; j* l% e$ D( u7 j if ($request_method = 'GET') {/ U1 c6 q- i9 j! |* M3 F
set $cors "${cors}get";" w9 ^/ N/ e+ y
}
( A3 S7 z4 B; J8 G, T" b6 u! l if ($request_method = 'POST') {
7 y) W7 ~3 |0 @6 V set $cors "${cors}post";
- k) o' a. }" ~$ C I. m }3 _6 T; O' ]: c! |
3 H/ u: ~5 f0 o9 h
if ($cors = "true") {- l- W; K7 i v/ I4 R5 _5 f
# Catch all incase there's a request method we're not dealing with properly
9 V! z5 q; h# M9 S5 ~. p$ Q* v add_header 'Access-Control-Allow-Origin' "$http_origin";' J6 N* s5 O# N- j
}
) p, t& n* F; H4 M2 _! ?3 D4 v% e3 ]2 ]5 z# f
if ($cors = "trueget") {
3 b! P7 t2 G# p+ [+ ]1 Z3 p add_header 'Access-Control-Allow-Origin' "$http_origin";
" @" O5 E% \# F( C0 [ add_header 'Access-Control-Allow-Credentials' 'true';
2 O: f9 I/ z: Z1 c& m/ r# V add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';# S& [+ ]: M/ T3 X6 g! i: ]
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';# W: S+ X! g! H+ ]9 u/ E
}
N+ O: v- _) g. @( g
/ y& p9 ` J* T. ^ n if ($cors = "trueoptions") {
- ]9 `* }: ^) N1 c4 }% M9 i( D add_header 'Access-Control-Allow-Origin' "$http_origin";8 V$ _# p2 L. |7 e5 g
. W5 X1 O9 y9 F0 u/ t0 q #
$ C) l( k& k+ N. d% { # Om nom nom cookies
/ P8 \' m2 z% U# M# f #
. l2 [; [3 S1 j' X add_header 'Access-Control-Allow-Credentials' 'true';& g c* f/ m2 N
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
# q) F( y9 T0 o' d" Q. w
- U/ ^- d' R' n1 x # N2 ?4 R2 A6 R# F% R
# Custom headers and headers various browsers *should* be OK with but aren't
. ^1 q/ E: S! r# L) b- b6 x' o9 @ #
* J9 |& d6 M0 ?5 Z add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
2 h/ R: p" S" U5 v2 s- H3 j5 K2 U) o
#
7 J$ c' T1 ?3 K+ H. E # Tell client that this pre-flight info is valid for 20 days9 M- P4 G) ]7 C, q0 f
#
$ K5 F& [. K! V1 g7 t add_header 'Access-Control-Max-Age' 1728000;
1 P k, b6 {, p9 ~1 \, k: b( d add_header 'Content-Type' 'text/plain charset=UTF-8';6 o1 a' P5 ^0 R& f3 ~, z$ P' o
add_header 'Content-Length' 0;- _2 q* k0 k# C4 P; Z$ A3 w
return 204;
0 F! c9 S( C& `8 ~* l* R% j }
3 e8 i# Q1 ?5 E* I8 i1 N K$ U( O) X
if ($cors = "truepost") {7 @& X5 a! g N, q6 K+ u( H1 Z
add_header 'Access-Control-Allow-Origin' "$http_origin";
]- p& u8 B( P m- R6 u; q add_header 'Access-Control-Allow-Credentials' 'true';% m2 y( W6 |, s9 Q4 Z5 w' M* k
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
5 V" o1 D$ e( v0 _ add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
0 M; F' t2 r4 J& f" t }$ I8 A/ R( K i+ ?2 @; K6 T
8 \# x4 k9 ?" Y5 s% q; D6 e} $ U" r4 m' H! n. u2 b& z, V
' a* F( `6 D0 [; F
|