52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:
+ G$ h3 |3 J* {/ Z2 W5 @5 T4 l" b0 |, g, C

: q3 f9 g) i0 M- Y  _- k
# CORS header support- f- w& J8 Q0 e" ?: Q9 t* t
#
$ N4 i: C7 |/ g9 {, P# One way to use this is by placing it into a file called "cors_support": w) p2 Y/ `: q" B
# under your Nginx configuration directory and placing the following+ t$ {* i8 R4 c4 \
# statement inside your **location** block(s):8 u/ |: u0 |! I1 v6 C. d* H5 h5 _
#* \  [1 H3 ~! D2 k
#   include cors_support;
: F1 w1 O& `" O4 C* F: l' o#
0 P* T; o; ]: R# As of Nginx 1.7.5, add_header supports an "always" parameter which' a* Z% i0 M* o7 z2 f
# allows CORS to work if the backend returns 4xx or 5xx status code.; j6 r2 g+ M# L& t9 V
#
5 S; n# H' e" R4 S. e# For more information on CORS, please see: http://enable-cors.org/. o1 t2 [- W9 R/ w
# Forked from this Gist: https://gist.github.com/michiel/1064640
- G9 V" C. W# {, _#
- Z' ?$ O! ]' i0 \2 k$ Y5 [1 _' M3 h8 f4 |. E( b. b& B2 ], l
set $cors '';, x6 ~  V! c( U$ U& g
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {+ i& m2 Y7 A% U* |6 _
        set $cors 'true';
7 p* z$ ?4 d4 P! c( r- J}
* U3 z7 Q6 h/ g) c1 T: B
5 J( p$ i  J8 B$ x8 lif ($cors = 'true') {! ?7 F) o( G6 Q! s( t
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
3 I7 [& o6 k, q- v        add_header 'Access-Control-Allow-Credentials' 'true' always;
" ?9 |) h" _7 F3 j- Y" U+ }        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;% \. j5 q2 z4 j  C
        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;% a- {- F" m% p1 ], S
        # required to be able to read Authorization header in frontend
/ P, t, D( X7 z1 \, u8 s$ Z4 J7 Q        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;' P8 c( H, n4 _  c" K: d4 A
}
! o% ~8 w  v8 P: h* N- p7 \% @1 W- E3 W: s4 k
if ($request_method = 'OPTIONS') {
3 U/ G, T8 n( t2 S( Q0 |6 v        # Tell client that this pre-flight info is valid for 20 days
/ L8 B" t; H* s4 O2 t. j  g4 B        add_header 'Access-Control-Max-Age' 1728000;
& o6 g0 q! i: Z8 w% y& ^- \        add_header 'Content-Type' 'text/plain charset=UTF-8';
4 P1 W9 h! A$ O! s* U1 w2 y        add_header 'Content-Length' 0;
, M5 @0 x% w1 M- r+ d: F  t6 A        return 204;
6 U$ F5 S- f6 g8 q}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

0 z2 [, W9 u6 v4 V' t! S
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;
; Y  D! I& ~9 Y3 e* J}* |3 e7 u  Z+ G9 A3 x" c/ u
set $origin $http_origin;
& g. F5 n1 S+ n, Bif ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {) O2 g) S" {2 f8 }3 b# K
     set $origin 'https://default.yourdom.zone';( r6 R% K) h& w
}6 ~6 ^7 H; q( M  Z: [) F
if ($request_method = 'OPTIONS') {, J8 f* v% @2 H$ Z) i+ o: T
     add_header 'Access-Control-Allow-Origin' "$origin" always;1 I. n0 f! m$ J3 T6 U/ z
     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;4 h2 w& p) t6 D3 o
     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
/ V' D& v0 g0 P( h1 }. \     add_header 'Access-Control-Allow-Credentials' 'true' always;- w$ z5 ], B) Y% f" D
     add_header Access-Control-Max-Age 1728000;   #20 days   0 D0 @; M- T/ D7 G0 W3 h' L
     add_header Content-Type 'text/plain charset=UTF-8';
" v' Q, {( q8 @/ Q: n5 k; r  ]     add_header Content-Length 0;
9 C0 f$ l3 D% Z% H, |7 i     return 204;  ?- C0 n) [6 _! n- A/ G! |+ ]
}
& j: W/ \& v- W( j; Z. @5 Dif ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
1 i; b8 m+ e: Z$ L  M2 H1 U     add_header Access-Control-Allow-Origin "$origin" always;
- X5 L# p$ |. U     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;* i! n! g- g, d! ^8 c) A
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
$ _. Z7 s0 M5 G+ s6 C* I     add_header Access-Control-Allow-Credentials true always;
  q$ z5 m% q5 G9 V! ^}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/* e  o4 j3 `0 U0 E$ s6 l9 {
#- [6 L% b, n' U% j) t! U- m
# Slightly tighter CORS config for nginx
3 o3 H' z" Z& k* s#
5 j( n! }1 ]( T7 d2 w$ W# A modification of https://gist.github.com/1064640/ to include a white-list of URLs
) U2 w% s* Y) L; ]+ |#( W# c* w2 Y! S7 [( g9 P' a5 T
# Despite the W3C guidance suggesting that a list of origins can be passed as part of
* F5 J+ U+ d4 @9 W3 N( o& K+ q# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)1 y* o- H( A; _1 T
# don't seem to play nicely with this.
; M! \6 m- O- P5 y4 l' d#' ~4 o7 V3 r& L- y2 q
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting
  t, }4 x) i* u' z/ y$ j# method to control access instead.. ~$ z# u3 d/ m7 N6 ?
#
( [- z4 \1 Q) o0 T# NB: This relies on the use of the 'Origin' HTTP Header.
5 _( n& `" l; I; Z* z* y( Q0 j* d2 \6 x1 V6 ^
location / {, Y/ M$ v6 P3 J- a& y9 [/ J+ W

  j9 Z5 S6 q+ ~( M0 ]# i/ F, P    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {* Q: J$ f) q  F
        set $cors "true";
7 N3 s+ L  Q8 J9 T) @, p    }: b, a$ O) J& Q+ r- c1 `

- }3 a  g1 ]) c    # Nginx doesn't support nested If statements. This is where things get slightly nasty." B( a; Z+ A$ s0 d) |* z  f$ j0 Y/ h
    # Determine the HTTP request method used
- a8 h' x! o7 h    if ($request_method = 'OPTIONS') {/ b# @1 c4 _& b  j
        set $cors "${cors}options";
% u" p1 B% H. N) c0 W5 d    }
! H% c2 D) P3 \# T    if ($request_method = 'GET') {
& F+ M9 r# v& u        set $cors "${cors}get";/ `& f% F4 D% \- U$ x3 b* m1 p
    }
0 T& Y0 J0 N! p    if ($request_method = 'POST') {2 A4 U) A6 J2 W0 o
        set $cors "${cors}post";
% E9 g; j. H! t    }8 ]# x/ \7 J/ S" P  h: a* d: C; p" J

8 {( w' r* o6 x2 P9 O+ @8 {& [    if ($cors = "true") {2 W* z% f7 V0 q6 |# F6 h# H/ M
        # Catch all incase there's a request method we're not dealing with properly
7 ^0 w1 t( X# Z+ g0 m! O" d        add_header 'Access-Control-Allow-Origin' "$http_origin";
, {$ Q2 B2 t  N    }
: b, _! s1 F1 f# t' F4 I
& i7 o; k" \+ M7 s! |+ {- w    if ($cors = "trueget") {
4 h! D8 X2 m4 O2 y+ Y        add_header 'Access-Control-Allow-Origin' "$http_origin";9 {- y* f( J$ O" f4 c
        add_header 'Access-Control-Allow-Credentials' 'true';: X* x" I& f& }1 m) J0 h! Y
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
. Z7 ^5 q. i# J5 [  H        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
* g+ v3 Y7 c- `- `    }* d* |  a. [2 b- j

8 e: B, P: k, c6 Y, H" q    if ($cors = "trueoptions") {
/ s8 O$ l2 u5 `2 D! s* _        add_header 'Access-Control-Allow-Origin' "$http_origin";1 P. n/ S' w& ]! |3 t
7 P, B$ E  S, E: E2 A8 P* U8 y8 C5 X
        #
& L- x! W+ b/ ~7 s/ ~& J        # Om nom nom cookies
; Z3 n1 q0 W! E! A$ b( ^        #
3 @" m' J5 e1 k( W        add_header 'Access-Control-Allow-Credentials' 'true';! w% b0 r% R' b5 a5 h
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
& K8 ]$ G& m  u9 X
# ]3 r  v1 k/ n- o5 }4 K        #
  m" p8 [# R' M4 P9 O2 w$ P8 X. R+ h4 E        # Custom headers and headers various browsers *should* be OK with but aren't
" E7 H0 e4 E3 a( @- z# U$ S        #* Z5 {; @# _- }9 w
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
+ Q3 s: t1 D7 C( N0 O9 K) M2 Z2 ?7 A. f' i) B, E4 P4 s. x
        #
; i" G6 ~9 j$ C7 U        # Tell client that this pre-flight info is valid for 20 days& s9 G$ E" H1 i& J2 n- ]* V
        #
% Q4 p' W7 `5 h; I, w# C( G& V! u0 W        add_header 'Access-Control-Max-Age' 1728000;' h3 L7 y) V; {+ C
        add_header 'Content-Type' 'text/plain charset=UTF-8';
" r9 X. r# K( N- n" V        add_header 'Content-Length' 0;
' G  t1 I0 }; |1 a6 z2 q        return 204;
9 Q: p0 R! N  q+ l9 Q( T    }3 x- Z; S/ E; k/ m; {( E' ^

' w: B4 {( b1 x& A) O) R4 m) g    if ($cors = "truepost") {% ~# C) M+ v+ a) A$ N/ h8 e" D0 x
        add_header 'Access-Control-Allow-Origin' "$http_origin";
* D# G( \, ?& Z& g6 |# g        add_header 'Access-Control-Allow-Credentials' 'true';
! g# s$ _  Y4 g        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
# y! L% k: r5 [. K' ?" u$ ^/ }        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';6 A. N/ M& |5 e9 J5 [  r
    }
1 L- g5 G. _7 e& V+ m2 [6 ]2 G$ @# r: X, l2 |% \
}

$ V: V$ `8 `* s0 I0 C6 a8 \' Z! u$ t
7 k" T9 n. V2 B% P* p3 N

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-2-8 08:37 , Processed in 0.060075 second(s), 15 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵