52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:- F! Z( j  ]8 ~
5 C/ ~7 ~0 m2 Z' C+ J1 N& z, f& J
+ k) J5 X* c! O' r7 g# R/ t: d
# CORS header support
2 T! N0 T! [; r& k# G- T7 \#) t) y3 ^  v8 p  P( ~, r: Z
# One way to use this is by placing it into a file called "cors_support"& M/ \/ Z9 k$ C  C5 t- ^
# under your Nginx configuration directory and placing the following
$ d2 h+ |3 h0 ?) ^( L# statement inside your **location** block(s):
# _, V5 `) l4 g4 e0 D#( P7 J) B& l+ k& P# H+ r
#   include cors_support;% X: l8 W5 S; B7 a$ m0 s
#
; h" |3 A4 G$ F' o! p# As of Nginx 1.7.5, add_header supports an "always" parameter which, K- o% p* D+ V( J' Q/ s
# allows CORS to work if the backend returns 4xx or 5xx status code.5 B; \9 d% G; Q; o3 U8 _! e
#
  ]8 Q4 l# J8 |( b( X, ^# For more information on CORS, please see: http://enable-cors.org/# R$ e7 d$ d: z) i. G, V# K1 C4 I
# Forked from this Gist: https://gist.github.com/michiel/1064640
# R! H1 t3 v& _( y+ r#
/ v  X. P! E  y1 v& X# J
8 g6 }% s4 L# w- L& W) f: Gset $cors '';1 c% o1 _- R! u3 d
if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {, {' V" V; X1 S; K# }, a# p/ q
        set $cors 'true';2 J9 _5 A* k# x' `/ l
}
7 k& N, }. t& h- g' r% I7 u5 C+ h7 Q  ^& ~: y# W0 t, W+ A
if ($cors = 'true') {
/ X9 G0 j3 B$ c) T3 C* n        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
$ I: S) L1 \3 O4 C0 y        add_header 'Access-Control-Allow-Credentials' 'true' always;
. H! E$ M$ r, v. e        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;6 K# i7 T$ g3 `3 h; V; P
        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;, ?2 k5 z- f. Z5 ~% I/ Z
        # required to be able to read Authorization header in frontend
% u* P0 o+ v1 R* u        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
. y  }- y5 _) E5 e. R5 l9 `}# |  T$ H  R' e$ F  @/ i6 F* C
$ C. L3 x- S$ e$ [( {2 \
if ($request_method = 'OPTIONS') {$ z7 ^5 z+ c- G/ d
        # Tell client that this pre-flight info is valid for 20 days
. u/ p- t3 }9 N! j& q8 _- d& |0 u0 J' X        add_header 'Access-Control-Max-Age' 1728000;. i) V1 m; F, B  d8 x" ~* `
        add_header 'Content-Type' 'text/plain charset=UTF-8';
9 p& |/ c# x: }. d; d        add_header 'Content-Length' 0;
" Z  B9 X4 G5 |! g% u2 m6 ]7 U        return 204;
+ a& E) Q* |# b$ R}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

  ~! Y8 S/ h, N. d
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;: I4 c2 {" U+ L1 O; j
}! Q; L- ^) J! O
set $origin $http_origin;6 `4 v: B- p( o# K. ?
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {- G2 @, i2 d( s% J0 G/ P
     set $origin 'https://default.yourdom.zone';
  I. [7 t0 v" L- }3 g, o/ w}
6 m: Y; i/ {( X. M' rif ($request_method = 'OPTIONS') {
$ n4 b% [+ x9 G     add_header 'Access-Control-Allow-Origin' "$origin" always;
, o$ J: R5 ?2 w! p$ q# R7 [; c     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;  n/ g9 i$ D# Z. v  w7 X
     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;  R4 J  C  Z: q
     add_header 'Access-Control-Allow-Credentials' 'true' always;! g+ k; |- D6 b+ I" a8 o# u, x( {
     add_header Access-Control-Max-Age 1728000;   #20 days   8 r: L% K" M4 V
     add_header Content-Type 'text/plain charset=UTF-8';
7 ?. k- u7 O! G# q5 I     add_header Content-Length 0;
* h" }  q# l2 N8 d4 y/ n     return 204;8 s4 y% l+ [; q  _
}; Q+ E2 w8 n5 _4 w0 U
if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
- }4 {+ F2 a+ h( H# `     add_header Access-Control-Allow-Origin "$origin" always;! w$ v4 x* d; r1 p/ P& D' K$ x/ F8 f; B
     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;( `6 P, k% \0 M+ I) ]' i, @
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;% `7 L+ [; W/ ]' E2 V* ], p
     add_header Access-Control-Allow-Credentials true always;) V$ }5 y9 t' s0 `- d8 w
}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
! q& o) t( W) d3 W4 B- {#5 u' {: T' M* V- C0 m8 J
# Slightly tighter CORS config for nginx
' ~+ C* B; B1 g3 g1 b: ?8 o#3 Z* L4 t3 Z( r$ g
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs2 W9 ~1 K: D& y, h
#
! ]* I1 F7 l4 s6 w$ g) @. _# m9 S# Despite the W3C guidance suggesting that a list of origins can be passed as part of4 l& ]3 p- }9 y( |  V% Y! b1 _8 U5 H
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)# t8 O4 d6 M# X$ V( ^3 q
# don't seem to play nicely with this.
7 V/ _7 l. g2 Z$ B#( w& ?5 A2 W: o9 O4 s9 w1 i
# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting: x* g$ o7 o1 k% W4 m  S1 {
# method to control access instead.2 y+ ~' ]2 J& L
#1 d+ a( s2 ~, L' x0 D5 N3 m: P
# NB: This relies on the use of the 'Origin' HTTP Header.
' r! B4 |9 M! ?" ~$ q
5 r+ s9 M* K5 z. R% j% W3 Ylocation / {( g) [3 X  a' r; b
# l4 M) r! q: @" z
    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {. E! N7 i0 P! K( l- n9 }2 {1 A+ m! h
        set $cors "true";4 J- v) W7 H" s4 t* U
    }
2 d; P0 f/ S5 k* q8 |9 L, \( A' g, G& m" ], Z
    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
% J: q! H6 {8 W' l* I$ n) t" ~    # Determine the HTTP request method used
9 F0 [) I  ~3 Z+ ^+ K! C    if ($request_method = 'OPTIONS') {) d; v$ f* q; J1 u$ m. s8 a
        set $cors "${cors}options";
. P' j  l; z+ o* d    }1 S# z' v8 t6 j; C
    if ($request_method = 'GET') {4 b2 z/ W/ Y% o9 I
        set $cors "${cors}get";
, X/ f! d& F% Q0 ^9 c" U    }7 L$ s. y3 ~! a+ H2 p/ g
    if ($request_method = 'POST') {  m9 M* W/ g! ^$ M+ L% c% i
        set $cors "${cors}post";
: }# P( @- o9 m0 b$ E% b( K0 g    }$ J' K7 g' m+ o# l; [/ Z( d# \6 b
  R4 }, A8 D$ T
    if ($cors = "true") {
8 v& `5 e7 O& u% g4 l        # Catch all incase there's a request method we're not dealing with properly
, |( Y; C  c& }! }        add_header 'Access-Control-Allow-Origin' "$http_origin";+ S' o! u0 V5 D6 n! B) F& f
    }: K/ ~; Q+ J! a: E* p

0 u6 F7 Q/ Q& m! o( p8 g# {    if ($cors = "trueget") {" t- e& Z# T! R# n
        add_header 'Access-Control-Allow-Origin' "$http_origin";
7 q3 S6 u5 G8 }- Y. Y% Z. _        add_header 'Access-Control-Allow-Credentials' 'true';5 W0 z2 @/ ^6 u3 a% _2 X
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';9 T2 m! ?5 O" [) X$ ?
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
& v1 y( j+ d' t7 L& _7 z4 ?    }
6 A9 [0 J, U9 [+ y' ]* a% `2 G0 Q& c# V2 P$ n
    if ($cors = "trueoptions") {
" \3 [; O+ C9 C, d        add_header 'Access-Control-Allow-Origin' "$http_origin";
# ?0 r4 L8 e3 r1 h6 K* u  c
5 U! \) Z6 W' I4 Z6 W        #/ c- r: O+ A& l, `2 ?7 A. ^
        # Om nom nom cookies: C: T( D+ \8 Y) `4 @6 R: H
        #
/ x5 a% f  n2 }2 j        add_header 'Access-Control-Allow-Credentials' 'true';
( [+ ?- A  E5 o( _        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';1 L- }2 T3 Q) s5 m
' f5 Q$ m2 _+ J. d: x3 k
        #
- {$ N/ E9 \8 z        # Custom headers and headers various browsers *should* be OK with but aren't% @+ i2 L9 J) ~4 F
        ## k3 T. ~8 P1 G$ z
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';( o: e: A' o! O8 j+ t" Z  ~

  k9 q$ c) P; P        #
% L9 m" X! Z1 W6 G        # Tell client that this pre-flight info is valid for 20 days6 N9 T. f/ A! J" |) @" [
        #1 x: W% X2 @5 q% g% }2 S
        add_header 'Access-Control-Max-Age' 1728000;
2 O% Y' O6 R3 ?8 N+ q+ _! Q        add_header 'Content-Type' 'text/plain charset=UTF-8';% A! Q3 Z- x: u3 f4 }" v% v  v
        add_header 'Content-Length' 0;. d$ v: J3 x6 s' B. g
        return 204;# ?! S  i. c0 y/ U) @( Y! ~
    }
' ?$ Y' z7 B) p8 Q2 z
1 t7 n9 t( C2 Z) X9 q    if ($cors = "truepost") {
9 T5 K- w5 G& w# _+ ?& Y: z& t        add_header 'Access-Control-Allow-Origin' "$http_origin";
7 I# ]1 u. ^( I3 q, z' P1 P        add_header 'Access-Control-Allow-Credentials' 'true';
& \* |* \4 [7 e+ J! O3 V3 c        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
6 q9 @* t5 A7 Q" Q0 W2 R        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';! e$ E( J! p4 @# X% Z
    }
6 q- y( U- D4 @) z' P, t
: T8 U! `& ]% g3 P; f# ^& w& w}

2 H) t6 G5 `1 N3 w9 a1 |
. ~0 a# I8 O1 q8 ?/ ?( C3 }- K

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-13 00:05 , Processed in 0.082613 second(s), 15 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵