管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发4 c# o$ @1 r1 @6 Z8 n
7 X n8 `$ y7 g本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
0 T; n* d6 L: n. Y
4 p* |" c, X# x3 H% a$ h将域名转发到本地端口' G1 O3 L! Y" @) j4 D- B
首先介绍最常用的,将域名转发到本地另一个端口上' s8 L' F0 r* R! Z" _
- server{
/ M& q7 x7 F7 Q) O - listen 80;
5 x$ U. w! h, D. [0 b1 J' b/ c( } - server_name tomcat.cncml.com;
" N& a1 h9 H2 b( S3 r& v8 S - index index.php index.html index.htm;5 X M C8 W2 e
- 0 {. P: Z8 d+ @' K
- location / {+ z* e9 l+ Z9 ?* S, q7 Y
- proxy_pass http://127.0.0.1:8080; # 转发规则
+ Q, E! H7 b9 x- b4 n3 h5 a - proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求6 P1 K; K$ j A" h5 r
- proxy_set_header X-Real-IP $remote_addr;$ v3 H6 n3 u Y c6 }) k7 K) k7 I
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ X1 {, h( \" O$ ^# R/ m, [
- }
+ {8 \, i6 A: ^5 F! T1 F( t3 c) S - }
复制代码 这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口- J% g( s" \6 S# S
) w: [" ^! F' R) a5 F7 y7 u- L5 ]' _
将域名转发到另一个域名
- [ c: ], j; Z- server{- k: s/ f' L* _
- listen 80;
9 ~! r' x0 g4 h5 u+ J5 ~ - server_name baidu.cncml.com;5 F& P' e5 t- R3 |% @ V
- index index.php index.html index.htm;( {6 Y' R i3 K N
- ' z" @& N+ r$ u; @) m Y% i% Y9 U
- location / { S4 {0 m5 f I: W% H% x1 S
- proxy_pass http://www.baidu.com;
# f" I) \' G/ `6 J+ ~7 ~& O - proxy_set_header Host $proxy_host;
5 |1 z7 d; n% ~' ~ - proxy_set_header X-Real-IP $remote_addr;
k/ w. a( R6 r8 \; ^: Y: ] - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; J$ l9 _. Y4 w& F
- }
; o) T4 U8 `- A2 p7 N+ N - }
复制代码 本地一个端口转发到另一个端口或另一个域名# d) u, z" h2 p4 O$ X
- server{
* X+ K, p: @) q8 E) h! Q - listen 80;* @7 x+ `9 ~, k4 z( V& |: b0 d
- server_name 127.0.0.1; # 公网ip
$ c9 N+ M m" ~8 X- ^ - index index.php index.html index.htm;
' c( |5 t: s, P9 F- X3 C- s
U6 s2 I% N0 `! q- T- location / {$ Q: [8 ]$ H7 N( y. Y! {
- proxy_pass http://127.0.0.1:8080; # 或 http://www.baidu.com9 N/ ^; s: s5 @; G# [
- proxy_set_header Host $proxy_host;
/ I1 i- {3 g* N5 [: h$ I! p, K - proxy_set_header X-Real-IP $remote_addr;
! Q: t9 [ [2 {( ], C - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* v7 S6 I% a5 o6 F* p8 ]7 r
- }8 r3 S& z: J( y) h. C% N$ t( d" ?
- }
复制代码 这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
+ E' s2 d' j6 e# L+ r2 E
0 P4 h9 H" r. p! O0 c0 R9 o加 / 与不加 /
$ p( P# b! @1 P在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
5 ]# y" ~$ X4 U3 H c# k2 `: ?
6 J% E' `2 i, ^+ e# K例如
* f9 Z7 t" p1 w; R0 n* W5 D# T5 n2 U! e2 N
加 /6 B: N, e3 m$ V4 M2 u2 K4 _
- server_name cncml.com0 U3 ^* K4 b, ~: ~/ W: ]: E
- location /data/ {4 j& Z3 B4 C8 N# n( e
- proxy_pass http://127.0.0.1/;+ r" }# _" Z7 w9 I6 ?# t" |
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html; t) R3 u% ]8 f; ?0 R0 s: t3 C6 s
& |( i+ ^. u: G" d不加 /
# [* D, A5 L+ g4 R- i6 {( g1 x- server_name cncml.com( Z' K n9 R, p# L4 w- F1 S
- location /data/ {! ^0 B; G& F6 C
- proxy_pass http://127.0.0.1;0 t- V; R p+ m4 H) c; C
- }
复制代码 访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html
& l5 P1 k) P; t# d7 y
& F* H; k0 S9 o+ y! G: S4 w2 Q+ @- L% {$ Y( S- E+ I
|
|