52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:
. H4 L- n% Z) m; L. Q+ F/ Z
# i) m. Z5 R' s+ |# v
6 W+ I4 u  H! \- z5 W* G# j4 S9 Z
# CORS header support
- Y* r; T  h8 g) \% Z/ y9 L#" |4 c8 i- M! E7 m. ^0 i
# One way to use this is by placing it into a file called "cors_support"& j. y& S6 u9 r2 c( h7 Q( O0 G
# under your Nginx configuration directory and placing the following( E$ n6 u$ ?5 b' O/ X# O1 Y
# statement inside your **location** block(s):
7 a8 U$ n. R" V; `$ O$ R( `#% z( f5 i% |3 b- G
#   include cors_support;
: i5 v2 \3 t% K" v#
8 g* X7 A" J9 n$ D9 G1 l1 P/ B7 L- g# As of Nginx 1.7.5, add_header supports an "always" parameter which$ r0 O8 }2 {* h8 T3 S
# allows CORS to work if the backend returns 4xx or 5xx status code.
* B0 ~1 N% z: v8 [* \+ r#
; o4 ]$ g8 f! b7 j8 h+ @3 x" Z" t0 s# For more information on CORS, please see: http://enable-cors.org/
. s3 p7 B7 k; G% U9 o# Forked from this Gist: https://gist.github.com/michiel/1064640, d. O0 ?' Q/ {/ y3 B  V* b
#6 J6 t6 Q- b; C* o5 q
& T# `' p5 k6 {1 m. E- L7 {
set $cors '';
/ d$ ^" |; [1 o, s( b4 Kif ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {8 h# g0 C, v: O+ M- U/ A" r! B" i+ k9 @
        set $cors 'true';( \$ ?7 N( T1 d0 t- F6 w
}
  s- z( w' V; f
2 |3 B% A/ l! O, P, n- p/ n- F# \if ($cors = 'true') {# }0 \% e& B' n$ b0 t, ?( _8 x1 [
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;1 t, G5 A- q, [9 U) X! v
        add_header 'Access-Control-Allow-Credentials' 'true' always;( J. h8 z7 D6 r, N7 Q7 g; T. Q
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
0 H, K/ z' j& j        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;, M4 W" {8 u4 l) l, r
        # required to be able to read Authorization header in frontend( Y! `. l, I# A
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;% r, d% r$ i* b
}
) `7 Y5 |/ w5 H: x1 f6 O! m+ W- ^0 U
$ y1 D" U) q) |% Mif ($request_method = 'OPTIONS') {$ ~5 G; a6 u/ M0 ^
        # Tell client that this pre-flight info is valid for 20 days) b4 C: m7 I8 j, O
        add_header 'Access-Control-Max-Age' 1728000;5 ^4 S9 Z' O* G1 O0 R0 L/ E, F) e
        add_header 'Content-Type' 'text/plain charset=UTF-8';3 `7 J6 w; f7 C3 d' v  E
        add_header 'Content-Length' 0;1 O# {0 [/ ^$ O! K" ^
        return 204;
8 K) h* x" q  U, G& _}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

# Z( J, w! B# t9 H
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;
: v. V0 _2 f& W}  X% u6 i: z: m+ x8 z8 U+ C
set $origin $http_origin;2 C& l3 a; A4 V- _, w& O$ L' d
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {1 N$ H" Z% y5 r) D' C' ?
     set $origin 'https://default.yourdom.zone';
: z7 a6 ~) n( C, a- |9 }}; y( w( \, c) ]: s* S! i
if ($request_method = 'OPTIONS') {/ r0 Y. P1 c7 b, {7 h  I
     add_header 'Access-Control-Allow-Origin' "$origin" always;' x) `& c* _% l. F+ q) E5 s0 Z/ @
     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
: \* |; F$ R! P6 o) d* _! b     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;% Y2 D, Z' X6 f! W- c
     add_header 'Access-Control-Allow-Credentials' 'true' always;
6 g$ P$ ?% T% p" X     add_header Access-Control-Max-Age 1728000;   #20 days   + L# i! I! K1 \  F( u; J
     add_header Content-Type 'text/plain charset=UTF-8';' {4 Y! I# ?* U
     add_header Content-Length 0;4 g: j$ I5 n  Y  ~0 u$ e" _
     return 204;! P. `, {+ k! O4 z! y7 T! F
}
8 s" M& U5 h% G  v: t- _7 oif ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
, O7 ]$ C/ a2 |7 R6 g8 w     add_header Access-Control-Allow-Origin "$origin" always;
. h: C5 D0 J& ]' q4 a* v     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
, B' j) h5 Z: N( ~* k" K  i# b' I     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
4 V5 f2 p, L7 N8 P5 u. I     add_header Access-Control-Allow-Credentials true always;
4 W. b2 Y; J7 b  b" j' b}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
7 c& o2 }- k0 @' v+ Y* @#- a- W3 x5 L  ?& d' Q9 B* z$ n, I
# Slightly tighter CORS config for nginx# F9 o& x5 I  V" M- `6 h  ~
#* u; i5 `: Z5 P
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs
8 E9 [7 V  V" U* @#- K# z3 y& o+ [" }# s
# Despite the W3C guidance suggesting that a list of origins can be passed as part of+ N5 I. a" c5 K$ S( d; Q
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)3 ~, m6 H) ]* Y# S9 }
# don't seem to play nicely with this.+ I- V0 a; b& {5 k, x
#
5 Z1 [" X4 c: [4 i0 w# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting. z( E9 V) C: C& w* K' q
# method to control access instead.
% c1 A% d+ z0 O+ I: l#
" f7 P6 E4 D  v0 o2 r6 R# NB: This relies on the use of the 'Origin' HTTP Header.# b/ }- c% I8 X0 a; s

" p- H5 N6 z3 Qlocation / {
, R' i, n$ D2 Y# R& a1 L  j
3 G1 H; H, H" F, a    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {
9 c* V: v) b9 h        set $cors "true";6 E; g' W6 t7 s* K$ S
    }
' ?9 W3 _5 }5 I5 B! H+ P& F
8 b  X, O( _$ Q( Y+ W    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
  y% Q8 K* S+ V; b8 m# K    # Determine the HTTP request method used
1 P$ q& k5 u$ W+ ?( ~4 ]  r    if ($request_method = 'OPTIONS') {
4 s$ U2 x. m: v* L: m- y5 c        set $cors "${cors}options";
, H$ I6 [9 g" i# T" }    }: \$ n" A! x6 d6 B( j, E, F3 R
    if ($request_method = 'GET') {. [6 Y9 w/ k6 r. C: ^4 _: W" E4 {
        set $cors "${cors}get";- O! O5 a+ s1 g! r9 d# l
    }
  ]% d% x0 f# X8 P8 E* ~9 Y0 ^    if ($request_method = 'POST') {5 n. J- [$ ^; p9 c. \0 h* s, Z
        set $cors "${cors}post";' v3 V5 g& t- ~
    }
8 l* T3 O9 G6 s; I: [( `+ y# F: p( x9 n" p. ?/ E, Y: w$ P8 w& l7 X+ h# u
    if ($cors = "true") {2 O; _* [! N, V. R; P9 G1 \) G; E
        # Catch all incase there's a request method we're not dealing with properly
% g; q( T- Z' b. b: Z( _        add_header 'Access-Control-Allow-Origin' "$http_origin";
6 e( @  H& u9 s* r3 ~" c    }2 h% j0 a" [5 i. I1 _0 C

& R5 q. y' ~, _. I$ F- Z4 h    if ($cors = "trueget") {
2 }  y  @: t+ X2 ^4 M3 U        add_header 'Access-Control-Allow-Origin' "$http_origin";9 i0 H) a! r9 p: g9 m8 X* Y
        add_header 'Access-Control-Allow-Credentials' 'true';2 q2 }3 D: T' B7 q8 H: }
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
9 Z& B* S, n( Q5 p* {+ T2 m        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
2 L- z- t! [# g* T6 n% @    }- \: \% Y" p$ }) ^: [* g, j( b

' f, c7 l8 K; P! `5 K/ l    if ($cors = "trueoptions") {# M- \1 s  \& s2 I* I
        add_header 'Access-Control-Allow-Origin' "$http_origin";- v' C3 ~' K# u; m$ L) ]
) N! o  y) V9 U- a2 c/ e* s
        #& W, |- H0 g4 q3 Y( \, \/ o
        # Om nom nom cookies
+ V$ x1 r/ O. x9 U4 ]        ## Y  B1 f$ u( d
        add_header 'Access-Control-Allow-Credentials' 'true';: G- O8 C! B2 a1 n% h9 |# A, f
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
! ^. p. ~" n+ \. x/ v; E7 @$ P
( P- O% ^, H3 v+ P        #
2 k2 o# E3 _* t( e9 D        # Custom headers and headers various browsers *should* be OK with but aren't8 F0 C5 W3 Z$ Z7 n, i
        #& Y! x0 u) x$ Y  J' D
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';0 X+ L+ `. M. }1 y# h0 l) Q

4 i6 O' O. F/ @  ~        #& z# h3 [# ~2 A9 V/ |
        # Tell client that this pre-flight info is valid for 20 days1 P: e. f( I! b$ Q
        #
7 F$ l# x: B& N  Q+ |+ L        add_header 'Access-Control-Max-Age' 1728000;4 o# a9 y  t# u: T6 ]- X5 W
        add_header 'Content-Type' 'text/plain charset=UTF-8';* U4 m/ t7 K  O  N' g, R# ?) p
        add_header 'Content-Length' 0;* \' j& k& p3 h
        return 204;
' f0 [# G& e5 \. v8 C    }
1 f! c5 F  X& D3 I1 Y
2 h) A. ^6 f" {8 L' }$ o: \    if ($cors = "truepost") {$ X, d3 j! f% a" r- H$ ]
        add_header 'Access-Control-Allow-Origin' "$http_origin";" V" b. D4 }+ D0 T
        add_header 'Access-Control-Allow-Credentials' 'true';
0 |7 G0 [, V* m8 Q; g        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
' Z0 y0 Y- ]1 B$ y  E        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
( J- i& f, O% Y) u: `! [    }
9 N, X' Z2 o0 [/ Y# E! i/ Y8 c6 R! }6 ^! K
}
0 q0 d/ o$ q, s1 }$ e
8 C. i2 Z- w0 \2 m

雿輻券

祉蝛閬

BT蝳

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

QQ|撠暺撅||52AV璈A

GMT+8, 2024-5-16 07:59 , Processed in 0.071427 second(s), 15 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵