52AV璈A|52AV.ONE

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

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

[銴鋆賡包
頝唾唳摰璅撅
璅銝
潸” 2019-2-20 09:34:17 | 芰閰脖 撣 |甇摨閬 |梯璅∪
隞乩gist.github.comreverse proxied APIs蝭靘:* I+ X; T6 a2 S% I3 q2 R& h
. {$ }: r# V1 k9 h2 n6 `7 G6 O* g
$ r. v  M3 n5 F4 }5 L2 m8 N
# CORS header support; ]3 e: X8 N8 I' `4 F1 A& |
#
7 U- t/ D. y/ h# One way to use this is by placing it into a file called "cors_support"
) t- T- [8 @, `: c# e# under your Nginx configuration directory and placing the following
9 V9 @9 w! B! p. q/ \/ `) J# statement inside your **location** block(s):" \5 Q' x- ?1 ]; Y4 h4 a8 Q
#' I1 B/ O9 {' O' i5 m
#   include cors_support;
# N. ?' M. a, ]6 @+ W. d#  A/ q$ {) s. w2 O
# As of Nginx 1.7.5, add_header supports an "always" parameter which
. A1 w- T% H! T# allows CORS to work if the backend returns 4xx or 5xx status code.
/ x0 B- @0 w& [#
' ]6 B  L! a$ X! u# For more information on CORS, please see: http://enable-cors.org/% Q  r5 S6 s0 R
# Forked from this Gist: https://gist.github.com/michiel/1064640
: J! L. C$ ^( b- d#+ ?' y/ ^. r& \. Y6 I2 S, G1 U

, R  f4 c8 C  o% Hset $cors '';
9 M7 G6 U  w9 a' B6 B6 v8 s1 D3 oif ($http_origin ~ '^https?://(localhost|www\.yourdomain\.com|www\.yourotherdomain\.com)$') {
; ]) B! X  }$ G$ l4 w# U        set $cors 'true';3 ]# Y( Q) u5 w* K2 T6 c& s
}9 r  Y6 v: Z8 Y; ~( ~+ D
# |, q- A- O( N3 O1 x
if ($cors = 'true') {
; z3 v- F1 Q/ ~, x. y; \- v        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
: l8 U$ U+ b: D        add_header 'Access-Control-Allow-Credentials' 'true' always;+ U" f- U! F) h. i
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;4 ^% L$ K4 {3 s9 N; f, V# _& 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;
. u3 h* g, Q0 Y4 g9 y        # required to be able to read Authorization header in frontend
. I1 I/ [6 a, _  I        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
4 A7 |% f4 N( L+ c! E}$ w1 n8 u  V/ L5 U
( k( f$ B& Y. r. ^, o
if ($request_method = 'OPTIONS') {: Y6 J2 [" U: [6 c
        # Tell client that this pre-flight info is valid for 20 days" m. j' q) C2 ?
        add_header 'Access-Control-Max-Age' 1728000;
% D8 w1 r' B* q! S6 x+ I        add_header 'Content-Type' 'text/plain charset=UTF-8';
5 D1 n# E. k; C7 x6 ~        add_header 'Content-Length' 0;. Y, |+ }8 c+ L0 X, t, d; @
        return 204;
6 m9 d# ^9 M" y, d3 f* A}
https://gist.github.com/Stanback/7145487#file-nginx-conf 閮隢蝭靘:
& d2 Q- F& F, u, ?  y. G4 U- }$ i- c
if ( $request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|PATCH|DELETE)$ ) {     return 444;
1 G3 m- v2 k! ~1 K- }# A}
; g# t7 ]/ ^2 E9 X* `$ [0 w+ g) d% d( k5 Gset $origin $http_origin;' c: K4 N4 q  u! G1 F) [8 d
if ($origin !~ '^https?://(subdom1|subdom2)\.yourdom\.zone$') {2 p$ Y2 u3 X) s& i: s6 n. w* _) n
     set $origin 'https://default.yourdom.zone';- \* }; v: d4 }* m
}: [/ e  _( }5 E" f9 U* [6 N
if ($request_method = 'OPTIONS') {
/ }% w' ^! W* P' A     add_header 'Access-Control-Allow-Origin' "$origin" always;
5 |+ l% k$ F$ _* v4 a- |5 x     add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;
* v$ P4 U! H1 @) [" f( ~8 D5 G) W     add_header 'Access-Control-Allow-Headers' 'Content-Type, Accept, Authorization' always;. ^8 j7 a( P  m
     add_header 'Access-Control-Allow-Credentials' 'true' always;
4 E' @- W; ^" n' ~     add_header Access-Control-Max-Age 1728000;   #20 days   : L+ w8 V! J; E: x' F/ h
     add_header Content-Type 'text/plain charset=UTF-8';
4 f# u% s9 w% p( W0 v8 h     add_header Content-Length 0;
- }% j  K( x$ w0 w$ G6 c     return 204;5 C4 p; j1 g; o; F2 K
}
1 A. F1 W* |" ~if ($request_method ~ '(GET|POST|PATCH|PUT|DELETE)') {
4 {5 p1 \$ J- u" D5 z3 r' p     add_header Access-Control-Allow-Origin "$origin" always;/ ?# u  ?: A8 ^4 B
     add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS' always;* a; N3 _5 T1 @. R' P4 v( ^1 H
     add_header Access-Control-Allow-Headers 'Content-Type, Accept, Authorization' always;" m, F0 Y0 u  U- R8 x" U( v9 w
     add_header Access-Control-Allow-Credentials true always;; K6 r$ h) q; R: H. p$ P, V
}
Access-Control-Allow-Origin Multiple Origin Domains? 靘摮:
# based on https://gist.github.com/4165271/
. a9 [7 Q) o, {#
! B9 Y. L; E# j9 X; W& Z$ {# Slightly tighter CORS config for nginx
) V% q3 K* o1 J2 ]#; I4 E! N0 O  L- b. \4 f/ M: l$ d
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs/ z0 m% i3 |( Z" h: p/ i9 q, `' V
#: L# C- O( \5 ~: J8 h
# Despite the W3C guidance suggesting that a list of origins can be passed as part of! A8 q( t/ k4 [- S" ^+ i
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)5 ~. W7 b( ]# g1 u" U! [: M( e" }
# don't seem to play nicely with this.  O& T4 Z; b+ N, s
#
. t8 B3 O+ q9 @; v# To avoid the use of 'Access-Control-Allow-Origin: *', use a simple-ish whitelisting: {% u: R) y% q, f& _1 ?3 _4 n
# method to control access instead.
$ }$ z5 T9 A9 ?4 p3 b- K/ e- x: H#, _4 Z. q- U% I/ L5 Y7 `
# NB: This relies on the use of the 'Origin' HTTP Header.
2 c) V" \4 w* f' {6 |8 B$ F( _" d
location / {
) U" Y/ k+ G3 e  I' G
, u3 ?: r1 W- r, i+ I+ I* |    if ($http_origin ~* (^https?://([^/]+\.)*(domainone|domaintwo)\.com$)) {
8 f# x/ y; q) d! g        set $cors "true";
6 e3 m' c  k$ o) I7 }    }6 H: x. L: k! c

0 u- y* o0 @$ L/ u* r% y) a    # Nginx doesn't support nested If statements. This is where things get slightly nasty.  b/ {  v& q3 g2 U" N# U8 G; O- `
    # Determine the HTTP request method used
: Y8 C5 A6 r$ l7 A- i, o+ s$ ?    if ($request_method = 'OPTIONS') {
  h! r, w+ x1 x! w1 o        set $cors "${cors}options";, K9 E+ F: e1 `, G- i
    }
6 @3 |5 _6 {, c8 ]7 S, N% k    if ($request_method = 'GET') {
  P% d6 S0 O/ Y) V2 c; Y        set $cors "${cors}get";
1 @3 A# y3 u5 V  [$ W+ D    }
5 P! O5 s3 N) E+ M2 P    if ($request_method = 'POST') {
2 C! i4 A& q0 {8 c/ h        set $cors "${cors}post";' f; s4 B, o# Q! }0 {* d2 V
    }. Q) ?7 ?  Z* e( P- {1 i  q

* U# [, \- {% b$ [6 ?6 j7 S    if ($cors = "true") {( c8 q/ L, t, B- c" \/ S# N! V3 _7 x( |
        # Catch all incase there's a request method we're not dealing with properly
7 y5 N: \& }4 y. B        add_header 'Access-Control-Allow-Origin' "$http_origin";
* R" H/ E# u4 f+ g  _1 u+ R4 m' A    }5 D: r: [! [1 q# `$ A( b+ {3 N
: i$ r% w) v4 d: q# a" v4 W# K
    if ($cors = "trueget") {" V2 e% [! O( T
        add_header 'Access-Control-Allow-Origin' "$http_origin";
! b* Y- f- \+ b% g* Z        add_header 'Access-Control-Allow-Credentials' 'true';
# z  b. J4 }  W; B5 U1 U6 m        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';% z9 _( O* ~" l/ ]- F5 u
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
' K) |" N- Q' \: C$ A% k! I- i    }
5 j# k% g' i) G5 t$ `+ [3 a2 a: E
" g9 w, W$ d5 ^. O9 ?    if ($cors = "trueoptions") {$ x3 e# w( C- ]4 n* W
        add_header 'Access-Control-Allow-Origin' "$http_origin";
, `5 p& b% ^, v5 f5 |9 M$ m7 g; @/ O7 m3 v& D& g' u/ ^! R
        #
! p. y! M- Q& a* V- m9 ^        # Om nom nom cookies2 N9 M+ i+ K" v) d! Z
        #" O& `( Z6 q' f) I% Y6 S* C
        add_header 'Access-Control-Allow-Credentials' 'true';8 g( }' d, n! h4 l6 m6 b
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';3 h- I/ L9 R; b9 G7 @

2 q# ?4 S( @  T  {& ]. x' x# G' T& R        #
& I$ w* o) c# W9 H( V# l# B( F        # Custom headers and headers various browsers *should* be OK with but aren't( r& f3 e0 m3 l, _" v, P
        #
% U8 c: Y3 e! g- k1 D, V6 p% K        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';' ]  ?" g1 {0 l7 z. F* d
4 f, t, S9 O8 r* ?, Y: l- Q
        #1 c8 x8 Y6 ]0 A( I
        # Tell client that this pre-flight info is valid for 20 days
' P* O) {7 ^! `% `        #
* q4 B7 z% a) G$ g. X        add_header 'Access-Control-Max-Age' 1728000;
! g7 o* z* x, \/ R# O        add_header 'Content-Type' 'text/plain charset=UTF-8';9 [6 k% q+ K4 A% \1 u5 }
        add_header 'Content-Length' 0;+ h$ ?9 g. s7 y
        return 204;- m& v+ C! {' C! i/ X+ h" x
    }
7 f3 Q  j# M. z# W0 E; L8 k' h) ?
  k2 ]  N$ |% |/ c* g! \    if ($cors = "truepost") {
5 t7 X# r& p9 q# d        add_header 'Access-Control-Allow-Origin' "$http_origin";; P# N6 Q+ ]& M' o. }
        add_header 'Access-Control-Allow-Credentials' 'true';( r) o- E8 i+ H. _& c
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
1 ]* a( |1 w- x# J/ q. [+ [/ a        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';% Z* @  ]% `+ [/ T" D
    }
# |4 R8 i, I5 m" v5 R1 M$ a! A' F7 g% b2 v" `
}
" T: v/ z; l: h

8 c( ^) a  Y4 q' h# x

雿輻券

祉蝛閬

BT蝳

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

QQ|撠暺撅||52AV璈A

GMT+8, 2024-11-22 10:10 , Processed in 0.055492 second(s), 16 queries .

蝯∠.撱

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

鋆貉憒 敹恍敺 餈銵