52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:
. x* A1 j7 y8 H* v4 H9 p' i! y, O0 M1 R2 ]* b: l- A
+ r/ c% W9 X% L% Y. K0 T* p! |7 M7 I
# CORS header support0 k; ]* D% E1 G; Q0 w
#( [' u  B& R' m9 G& q
# One way to use this is by placing it into a file called "cors_support"
0 Q, x0 \) L6 h  k) p# under your Nginx configuration directory and placing the following# F3 E$ g- Z7 l- X: D/ r( z2 _
# statement inside your **location** block(s):
9 K) ?( l4 y+ g% @#% P0 o# z: n$ Y3 ]
#   include cors_support;& W: w7 Z2 c8 v" t8 X
#; v! u  x  I2 U
# As of Nginx 1.7.5, add_header supports an "always" parameter which  ?& z7 I) i& h: H; m* |5 H
# allows CORS to work if the backend returns 4xx or 5xx status code.; m8 i  e0 j, s3 b5 U, u
#& t4 O% T. M8 n
# For more information on CORS, please see: http://enable-cors.org/
; q, w4 q  m& t# Forked from this Gist: https://gist.github.com/michiel/1064640( q% f$ C- j. i9 v" _/ O
#, ^4 h1 e1 W. M8 o
; k" z2 a1 X/ K- N; k8 M: E5 D
set $cors '';
% n3 o* X9 G6 x! N7 iif ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {  [6 o, j6 z# [+ |5 P
        set $cors 'true';8 i. A& \1 c4 ?% q- D0 J" F
}
! {2 Q! e/ [* m- V4 t8 V. {9 a0 [  O) n
if ($cors = 'true') {
5 S  K% @6 I) s. s        add_header 'Access-Control-Allow-Origin' "$http_origin" always;, n- q6 e7 J4 s5 P5 i
        add_header 'Access-Control-Allow-Credentials' 'true' always;: u* y7 O. M' g& {# N( W" q
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;5 H1 F6 [. @8 C; B/ F  z
        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;
6 j  j- B) i  Q- m( u        # required to be able to read Authorization header in frontend! s5 o6 y' u/ B0 `  \' [
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
$ p% \' S8 [  Y: t}4 P6 c7 ?8 [" W* ^+ g4 ?" T
. [/ F& i8 T) O  H; h6 C8 a
if ($request_method = 'OPTIONS') {
( Q  y. d6 X# r9 D( \$ z: Y, ^        # Tell client that this pre-flight info is valid for 20 days# o# A( n+ a6 e, y9 L; Z% }' u; s
        add_header 'Access-Control-Max-Age' 1728000;& K4 \# e+ ?/ p* R) o8 K
        add_header 'Content-Type' 'text/plain charset=UTF-8';
' i1 _3 I& p# I        add_header 'Content-Length' 0;
8 k, r5 `7 _0 m# A# r        return 204;
* K" }6 o) }% I4 p}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:

% ^( S) Q5 x* S5 L' W
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;
5 \* P9 Y( B: a( w}0 s6 R3 ^, u8 v5 v( \6 Q
set $origin $http_origin;
" ?9 b1 \# J5 Q: s% nif ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {; z( Q% I2 f- [1 Y% I6 j: d
     set $origin 'https://default.yourdom.zone';
& `6 \% m& e! L! V}
! l2 u* @' Y+ {, |+ t9 Xif ($request_method = 'OPTIONS') {
+ R4 r3 K% V* v: ]" u$ a0 w# Z* Z     add_header 'Access-Control-Allow-Origin' "$origin" always;
0 u5 y7 A2 V% g/ b; u     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;5 W) F" I& s7 K- l: @
     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;
$ ~7 Q' w  p# m+ r  u8 l9 O+ M/ _     add_header 'Access-Control-Allow-Credentials' 'true' always;
/ y- `; Y! T& y* Z, d     add_header Access-Control-Max-Age 1728000;   #20 days   , I, T* ]- w# f0 v) N5 b/ L1 }# f
     add_header Content-Type 'text/plain charset=UTF-8';! N4 L% Q$ v( a
     add_header Content-Length 0;
: y3 q, k+ B2 V3 R! `     return 204;
2 {* ]9 z: Y+ I( n}
! K& i4 U5 C* {# [  I/ pif ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
0 J* b' T5 K/ O     add_header Access-Control-Allow-Origin "$origin" always;: z2 r1 R% q0 d! a: R8 J4 x- k
     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
# k/ n" r6 u0 b, W. {7 R/ ]     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;  Z$ g! H) {9 @% I$ z8 ^
     add_header Access-Control-Allow-Credentials true always;
4 J% g& m4 `6 s! z  j}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
" h% C. D4 f) Q+ Y6 ~' X8 B#3 K0 i  |" z6 I) O1 k, R9 Z/ D
# Slightly tighter CORS config for nginx
2 r/ y9 X# ^/ X: c5 I3 W# r! O#3 U# O7 o! Y8 `$ @" I' f6 x
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs: i2 G- D% a; }( P9 i  r
#
$ d7 N( U3 J( R7 p+ G0 I# Despite the W3C guidance suggesting that a list of origins can be passed as part of
2 F( I, D8 T2 y5 J. j+ \# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
$ S. H( }: B5 l, W; v# don't seem to play nicely with this.
3 X( p6 j2 k' z6 D2 Q! q#
5 d0 P' i. p. }$ M# r" @# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting3 X6 C# q8 q) L7 {0 g
# method to control access instead., C. Y9 ~% h+ L5 b  q5 `
#. n* ]3 W; t# s9 T5 _5 E5 p+ K6 I
# NB: This relies on the use of the 'Origin' HTTP Header.$ p- j7 [* B4 b1 `2 A5 j) z
# M" I  i9 U. c: ~8 p9 w( M
location / {
/ g, w8 o: K. w! e$ t% s. [" a/ ^' _5 B) r5 k7 L. e
    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {3 `8 P8 {" f- Y- b6 N; w, `
        set $cors "true";
$ g9 O, n9 J* e! V6 k& @* T    }
; c: y2 y! C: j  {1 l
1 a. ]2 [3 l( N8 S# A* }3 J    # Nginx doesn't support nested If statements. This is where things get slightly nasty.
' M8 f# x( I$ O  Z6 d- s/ j% u    # Determine the HTTP request method used
8 N& x  b8 ~* G9 B    if ($request_method = 'OPTIONS') {1 A$ w8 X' Y1 ~1 u1 _
        set $cors "${cors}options";
/ O4 D/ i" I3 G5 T    }
" V, _1 |  R& l2 Y! u  X: X' d    if ($request_method = 'GET') {$ J3 r" h# e* C  E9 u- ], r0 r
        set $cors "${cors}get";# r" s- f3 X& h0 X2 G
    }
5 N$ d  F* L$ R5 j/ e! [+ ?    if ($request_method = 'POST') {
$ r! |, p# h; ^3 O7 b* M        set $cors "${cors}post";
+ k- H6 V3 F5 e8 r9 `    }
1 T- F4 Y6 f$ V3 y
. x# M5 s6 Q) A; Z; u8 S# a    if ($cors = "true") {* O6 C1 l1 a, P- `; T* x" W
        # Catch all incase there's a request method we're not dealing with properly
# `  w4 L" P% x: h' r$ ^        add_header 'Access-Control-Allow-Origin' "$http_origin";
! a; D8 ~3 D6 L" y/ r4 g    }
7 A7 A: d+ O5 i( j1 P' h1 [& ]8 m
    if ($cors = "trueget") {
/ B5 v# e9 H. D8 B- Q# m" m        add_header 'Access-Control-Allow-Origin' "$http_origin";
0 H" u' F; ~1 h        add_header 'Access-Control-Allow-Credentials' 'true';6 F5 l# Z. g5 v: p" u
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
. j9 _+ ~/ Z9 l3 M1 v/ ?        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
8 G' y" v3 v; I$ D( J. z    }2 ~' K! k- g. f6 m4 A$ r! k- d
# J- w1 q/ T. E5 V1 x# J1 y
    if ($cors = "trueoptions") {& J% R& u1 x! A* X* p+ g
        add_header 'Access-Control-Allow-Origin' "$http_origin";7 R0 n' Y( E3 @% b
" }9 H1 S6 I! Y. O
        #
" C( b2 v; ?  ~, A3 Z, @  v        # Om nom nom cookies+ a* V4 X+ o# u" U8 o; F
        #( q! c. j- s: d- @$ H+ R1 c
        add_header 'Access-Control-Allow-Credentials' 'true';
0 S6 z( e# i$ D: ^; N6 u% `        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';% h5 u. E: ]! g1 b1 o; D0 X. H

% R3 F" t6 J2 h0 u4 ^8 i& \        #
8 q5 s4 z- ?4 D$ S* b        # Custom headers and headers various browsers *should* be OK with but aren't) o4 ~2 E9 h7 C2 n2 m* h
        #% e& y; q6 ~$ `0 K
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';6 H2 \& F$ S" B9 t- a7 r
# H% D& ?6 k0 [; E, m
        #3 E5 D- Y, L8 c
        # Tell client that this pre-flight info is valid for 20 days
/ d" W7 P7 E# n9 r        #
& o' f( F; b1 j        add_header 'Access-Control-Max-Age' 1728000;
) P) x1 S0 P! s        add_header 'Content-Type' 'text/plain charset=UTF-8';
3 }( W4 l( T' o; g8 I' X8 q. l        add_header 'Content-Length' 0;9 V; h; P  S" T) `5 L
        return 204;0 E" H. @& ?; }/ z8 s
    }/ o. K2 D, @4 d4 }( a" b
; S5 v& s4 r  d! e3 F
    if ($cors = "truepost") {0 x3 s) K, m, k9 Q
        add_header 'Access-Control-Allow-Origin' "$http_origin";
# @* H4 ]# H6 ]5 w" U% I! h% M& O        add_header 'Access-Control-Allow-Credentials' 'true';
  x: \$ W+ l  z' m        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
+ P% t0 V0 H$ X9 V: I1 E# L% G        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
2 m' g" S; {" }3 Z    }
8 i  {6 Y) c- G9 h" W) y; V4 y; Y8 B4 ^; w- P% `% U$ `5 S
}
5 h) e  z2 ~1 g' [9 ]
0 t4 v, e4 b* `& J$ ?6 z

雿輻券

祉蝛閬

BT蝳

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

QQ|撠暺撅||52AV璈A

GMT+8, 2024-5-4 19:23 , Processed in 0.067364 second(s), 15 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵