管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发
2 m1 f; P/ ?+ |' N! J+ R# }
1 e5 a9 `4 f1 s5 q! C本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
( B4 b: R' r& @" ?
1 Y2 ]" Y. f* C3 e- h将域名转发到本地端口. e9 P( G5 `, C
首先介绍最常用的,将域名转发到本地另一个端口上! V/ g- d6 j% ]
- server{
% b( q/ y" ]. F, O - listen 80;6 R" Y) N' s# }6 b7 k7 }/ \+ U
- server_name tomcat.cncml.com;
: g+ Q! t) f$ d0 p - index index.php index.html index.htm;
0 M3 L) m3 i e5 o) F& B
' v b8 v: Z3 u6 t6 ~) D, L% T4 m- location / {" R! J) T; w$ K: \
- proxy_pass http://127.0.0.1:8080; # 转发规则5 y7 [0 }2 Q$ P/ K% m/ M9 E
- proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
; C) f6 J( ^( Y4 G - proxy_set_header X-Real-IP $remote_addr;9 w0 K$ W* G" R3 [
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; P1 g( B5 [2 ]6 z' c5 f
- }
6 f1 A1 M/ {$ o n# Y1 e+ l - }
复制代码 这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口( c! ^( ~- r2 ^, ^
$ d/ Q( r. ^6 k3 h F z& u
将域名转发到另一个域名9 i) K. d$ A, U: Q6 v+ H
- server{
# T) J" h+ D1 }' }% K L - listen 80;9 {8 g# c$ W% @4 g
- server_name baidu.cncml.com;5 H! v n; Y- M! h4 \' Z9 k$ X
- index index.php index.html index.htm;/ n% D$ x( v1 |" q0 I P/ }9 ]( g
9 d4 \. E& i" e$ U3 _- location / {
2 t4 z' Z( J+ a2 n1 X7 e - proxy_pass http://www.baidu.com;, i2 ~- i: d# P7 h7 w
- proxy_set_header Host $proxy_host;
& C" P! r; t, d - proxy_set_header X-Real-IP $remote_addr;0 x8 m! l4 E6 i0 x
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- C9 b }, O* o; e
- }
8 {- F1 J! o- S, K - }
复制代码 本地一个端口转发到另一个端口或另一个域名! b" ]2 j V& o" l; o& s, \
- server{; E" M+ U) W$ e) f/ S; W
- listen 80;
9 h5 B& P G" V( o" p - server_name 127.0.0.1; # 公网ip1 O0 F! j/ E9 A2 X
- index index.php index.html index.htm;5 U4 P5 ?6 g' y
- , @9 f1 w/ m# f3 w0 S
- location / {1 U! g, R' O* x. Q( Y/ F2 I1 D
- proxy_pass http://127.0.0.1:8080; # 或 http://www.baidu.com
7 y" j/ v; d' O$ K2 ] - proxy_set_header Host $proxy_host;
) }1 h' q: w1 k - proxy_set_header X-Real-IP $remote_addr;! A o9 K+ ?+ p6 G! s5 E( }
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- N) L9 a! ~+ ?3 { - }
; n( ^0 f+ |+ S# M5 E( `1 Z" l1 b! X2 m+ R - }
复制代码 这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
; ?: H# f4 d' ~5 G+ O3 v$ @ \- g7 s }1 ^
加 / 与不加 /$ H. \* T. l! T
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径" Y+ i9 B! _0 }/ B% x- j1 l
" R4 m0 e- W% |6 a) r
例如
* j8 w# O3 S& ?4 r
9 D7 ^$ h" m, s) V0 W6 [7 \ A加 /
V/ q7 x# g# p. v6 l- server_name cncml.com
B: N& t, k2 C - location /data/ {* j. P0 o0 x8 ` x/ A$ v {7 s3 K
- proxy_pass http://127.0.0.1/;* m1 H+ H! W/ y& F4 x2 X" V
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html4 q1 p% c. s: ?8 \: h
% s$ O6 n, l3 g% ]% p
不加 /# {4 G% A0 Y4 G' P
- server_name cncml.com
5 a8 b! N+ W9 p& t - location /data/ {
0 X* k7 g( R7 e9 w3 b! b! o8 i" v - proxy_pass http://127.0.0.1;" n4 f7 p: d- A4 E9 h# g9 P
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html: P2 x# I7 w# `# d# W4 A1 ?$ r
( @# K! s% L5 y) r d. h& s% ]
V; x+ _1 b" q4 D; W- X
|
|