管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发( Y# a `' U+ q, }5 i( R+ U
' g( e9 V9 W- |
本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
$ Y7 K& J8 e3 ]! m8 G
' l$ F# d* I" C: m5 Z将域名转发到本地端口& y& ~5 a7 l! U$ q7 e+ d* w
首先介绍最常用的,将域名转发到本地另一个端口上
- }8 u6 W4 o0 D0 t; J1 U- server{8 u8 s" C9 e' n1 g* S' T, J
- listen 80;2 X! J) {3 ]% ]8 n E. F
- server_name tomcat.cncml.com;
1 ?( \* u" }, h* r, k2 E* k - index index.php index.html index.htm;
7 `% b* ?, N* m0 V/ } v
9 z: o% X- H+ S6 X- location / {
- u" T6 |* V! J& T5 Z8 ~* ] - proxy_pass http://127.0.0.1:8080; # 转发规则5 K4 \2 h' z2 S' V# U O' Y
- proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求* }. u3 y4 a5 Q
- proxy_set_header X-Real-IP $remote_addr;1 w2 C' H' d4 C# ]7 S$ y
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 U* b1 A9 j% K5 h {
- }
8 J, m6 F: m+ ~# L% [ - }
复制代码 这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口1 t" l- _! \ ]& l$ J
1 Y7 W" t7 `& P* b1 f
将域名转发到另一个域名9 o8 @& J$ q" n- ?
- server{- m7 ^0 T2 C9 _) x4 a
- listen 80;% r6 p: ?9 z2 V
- server_name baidu.cncml.com;
9 k0 K0 k0 |( |2 L7 D- n - index index.php index.html index.htm;' O+ o5 f, w/ G+ \- d- j. F) y4 K: r
- & w% N& d2 p, h9 z D+ T9 F
- location / {% A2 j5 r3 a2 s* @) C" W7 T; |6 g
- proxy_pass http://www.baidu.com;
; l0 L9 Q+ N' E1 n9 |5 _6 b& F; m$ I0 B! y - proxy_set_header Host $proxy_host;4 }- X8 O0 U2 }6 ~0 S/ k
- proxy_set_header X-Real-IP $remote_addr;
" V0 Z0 T8 P$ G# r* R H - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ `$ V8 U" v7 U+ ^& P) O$ M - }4 _# b+ e( a5 d. ~) M$ b3 T' Q
- }
复制代码 本地一个端口转发到另一个端口或另一个域名$ g" a9 ], t b" h
- server{
" S$ t' ?' q% b& b3 F/ z; z. M( y - listen 80;
, u& f& F& {1 Y - server_name 127.0.0.1; # 公网ip% P1 Z8 Y3 D# P
- index index.php index.html index.htm;
$ Q! [" @' H @/ ^, ] \- w, Y
7 f+ @7 q% t. l9 G- location / {9 K! R- a5 k$ o
- proxy_pass http://127.0.0.1:8080; # 或 http://www.baidu.com
( X) g# I5 w9 Z0 i" L1 W' e - proxy_set_header Host $proxy_host;
0 t- O x) H+ a1 Z5 h& |6 a - proxy_set_header X-Real-IP $remote_addr;( X9 B3 C1 r5 f" \& q: u
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% \3 X3 U; T2 v' i8 ?, [ - }% q# M7 L6 K/ _0 k
- }
复制代码 这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
, x J G) G0 j% S0 x* V
' s( H( c) |" t1 }/ ~ o加 / 与不加 /
- E7 }# S' k8 ^' S+ Y# @$ }. J5 T在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径$ m! [ U, U5 {! Y3 t
. [ {" V& n6 q4 b% B例如2 e+ S: b5 [) C6 b
9 _! d' D6 m( @5 p! w
加 /
. @" o. U6 p" g( v- server_name cncml.com
, d5 Z M! L2 o( h* v' T K - location /data/ {
R6 n: ~% j) J) V - proxy_pass http://127.0.0.1/; Q9 l( x0 j9 _2 s) h7 A* I
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html9 D; z6 }$ p. j2 K( V
5 `4 f1 I1 t9 J5 h, p不加 /6 e) u1 X1 {- f8 L: G
- server_name cncml.com" a9 ~0 x" y5 \. T/ F8 a
- location /data/ {
2 D& m9 k( D/ G, ^9 T5 |( z - proxy_pass http://127.0.0.1;
( U9 X" u+ F6 @9 W5 F0 ` - }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html8 T& R/ Y$ P y1 s
- y* P0 X; c, r( g" O
" a+ D/ B* \3 D; p C& V$ o! K8 f. ~ |
|