52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |甇摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:
$ K# o- K! H* ^5 L0 @) v
8 W0 n2 H6 x5 F+ p5 |3 G

- V& p3 d4 d6 a- s5 R; \0 H* e# o, i3 A
# CORS header support& @8 `* m" b# L; ]- Y$ O$ w: ~
#8 x& f7 T5 e4 j) l5 u
# One way to use this is by placing it into a file called "cors_support"
( U. A4 h$ y  Z# U; I& a# {8 m# under your Nginx configuration directory and placing the following
( q. Y; Z6 x7 i; P4 K' o8 q/ U# statement inside your **location** block(s):1 Q8 x; I' ?+ k# {2 W& m
#' j8 I2 Y3 g0 y2 W9 P
#   include cors_support;& \: ^- S- o0 @# t
#
7 K; C* I, N6 B* _# As of Nginx 1.7.5, add_header supports an "always" parameter which
0 ~! t" V3 {" ]) s5 b# allows CORS to work if the backend returns 4xx or 5xx status code.1 N7 C7 J; t, u2 {5 x3 G2 e
#2 ]5 L; Y4 Q# W
# For more information on CORS, please see: http://enable-cors.org/$ x4 J) o% w! e1 L1 G5 B* ^( h. [4 @
# Forked from this Gist: https://gist.github.com/michiel/1064640
+ A2 V2 ~( i" A#; l( U: F% u, M5 ^; V" C5 v  N
8 Q1 [* _) l# c2 T2 P
set $cors '';
+ Z: L9 w% E& `# n+ B4 iif ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {
2 U: R1 ?. S- p! B5 h        set $cors 'true';
; M0 \; {4 U2 L& z8 _6 g" C: D}( s: l! J  `4 w

  o  k/ n: G9 n. c4 O, O- tif ($cors = 'true') {
0 ~5 n% l# P* e- l        add_header 'Access-Control-Allow-Origin' "$http_origin" always;: @8 {* V' p& \# J( n! j
        add_header 'Access-Control-Allow-Credentials' 'true' always;& M$ g, [& u" c# j% @0 q
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
: z: o$ o" |: F6 A) n& o/ @        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;
9 X) o' W1 e% O- n4 n1 A        # required to be able to read Authorization header in frontend  _9 O( X2 ]( a" l* ]  k% ?$ @0 L
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
7 D9 q% C2 g$ W/ ?( j}
: Z/ }1 C# d! N/ e6 _% V7 \  L
) b2 O% I3 L/ c& h+ ^2 Eif ($request_method = 'OPTIONS') {2 c- _: c9 Q+ T0 K1 p3 Y% ^
        # Tell client that this pre-flight info is valid for 20 days" _: o( F! l6 b7 Z
        add_header 'Access-Control-Max-Age' 1728000;
! t. z6 p) \) J( x3 d) A- C        add_header 'Content-Type' 'text/plain charset=UTF-8';
# n) t$ Y" |- K, I+ R( z' e' E        add_header 'Content-Length' 0;4 W. E' z  }+ @$ B6 A: T9 G( D
        return 204;" ^8 }7 V( J( U2 q* d- f
}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

2 a0 ?" b- y/ b; H) T
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;/ h& v  F4 U6 `+ i
}
+ |" H  Q- K" y) I. K8 s7 Z2 e& Jset $origin $http_origin;
2 k1 w6 |4 Y% _& Hif ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {, f  [2 y3 D3 t3 X- n9 K0 ?! J
     set $origin 'https://default.yourdom.zone';
' k# o# z* W3 s}" V, O. `$ F1 K) w
if ($request_method = 'OPTIONS') {* v) {! G' H" w
     add_header 'Access-Control-Allow-Origin' "$origin" always;3 R* T) O1 ^; j2 `% d
     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
3 F0 v5 a& D% ]     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
0 u1 d/ t7 H3 O" S     add_header 'Access-Control-Allow-Credentials' 'true' always;4 q# C! ?; ]1 t- z# Y- V
     add_header Access-Control-Max-Age 1728000;   #20 days   
1 T5 t5 Y; s( J( v" P8 |" F     add_header Content-Type 'text/plain charset=UTF-8';4 v( G* M: N+ b
     add_header Content-Length 0;
2 S" I- X1 O. @     return 204;
, y# E! [( F- `7 M4 V}7 N+ d; o' C$ ~
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {4 {2 K# Y' y6 v: h" v' p; m
     add_header Access-Control-Allow-Origin "$origin" always;
* F/ X& A, @' _3 k2 Q     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;- ?( U4 h; V3 C! O: l
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;; n8 k: F3 o6 @% s9 b' w
     add_header Access-Control-Allow-Credentials true always;5 W5 R5 J7 E) m1 o& B0 F1 O5 I
}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
3 b3 }7 p0 b, ~% A) Y#
$ |, [: B7 ]4 f- ~/ y# Slightly tighter CORS config for nginx
7 }, l0 Z# I3 N  [$ |#
$ B# w- t0 a) `0 q# A modification of https://gist.github.com/1064640/ to include a white-list of URLs' g6 P4 I5 @3 s0 L% \9 Y2 T& Q
#  K' j$ V  t& F2 M/ o9 N$ ^2 c
# Despite the W3C guidance suggesting that a list of origins can be passed as part of2 w3 }! T/ D4 R4 ~
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
% I/ f# I$ u$ u# don't seem to play nicely with this.
8 t$ q3 M1 D3 U4 k1 x; v#  V' ^- m0 H; j6 i% U
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting8 c( B, J2 g, h$ g& r- T1 H4 Z
# method to control access instead.
' \4 Q) M  B1 J#
8 x9 M; x4 G7 i" A6 e  O4 q# NB: This relies on the use of the 'Origin' HTTP Header.- S. a8 |1 X6 {( Y% ?. \6 R' s) M6 Y
. p+ O& A5 d/ ~$ W' U6 f
location / {
( G$ z/ r7 e$ B# n8 h3 p
, \; H' d2 S$ X% ~3 H    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {& Z4 W" {3 H1 b! S
        set $cors "true";
7 D$ J$ J1 `; e* r- Z& q    }- D7 h3 ?  S5 ]

% C" m$ E' V% r& {    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
4 y. |, s6 k7 H6 o( T    # Determine the HTTP request method used! I8 r2 s% ^, V) h
    if ($request_method = 'OPTIONS') {
6 d6 z( `/ |7 G4 u# L; m        set $cors "${cors}options";4 a8 \! w6 J# q% o" c: Q
    }
) ~9 s( r) y2 y: \    if ($request_method = 'GET') {3 f0 L( I- N7 \8 S: i! Y$ v& g& z- z
        set $cors "${cors}get";
/ }1 L' S: D7 E; k6 Y+ V/ F9 J+ i: t    }8 l0 o' z  F; G. v
    if ($request_method = 'POST') {2 U; _2 G0 u" S" O- j( r! I" `( S
        set $cors "${cors}post";+ m; Q) b/ Y) L, L2 M. V& h( ^
    }
* E) |# O' A- p, V) N- m. i( P4 F6 n3 C  l- r3 L* L
    if ($cors = "true") {
  E0 q& s2 w9 x8 @, I1 k        # Catch all incase there's a request method we're not dealing with properly
4 j7 }5 i. A- d2 i5 P: Q        add_header 'Access-Control-Allow-Origin' "$http_origin";0 x2 J  K# V3 ^5 I3 G
    }: l2 y# s7 }, ^  j% F- q, u0 W+ t. T
& G! h8 {4 a) F6 S* x( [- v
    if ($cors = "trueget") {
/ a# G0 Y4 s1 U! j( |+ y/ ?4 k        add_header 'Access-Control-Allow-Origin' "$http_origin";
) C1 k% b" D" l9 X        add_header 'Access-Control-Allow-Credentials' 'true';
% |2 F+ j, c! T/ {; c        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
9 T$ c8 m! V% r1 B        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';- c* U7 X' t3 z" ~
    }' M6 Z- X5 S. p, x3 {4 v
% ]9 r8 Z6 k0 p9 b, }3 G
    if ($cors = "trueoptions") {( c5 e8 f* D8 i( f* {1 B7 ^: n
        add_header 'Access-Control-Allow-Origin' "$http_origin";
* v' K2 i8 c/ t# v, z, v1 V# u# k4 X* d# T
        #, P9 @! @" c. U- \' C
        # Om nom nom cookies
9 J% [: L5 t' z! J9 w9 P        #6 J* T& a. R, G# L7 i8 A: k
        add_header 'Access-Control-Allow-Credentials' 'true';9 K  M; s2 Q& v3 A0 \( j5 D/ L
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
1 q/ U  d* q$ x, ?
# A+ a. `+ _/ m        #
/ W! w( v+ ^) \0 y9 l' g$ c        # Custom headers and headers various browsers *should* be OK with but aren't" l$ B( p  _: \$ ]' }* b
        #
, y+ }6 V" \/ T# X        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
; [9 n* I3 l" ^0 d) `  Q9 r1 }+ O5 y! T
        #6 t5 Y5 q4 |" W2 ^* X: H+ N/ v7 F( v
        # Tell client that this pre-flight info is valid for 20 days
7 g6 P# D# w- O4 \4 R/ t: {        #/ @8 r8 l/ ^. D8 Q+ y' W7 [7 K
        add_header 'Access-Control-Max-Age' 1728000;
9 N' C4 Q1 v5 c2 C; [9 l        add_header 'Content-Type' 'text/plain charset=UTF-8';* j$ V1 l; ~& q! Z( n8 b
        add_header 'Content-Length' 0;$ R/ k" Q+ n+ o0 `$ K2 y7 m
        return 204;
# q  n& N5 ?6 t: g# }6 i8 s8 x    }
, Q1 R' W& A9 F4 y
8 ^5 S. n! m" D  ]7 }    if ($cors = "truepost") {
" G" A* x( _0 \+ d  o2 |* K/ C        add_header 'Access-Control-Allow-Origin' "$http_origin";
, G+ ~9 O& _1 l9 s9 C0 u        add_header 'Access-Control-Allow-Credentials' 'true';$ W: M3 Z) W2 E) e7 p% c9 o2 j+ ~
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
4 H5 M5 g6 z$ Q5 e; l( f0 X9 K        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';9 v* w1 R2 `3 o! u/ T9 {# N8 R8 p
    }
' t& x9 J8 m* v6 D9 E' z, Q8 V9 J
}
" _7 r+ x, |7 V/ p, e
$ G/ f0 A; G( ~8 F: w2 F  K

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-2-8 04:49 , Processed in 0.065731 second(s), 16 queries .

蝯∠.撱

52avtv@gmail.com | QQ:2405733034     since 2015-01

鋆貉憒 敹恍敺 餈銵