52AV璈A|52AV.ONE

 曉撖蝣
 蝡唾酉
敹急瑕
  • av隢憯BBS
  • 璈A
  • 芣瑟憭瘚
  • 鞎澆
  • 52av鋆貉摰
  • 銝剜-銝剖銝餅
     
亦: 2907|敺: 0
銝銝銝駁 銝銝銝駁

[nginx|php-fpm] Nginx銋CORS嚗Cross-Origin Resource Sharing嚗頝其皞鞈皞曹澈嚗靘撖衣暸脫迫憭慦擃鞈皞

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:
1 A; N$ U' @* a. R' `; z/ U! M
6 f" b7 D+ a/ R& N) {- O1 r
* x7 k  i3 N% }4 e7 Z
# CORS header support5 H* m; x& y  `  X7 v, `
#; S( Z4 U+ P7 S$ X5 d7 E* G) v5 E
# One way to use this is by placing it into a file called "cors_support"8 a# e/ g3 r. P1 Y! Q' w% K7 Y
# under your Nginx configuration directory and placing the following7 `( m, q' P4 T2 v3 \
# statement inside your **location** block(s):
4 h) L8 {  U$ f: m#
/ X* @3 W2 t( L' Z% R#   include cors_support;) V" o) h  T0 [: v8 e+ s* j% a
#
  Y- q; [# g9 t3 j# As of Nginx 1.7.5, add_header supports an "always" parameter which
4 Y4 |" x" p7 ]0 R# allows CORS to work if the backend returns 4xx or 5xx status code.
& h; C( o+ z/ i3 k#" K2 R2 }; ?$ B: e) c' O
# For more information on CORS, please see: http://enable-cors.org/9 i" B# K8 ]4 h5 s
# Forked from this Gist: https://gist.github.com/michiel/10646409 ^9 x5 r% T- E
#* R* `9 u9 v. T$ q# Y/ K( Q

9 e, Q  C  F. ?# D1 ]set $cors '';, p. a! ^. H1 @- {! E; D
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {$ c' q5 r1 g1 _. Q0 e
        set $cors 'true';. B6 I# G( ]6 b. H% I
}
: [, M1 V5 v; V8 Y' u- H/ t! {  i  }: M0 r$ Q/ ?) c" Z3 J" e
if ($cors = 'true') {$ R8 H  t! U( o4 Q
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;% r4 v4 r1 I8 G0 d) O/ s" T
        add_header 'Access-Control-Allow-Credentials' 'true' always;
9 p8 A2 l! {* k$ c6 B$ H        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;  e( {# F8 S$ P* ]" P/ Q1 O% Z* }
        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;1 `  k% z. G. U" k3 ^8 h2 U4 @
        # required to be able to read Authorization header in frontend
1 P8 {) R0 p8 D/ w8 ?6 D8 z        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
: Z* ?2 y: M4 l$ m! L( a}
7 U0 d+ @6 k  Z# ]
$ t# R/ e5 K7 cif ($request_method = 'OPTIONS') {( S6 Y/ F% C' e" e7 C0 t+ G
        # Tell client that this pre-flight info is valid for 20 days
- F7 p( K; V% A$ B1 }$ K        add_header 'Access-Control-Max-Age' 1728000;
, p. ]* C  W, {7 R: f' N. A        add_header 'Content-Type' 'text/plain charset=UTF-8';
# h% ~7 c3 Z3 o  }% ~3 M) A        add_header 'Content-Length' 0;
4 {& O5 y9 n' q3 X; Q9 ^        return 204;+ y1 x5 E0 @9 |' m; z8 [! d# v) a3 n
}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

: j6 m6 Q1 r" q2 m; a
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;1 Q3 }3 a+ n, J7 {5 C3 V+ t+ O
}- J& C8 l0 D9 a% @6 _5 N
set $origin $http_origin;' d' \, L6 ^6 v' _$ c8 x1 f! K
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {; C& `: q& c6 v
     set $origin 'https://default.yourdom.zone';
0 x6 f, F& ]7 K" G6 \}$ t& M* q8 a) n/ X7 F( S
if ($request_method = 'OPTIONS') {
% f) y' K" _1 D     add_header 'Access-Control-Allow-Origin' "$origin" always;
2 _  E# [* s4 P     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
  o) b4 i% U1 ^9 k, b- m     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
, G2 F) e, d8 H( v6 }) i  P6 p' ]     add_header 'Access-Control-Allow-Credentials' 'true' always;
4 `8 w- F3 `3 {  y# _5 @     add_header Access-Control-Max-Age 1728000;   #20 days   ; g$ D9 }# v; H# g( y, q' b: R  J
     add_header Content-Type 'text/plain charset=UTF-8';7 o4 F# k' ~8 Z5 q- f
     add_header Content-Length 0;
+ e+ W5 c) r( g' m: j     return 204;: Q" I3 F5 Z; a  K3 Q1 I" @
}: K+ T; n6 W! ^
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
! x( s( @' |8 B5 Y; e4 j/ a- h     add_header Access-Control-Allow-Origin "$origin" always;1 O3 B% s7 Y4 G
     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;" ~5 Y+ n7 R; L+ s; A+ o1 R. C+ [
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;3 ?  G9 N5 S" h$ Z' @6 g
     add_header Access-Control-Allow-Credentials true always;8 H; c2 V9 N4 \
}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
! X7 U7 ^, N( n6 \( z& r, w; Z/ h#
* H- [0 U+ P, R% q8 C# Slightly tighter CORS config for nginx' J4 q# ^- Z, M+ E5 O
#, C0 g0 h9 A) F
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs0 m& _: k& l+ l* D7 A/ ~1 T% E; t
#: t. I* j( Z: D# K
# Despite the W3C guidance suggesting that a list of origins can be passed as part of1 T0 |  ]3 u5 H; q2 H, t' c
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)3 w% I7 d" }: h- B. n! l7 r
# don't seem to play nicely with this." n2 t) F0 k7 ^+ V& r8 a
#8 n6 Q$ p8 C- P8 I
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting
4 D2 |1 q$ y4 `# method to control access instead.6 J: z9 W# y- c& j
#3 i5 E- e) R, V) w! Q8 s
# NB: This relies on the use of the 'Origin' HTTP Header." }8 N+ m5 m1 z
& v( M- q" F  t! }) @
location / {1 X& i& @) ~  B5 l
4 W4 \" [/ T8 x
    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {# i4 N  M" j& r7 L5 e
        set $cors "true";( b; }* Y! ]- T' B. ]; t
    }  L2 f- Y3 N- K1 F% q+ p. ?& r$ y

" ~' }; v4 z! o' L! E    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
, j4 f( |1 i1 g  p; O    # Determine the HTTP request method used
4 h% a9 x3 `) @8 R    if ($request_method = 'OPTIONS') {
# n' F3 d0 y; O% i0 E" K        set $cors "${cors}options";9 {9 J& F$ \. ^, L+ `7 Y
    }. z7 T7 {0 y" F+ c; f
    if ($request_method = 'GET') {; h5 K( Q- s! E" d" ^" k
        set $cors "${cors}get";
5 m3 Z5 [$ B& Y5 c- e  b    }" Z$ d5 a; g) I0 q' G
    if ($request_method = 'POST') {, A; q0 d) {1 v. F
        set $cors "${cors}post";4 F0 _4 M$ ^) O# ^, k# ^( M
    }
& o' r* G. a" b+ t% q# i
% a: m+ K& j3 W8 N: ]0 w; n) u! Z' }    if ($cors = "true") {
! V( R9 |# i8 `( o" M        # Catch all incase there's a request method we're not dealing with properly( h( Q8 J0 r3 w& P9 f1 k* {
        add_header 'Access-Control-Allow-Origin' "$http_origin";5 m4 l; w1 E: q7 _' ?& ?; f' Y
    }2 H2 u. ~# Z) z  ^

! ^$ c4 A4 F6 V6 G( ~2 d    if ($cors = "trueget") {, F3 d" z5 i# P7 y2 g8 N- L; |$ P( a
        add_header 'Access-Control-Allow-Origin' "$http_origin";
7 t4 T* `, Q; V% I        add_header 'Access-Control-Allow-Credentials' 'true';
7 {" u4 {+ L, A/ h2 E  f        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
" }: `( C+ F/ R        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';6 b6 d9 H! C& K$ |
    }
! D: o" [" @1 {5 ~( x
6 g' P) M; ^$ l: Y4 b/ M! Q8 c6 r    if ($cors = "trueoptions") {, r; r$ Q/ l" g& o; u% X
        add_header 'Access-Control-Allow-Origin' "$http_origin";3 @3 A; R4 w; R/ B: |! z
5 F/ L: ]) z- G+ [5 Q! A
        #* {* i$ ?- L5 L5 D( w1 u# Z
        # Om nom nom cookies( e% p! p  N/ Z# d0 ?7 f4 z
        #/ p( l- |2 A. Q* E  X
        add_header 'Access-Control-Allow-Credentials' 'true';2 \0 r" y2 T6 a& K
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  L4 G2 _* i& k( I, X8 J# A+ U% D4 \+ o$ s- U+ x$ |
        #
5 O. u5 b0 ~& Y4 s- `7 w% R        # Custom headers and headers various browsers *should* be OK with but aren't! ]% z; b3 {% S6 j1 u; N$ j0 R0 z
        #
) W7 P4 n1 v& x; i- K6 a$ @' [        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';2 j2 b# D( R' r- d" H: I5 f5 y
1 O6 Q1 e7 s" w- w! P' g
        #6 U; k% c4 M# p. w- c+ r
        # Tell client that this pre-flight info is valid for 20 days' U, k% B' A: k0 R& }. y
        #5 l# H! g# _  A: k4 O
        add_header 'Access-Control-Max-Age' 1728000;
2 A( N; o$ p- E6 ^: Q2 M# g        add_header 'Content-Type' 'text/plain charset=UTF-8';
, t8 A4 C, j! n+ R        add_header 'Content-Length' 0;
/ V7 n5 _" N) r        return 204;- j" ~/ }2 w5 L% \* X
    }
+ h: ]3 o; o/ \9 f! w9 s
- \1 c+ a6 j" g- ^    if ($cors = "truepost") {8 T2 w& X: o! \# G
        add_header 'Access-Control-Allow-Origin' "$http_origin";3 q' B$ m2 _4 e
        add_header 'Access-Control-Allow-Credentials' 'true';
& ^! x6 R5 M9 E3 y8 Y% p& M7 L        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
0 o4 a  [: j0 w) q, v3 ]        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
7 v! I0 [, C) R' V: Z* `    }
- D9 Y% i# Z! F7 l, c1 c0 l3 Z1 u$ H0 O7 [3 i
}
, N0 x7 |- A6 I9 h2 t
1 |, q+ O5 Z9 h0 p7 i8 n# O7 v6 `6 }

雿輻券

祉蝛閬

BT蝳

砍憛批捆靘餉衣雯頝臬批捆蝝颲行粹嗥蝬脩嚗摰撟湔遛嚗嚗甇脖誑銝嗅啣摰嗆摰撟湧翩鈭箏ㄚ孵舫脣伐銝憿亙祉璇甈橘芣遛18甇 雓蝯脣亦閬賬粹脩芣遛18甇脖芣撟渡雯閬賜雯頝臭嗥批捆鞈閮嚗撱箄降典舫脰蝬脰楝批捆蝝蝯蝜ICRA蝝摰鋆閮剖 (粹蝯行霅 祉蝬脣銝蝝瘛函隢憯啣嚗祉閮剜蝞∠)

QQ|撠暺撅||52AV璈A

GMT+8, 2024-5-3 14:27 , Processed in 0.063348 second(s), 15 queries .

蝯∠.撱

[email protected] | QQ:2405733034     since 2015-01

鋆貉憒 敹恍敺 餈銵