52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |甇摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:$ b" O8 r; X# Q2 z. a3 e+ M
6 J( `: E9 y7 Q$ a( s1 G: e
) a9 [2 \( d! C5 g, E. q& z/ R/ n
# CORS header support
) Q$ _' x. t" |. }6 b#1 [; k% D9 i- ~2 P3 l7 s2 R
# One way to use this is by placing it into a file called "cors_support"- L" @; `( @& U/ x+ j5 L  x0 Q, S
# under your Nginx configuration directory and placing the following( `. U  i5 `+ l1 E
# statement inside your **location** block(s):+ h  Y4 M* O2 [% `9 B& d- y
#
2 G0 o3 z2 b: Q7 {1 N2 e#   include cors_support;
' A  ]; q- J4 W* @#' O( s+ f& n- X0 T: s
# As of Nginx 1.7.5, add_header supports an "always" parameter which7 {7 t) F2 \0 {( ?2 d
# allows CORS to work if the backend returns 4xx or 5xx status code.
7 ]! ^" Z, J8 F. W% ~. o; I, U#
5 C! V' c+ O% T$ M3 \. O# For more information on CORS, please see: http://enable-cors.org/3 V, [9 w; k# e/ p6 w  k; |% Q
# Forked from this Gist: https://gist.github.com/michiel/1064640  p: T6 j+ k( Y2 u4 I
#
8 `* B# Y% t) j+ _/ V& B8 p) s! q$ F( X- W7 {1 @+ T
set $cors '';
, T' z$ `% a3 u: c" ?if ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {5 G+ F6 b) J/ R8 J0 B9 i: `/ e
        set $cors 'true';7 V! t) y' X- Q- d- d# l
}( r1 c5 r, s& p3 g+ I6 S- g

  C* H8 v6 ]# P9 d* R: t( jif ($cors = 'true') {6 J+ i& q- Q# E1 s3 O6 F
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;$ u1 i4 B# u% c1 ^
        add_header 'Access-Control-Allow-Credentials' 'true' always;2 Z2 E# o! [5 z
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;* [: D2 H% {& 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;7 s6 K: E. e/ a2 T1 Q. D
        # required to be able to read Authorization header in frontend7 D" _/ c/ |8 D
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;/ a5 A5 C/ c+ ~/ u! ]
}
. b0 Y) r# m# m$ ], e: X$ L3 u) Q6 ]$ f# m' J& K8 j
if ($request_method = 'OPTIONS') {; D: m( P, F1 x+ Y) [' I( |; b9 C% f
        # Tell client that this pre-flight info is valid for 20 days
) Q+ d( {' V, n. q; D1 \# ?$ i% l        add_header 'Access-Control-Max-Age' 1728000;4 ?3 y& W1 [/ o  c" A6 ~3 I4 H* h
        add_header 'Content-Type' 'text/plain charset=UTF-8';) y$ g+ B# ?1 R6 V( c9 N
        add_header 'Content-Length' 0;) J6 S$ x+ ^; C  o- C" C8 t" x
        return 204;& K6 u; S7 E! M) F9 x5 C- h
}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

! m. s+ ?) Z' P
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;
! p2 v0 I0 b' {/ ^' S& j. B* b}
* q. s' s: r: bset $origin $http_origin;# l- N$ J: L0 K0 e3 d& L+ L2 }
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {# w7 C% M( Y" X" U' y! A0 g
     set $origin 'https://default.yourdom.zone';) {: F" L' q3 H- a# ~
}( h1 D3 s: i5 u' M# [, B
if ($request_method = 'OPTIONS') {/ i' t* N, Y: F
     add_header 'Access-Control-Allow-Origin' "$origin" always;
$ b/ T; P% M2 m1 e     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;( \% f5 e) K( B" V
     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;4 g0 `1 a* ^' u6 G, u7 |: F
     add_header 'Access-Control-Allow-Credentials' 'true' always;  s3 p% L- G% u2 b  g5 o8 v- n
     add_header Access-Control-Max-Age 1728000;   #20 days   
6 {3 L* L% J: w7 ]     add_header Content-Type 'text/plain charset=UTF-8';: m- @' _1 ~7 v- u: X
     add_header Content-Length 0;9 ^& Z8 i2 t* D) C! ?
     return 204;
' G! v( h% p' A! o9 P) p+ s}
: B6 N) J; k) V4 j( vif ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {4 b3 f+ b6 Q  b4 i
     add_header Access-Control-Allow-Origin "$origin" always;
% r- h( k4 K9 E     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;; [! E& Q6 @4 A6 V& r
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;
% ~1 k* z$ g: {; a, \, a% I+ l! ]+ h) V     add_header Access-Control-Allow-Credentials true always;
* Z7 t6 v; m% u* v}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271// p, r) O, k+ X0 Y+ A. e# D
#$ a4 K8 `5 Y& M1 f: k  M
# Slightly tighter CORS config for nginx: k9 K7 a5 F. D
#5 J8 Q' E1 v, \7 m
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs
) j4 _6 [9 V  _& K) C2 H#
2 s- [) w/ F- [7 }' W# Despite the W3C guidance suggesting that a list of origins can be passed as part of2 L, g, P: @' Q- \( e' h
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
- B( b; e+ Z# u, [" Q8 M: L# don't seem to play nicely with this.
5 r5 f; [& c2 H* S) }#
! N* z4 A  Z( T1 G2 p1 A# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting) L2 H* n/ W1 b
# method to control access instead.4 T/ q( Q0 Q2 m
#( ^; G- W- i. B& }$ @
# NB: This relies on the use of the 'Origin' HTTP Header." j/ Y& b8 B6 C# E" R# @2 F3 M2 I4 N
5 g6 L0 W1 z1 f- W% O
location / {
% Q; T( |& H9 s) o" c1 g: S; m* N& h8 R( Z) `8 g7 E/ v
    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {
3 L$ u* d2 r, [( |        set $cors "true";# f# x# h; ]" z5 _  K  w' M5 y/ W4 E
    }
; F, ?" d/ X! A! X# r
$ ~3 F2 N2 g) }    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
1 I- r4 o' i0 ~) K+ H! R% o! s$ i    # Determine the HTTP request method used
# X" D4 ]- O( N    if ($request_method = 'OPTIONS') {
* v1 e3 o9 D, l6 ]) m        set $cors "${cors}options";! X% h" `& Q! F6 W
    }& k% ~$ v$ U" _
    if ($request_method = 'GET') {
) E) t0 X6 i3 [8 h5 E$ L. b3 u- E4 A        set $cors "${cors}get";
' ?, E: u5 W: l7 f4 t2 s    }. C( f+ v- C  f! E3 d  p
    if ($request_method = 'POST') {
. R0 s$ j/ _3 v& e, b8 K        set $cors "${cors}post";
% v0 b, W2 h3 O+ C    }% _) n" D; M' X" M

) A. F1 b2 p& s) y7 g    if ($cors = "true") {& l( {1 m: y+ |) u7 t5 }
        # Catch all incase there's a request method we're not dealing with properly
. \3 J, B$ n6 l        add_header 'Access-Control-Allow-Origin' "$http_origin";* ?2 f9 X; u8 j8 m
    }
, v6 U: }7 k. K8 B4 p4 t! D7 @  q( J
' N* n6 R0 A7 R; N  c    if ($cors = "trueget") {# Q6 U: O- k4 N# w: c
        add_header 'Access-Control-Allow-Origin' "$http_origin";+ Z* T9 \4 l; L) C/ x
        add_header 'Access-Control-Allow-Credentials' 'true';7 I$ U# ~) j* p" l- W" ?, ?
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';8 j5 j' m; k' i2 P0 U
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';$ c% d$ z+ F! `$ ^; e2 X
    }: U/ u3 J: g. R$ v0 d$ v. ]5 z, p
) w  L7 ~( \) M8 i0 F7 c8 }) j0 L
    if ($cors = "trueoptions") {
* h* U4 u% Z: Q0 G4 Y        add_header 'Access-Control-Allow-Origin' "$http_origin";
9 p/ j# S8 f. U
6 k; P; I9 g! Q; s- J, S, T        #
; Q! k. F- r, j3 |% e# f7 n5 |4 Y        # Om nom nom cookies
$ ^+ M% M9 P" {( ?        #! h+ D" ~" Q# K7 F7 U; n
        add_header 'Access-Control-Allow-Credentials' 'true';
1 ^2 ]7 f, H7 t$ q/ [        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
- o) }/ }: s6 ?/ k/ o6 k" ]- G2 x4 E) `
$ }+ G; c5 ]1 W! V1 \" K        #! p3 h$ Q7 _( n9 s5 R0 `9 }- D
        # Custom headers and headers various browsers *should* be OK with but aren't+ A' g' E- \' X7 I& Q
        #
0 J) I! v$ q" |/ T% C% \6 ~        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';; F9 a& d/ d9 g* ~0 K/ T" R
7 `1 S5 p. U+ h$ j- F
        #, V( s- p/ K6 o! d
        # Tell client that this pre-flight info is valid for 20 days) ^' y7 y+ J& h9 t6 K
        #
, T* v3 O! s2 m4 y8 N        add_header 'Access-Control-Max-Age' 1728000;' @$ Q8 t" a! D. G8 n$ a
        add_header 'Content-Type' 'text/plain charset=UTF-8';
% P0 V1 S2 C9 ]8 P( P4 T        add_header 'Content-Length' 0;& w$ }7 r( E" e3 o5 A# y( i
        return 204;
8 B( J2 d( ^- Q. h& c    }* Y! }1 i. E2 J0 Y) t5 J2 J

! i1 W2 r5 n5 U5 T% H    if ($cors = "truepost") {
) ?# j, P5 o% N        add_header 'Access-Control-Allow-Origin' "$http_origin";, H6 U8 |% n3 y5 V. V
        add_header 'Access-Control-Allow-Credentials' 'true';/ w( C: z  j) [. G9 W
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
4 @$ Q; {" X6 Z  U! r) Z: y" c        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
) a3 |: N# S2 r9 k  N) i    }
/ V) H/ B) o/ h- e7 _! v$ Z5 R0 d4 [5 n; I+ q
}

; B# B0 r7 ~- O2 f1 H# Q% ]" l
+ o6 ?) A/ Z! G/ H

雿輻券

祉蝛閬

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

QQ|撠暺撅||52AV璈A

GMT+8, 2026-3-15 19:01 , Processed in 0.069559 second(s), 16 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵