52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:: `: O; C! t& T9 t5 r* C( x
2 k% F. Z/ H; t
7 p3 ?2 l+ ^+ h8 s" R
# CORS header support
1 {" l8 J( u5 t/ B" o+ T#
( ], ^0 Q9 I/ U: R3 e2 F2 `3 `# One way to use this is by placing it into a file called "cors_support"
4 w  G5 G0 x' {9 l# R1 x) E* Z# under your Nginx configuration directory and placing the following
3 o6 R# \9 M' S0 K$ y9 M3 M4 m# statement inside your **location** block(s):
/ b. b, Y$ ^8 O$ c#6 v& m0 U7 E/ ]2 q% W- B; a9 j
#   include cors_support;: Y! x. H' @# y9 o/ L  o
#
: u- ?9 O6 `" [3 k4 b% v- Z* |# _# As of Nginx 1.7.5, add_header supports an "always" parameter which$ T# l' o# Z8 |0 S% k: K
# allows CORS to work if the backend returns 4xx or 5xx status code.+ d4 t8 @0 @+ x& M5 x
#
' S. L! R  B  E$ z! e# For more information on CORS, please see: http://enable-cors.org/
/ \% J" O, ?- }( B# Forked from this Gist: https://gist.github.com/michiel/10646401 X; Z9 l8 i, r% U" d1 i' s% M
#3 W% @* S! h" [6 o" }% J. M
$ ^- m  C$ E) G3 F$ j' V$ p
set $cors '';
# o$ B  g. ]1 [: X" `if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {
% `& d/ C/ }0 H' Y7 i( |! v0 @' n        set $cors 'true';
) O" E, ]8 u: Q8 n}0 U4 B7 ^9 q3 c& o" F/ Z
1 F; J3 K6 }6 ^1 z
if ($cors = 'true') {
1 v* g5 i1 V1 \. N- H% v+ J9 H        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
. A2 N# v( u' W# o        add_header 'Access-Control-Allow-Credentials' 'true' always;
9 V0 a1 k! x3 ?- O0 V' ]) X" p. F' }        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;; a0 V/ H# s% P1 J$ ]7 i
        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;( l6 A6 y( i! u; F# T, I
        # required to be able to read Authorization header in frontend
- w% n* m5 v2 f        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;! \3 N& ?6 @+ H0 p: U$ ]/ a
}
) {( H' `% N- P$ W. c" N- Q
2 z6 j8 e6 z5 u# u% w9 bif ($request_method = 'OPTIONS') {
4 F: `1 N. `) X7 p  p1 ^' ]        # Tell client that this pre-flight info is valid for 20 days' ?( l$ F2 c0 y3 E. ]
        add_header 'Access-Control-Max-Age' 1728000;6 |% K; T2 V/ a  Q5 {
        add_header 'Content-Type' 'text/plain charset=UTF-8';
5 A- `; e  W  Z9 N        add_header 'Content-Length' 0;( w8 ~$ T1 q1 r2 Z( \
        return 204;
6 {" v  o, O6 ?. \4 R}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:
( j4 D* v7 y9 q) w- r1 g+ V1 q
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;4 B) W6 m- Y2 u0 g, g
}
8 Z  v- o' u% f- n8 j* O3 _set $origin $http_origin;" \; |4 ~  r  P
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {$ O' i1 R1 X! p4 a. x8 Y6 n1 g
     set $origin 'https://default.yourdom.zone';+ {+ v3 z# o. p8 S8 m1 b
}7 W' w( F4 @$ o/ Q  l8 p; N0 V
if ($request_method = 'OPTIONS') {
; N7 R) A' o9 v5 J" k     add_header 'Access-Control-Allow-Origin' "$origin" always;
$ s0 _3 r, u& \8 h; c     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
4 E2 P: r0 X; N$ h     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
- l8 t: X3 l7 N2 _6 P8 v" b8 S  Q     add_header 'Access-Control-Allow-Credentials' 'true' always;& _$ v# I5 q* r' |1 k3 U
     add_header Access-Control-Max-Age 1728000;   #20 days   
( @" G! X* f% n0 f7 q; i$ t     add_header Content-Type 'text/plain charset=UTF-8';
3 R" a* N7 U- y- B" ]. c' ?     add_header Content-Length 0;
, r( q0 L4 e% w2 o     return 204;
) \$ z$ g2 L) G}) a( f. h& Y# q
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
. L4 m2 l! Z7 n& A% y     add_header Access-Control-Allow-Origin "$origin" always;/ z2 D6 Z$ \* q7 i( }
     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;& Z+ R" t) ^8 O; r4 M4 K! i
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
* ]) `, I1 z& y- r6 e. s, V     add_header Access-Control-Allow-Credentials true always;* L# J2 E: Z9 m# ]/ w  ?
}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
7 d. V, \1 x. f* k  X3 o. S  G0 Y#7 P5 c7 z1 U% r. F2 }& J7 h4 V
# Slightly tighter CORS config for nginx. u4 Z6 w% w. i5 N9 q
#
/ C" _' {" L8 Y4 m! Z) Q+ A0 i# A modification of https://gist.github.com/1064640/ to include a white-list of URLs4 s6 ]5 k) r0 d! _; r  \8 ~
#
1 o6 k2 I8 ]9 k# Despite the W3C guidance suggesting that a list of origins can be passed as part of% k' A5 J5 }1 x2 @
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
4 G% r, ~# U2 `( k) ~5 L# don't seem to play nicely with this.2 i) I, a% u5 j4 ]; k
## J- b3 q  q* I3 D, @
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting% L! g% C1 ^+ g* Z3 g3 x
# method to control access instead.
; g) e& y; ~; ?& ~9 \#* l& W  r) O' G) \& K
# NB: This relies on the use of the 'Origin' HTTP Header.& Z3 q6 ~0 \1 n# i+ A* I4 ]

8 R/ K' o; E) G: m# mlocation / {1 M$ t) x' Z7 U" p

9 e1 X- u3 D/ L. B6 Q& m3 t& [% R    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {" u; R+ v7 f* c
        set $cors "true";
/ t8 j. W% D$ {& I/ E. [# B    }7 x9 R, c0 Q7 p7 c: }& O1 a% R& Z

. D; N9 n; G, N' d% k- \( \    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
+ l  G+ Y' U6 f2 S& }  L/ m    # Determine the HTTP request method used3 m; K& k" a! C! T! [. k" n* ]( d
    if ($request_method = 'OPTIONS') {) y7 |1 e$ {! b- F' X, k8 J* c
        set $cors "${cors}options";  q6 ^4 Q8 l3 x  L$ z- A- A! U5 Q
    }
& u( {6 \! d. n2 w3 ^% X6 U    if ($request_method = 'GET') {
+ c+ e" u; }! P        set $cors "${cors}get";8 o" ]# P! G( C) Y) Q% Q3 k- k1 T" a$ [0 j
    }
1 [' l' r) z3 \$ g$ a, ]# U    if ($request_method = 'POST') {* Z3 k) r# F( b, ^
        set $cors "${cors}post";/ ]/ K" G* x& p7 @# K  C* x
    }$ o% |* G: U9 d0 m
! G3 _4 m8 v6 w' {
    if ($cors = "true") {
. H5 Z9 e) d) B; p& Y  E        # Catch all incase there's a request method we're not dealing with properly- X6 s, I2 _. I- @6 M  j
        add_header 'Access-Control-Allow-Origin' "$http_origin";
3 s/ Z. X% s$ U: k0 n/ x    }$ S4 K$ g0 d0 X! l8 I# ~3 e
. @8 V  X+ T4 b4 p+ u" Y9 v3 k
    if ($cors = "trueget") {% I0 b& f8 x/ G  ^
        add_header 'Access-Control-Allow-Origin' "$http_origin";
9 C2 ]% W( a7 X9 u        add_header 'Access-Control-Allow-Credentials' 'true';
$ z0 X& e/ v$ }/ R        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
, ^% M. x/ `7 I1 S4 L" d        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';4 e9 k0 O# _, `. D
    }, H6 Y& B- c+ k

0 f0 {3 C8 m6 S% b, T6 H' a8 B6 C    if ($cors = "trueoptions") {
5 R: s6 T6 x8 D& {. E, K% `, ^2 U        add_header 'Access-Control-Allow-Origin' "$http_origin";
" j0 X- L( l, A1 a7 a
. h8 k$ Y4 M) d2 d! s4 o2 R        #, S) G/ c( [; L) M6 T" B# T
        # Om nom nom cookies
3 ^0 G% q0 k* j- z( N% o, ]        #6 \- d( r5 {! t9 ^
        add_header 'Access-Control-Allow-Credentials' 'true';" ?5 N1 V' H. h) ?, V$ t. Q
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';8 `; j4 @8 Y" ~7 a* D
* h" L5 L: v; b% J; h
        #% }8 p# H" o5 K% e
        # Custom headers and headers various browsers *should* be OK with but aren't$ R; U& i& v' I; g. T+ D" ^. Z/ v
        #6 |& C$ O2 H( \  S9 ~5 ?/ B
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
" p: y1 Y0 }: J' ~, ~# H1 p* |1 S6 C& M0 e' F! L9 J
        #1 W. R: Q, A# \  x8 Q. O* l( p3 T
        # Tell client that this pre-flight info is valid for 20 days
* W# A2 G* P8 V2 _% p, C$ p        #0 Y8 Z0 }  o* v! r; l3 |* n
        add_header 'Access-Control-Max-Age' 1728000;
2 F2 p( J1 E- K& ~5 O7 t        add_header 'Content-Type' 'text/plain charset=UTF-8';( S( _  q: E9 J( ?+ g
        add_header 'Content-Length' 0;  @" G: B0 H% q$ f- P2 f' x
        return 204;
+ N$ M4 ?; |% q4 f    }
9 X+ Y, w0 c7 D9 ^) C) C( W( v  w) ^
    if ($cors = "truepost") {
4 T8 I" r1 D# Q- ^        add_header 'Access-Control-Allow-Origin' "$http_origin";7 J% p4 A8 [% y
        add_header 'Access-Control-Allow-Credentials' 'true';
  c* U; G6 U% K, _) l. R/ a# {% w6 e3 x        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';" H5 W$ s8 |7 E7 V5 M5 B& [- Z
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
0 N+ C& a" a$ F2 {3 x    }
) ~& S* w8 T' Y5 U' J& B& d3 u  {) V% e" t
}
5 @9 U5 l1 `* Y5 D4 \/ A

1 J: X1 Z7 J8 [0 W& S2 }+ v

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-1 16:44 , Processed in 0.077257 second(s), 15 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵