管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发
& x; D5 {# ^! L% g3 C
. `' i' ]- `' p% j本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
* C# p+ A; Q, O% O1 P9 H5 I2 |! `+ r7 e6 F2 m+ L& m1 `
将域名转发到本地端口 B2 n' G; [; c. A
首先介绍最常用的,将域名转发到本地另一个端口上
& M8 y; Q, X+ t- server{
) N# G; |) X3 l) }0 Q t2 b - listen 80;7 J" ~+ K0 y: J3 H; Y
- server_name tomcat.cncml.com;
% e0 G. K n+ ^% { - index index.php index.html index.htm;
! S/ g+ A( O' Y/ t w
( P& f }$ R1 B- location / {( f- u3 A) E% O2 ~/ ~/ {/ Z
- proxy_pass http://127.0.0.1:8080; # 转发规则9 H9 S+ q6 n2 D- B+ U! c4 p/ D6 P
- proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
5 f: v V l8 b L; M, v. T - proxy_set_header X-Real-IP $remote_addr;
& x, b3 ~* I w% G0 A, J3 f; f4 _ - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ @3 P) _& J8 ]/ F9 ~$ N+ G: V
- }
4 f" w1 d) ?& o" e - }
复制代码 这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口. g. {. p1 j4 T
. l( Y: U6 }0 b7 A! x2 b将域名转发到另一个域名
8 C( j/ X! j2 S+ D/ {- server{3 `& w6 J2 M+ G! P6 {* I
- listen 80;& g9 d0 r, q( L( c- T
- server_name baidu.cncml.com;; T7 t ^8 Z1 [# ?. g ?, n
- index index.php index.html index.htm;
6 Z3 g3 E% i$ v: f, R
" ?1 O) c: C- D. e. B- location / {
4 D+ A* f$ W; S1 B - proxy_pass http://www.baidu.com;
, V5 w! N& p, M: w - proxy_set_header Host $proxy_host;
- v8 K) @; C4 O - proxy_set_header X-Real-IP $remote_addr;
u1 B. @2 k0 `" i& M K - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" | s$ G! T# _, K - }6 h* B- U% L) N9 W4 S
- }
复制代码 本地一个端口转发到另一个端口或另一个域名
. r1 r* }9 a/ z$ c/ D( p- {- ?. ?- server{
+ d( ^$ g" b6 }# ~ - listen 80;5 l, U% I! T# h8 G( `. v
- server_name 127.0.0.1; # 公网ip/ A9 Y3 [' [0 W. G `$ ^! Q
- index index.php index.html index.htm;
9 ~' }+ r2 D) v1 a% {
! g( @2 [9 W: {1 q/ Z- location / {& J/ X7 g) N" j
- proxy_pass http://127.0.0.1:8080; # 或 http://www.baidu.com b8 x7 K/ k, a. O
- proxy_set_header Host $proxy_host;
6 _; ]5 _* r( s& ^. z1 \& J0 R - proxy_set_header X-Real-IP $remote_addr;/ v2 l, k1 @; L/ M+ Y# Q8 h/ t
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;( N- {' x6 \, N+ V& \
- }
( S6 N, @$ h4 N; d+ f - }
复制代码 这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com* a$ B+ {3 x7 x: \! o/ R
0 m8 d/ U9 ]# x9 a% ]4 i加 / 与不加 /6 T' l ]) S7 C( ?7 L
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
6 `! Y8 \2 V. ]; M% s/ S6 j7 G. R; s) S5 S7 t
例如
! ^. Y! y, { S9 e1 D2 l3 s& f* F+ S# K
加 /7 i; r. @ H' p9 I
- server_name cncml.com% t* l0 b8 o: U9 X; s7 x
- location /data/ {5 X7 c# }: Z6 t8 Z3 @& b& r- }4 {
- proxy_pass http://127.0.0.1/;. }3 n% W8 K# {! A9 `. J" ~; N+ ?
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html
; M: s: w7 d7 K. G
9 q! t. a9 T- R* `不加 /
3 m3 y9 Q5 Q. q' Q1 t- server_name cncml.com
5 x/ z- [; @, P5 e- d" G - location /data/ {6 Y# X) @- |: R5 H! Q
- proxy_pass http://127.0.0.1; m) Y: ?& y4 A/ V
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html
6 W5 L7 u. I A: ^2 Y A i9 X+ x: V! }; B. \. }. p
2 e+ o( K% [) | _5 d7 D* Q* s
|
|