cncml手绘网
标题:
用Nginx做端口转发(反向代理)
[打印本页]
作者:
admin
时间:
2020-2-25 05:46
标题:
用Nginx做端口转发(反向代理)
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发
, v W' A' { L$ x
, U x: {7 L+ m: B% w- M% c
本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
& m( x8 l9 s) n. N
% s, S" D1 Q+ G
将域名转发到本地端口
* j+ n5 W8 S2 f' G' s
首先介绍最常用的,将域名转发到本地另一个端口上
: T7 `4 S' z6 ?, N5 V
server{
9 o/ r% k( k& v4 @! s0 `; L* h; V. \
listen 80;
) O* M& M: ?; @* h, y
server_name tomcat.cncml.com;
5 ?$ g# b- A$ z# K X! Y) C
index index.php index.html index.htm;
) f1 g* x4 w& t4 H" \
* x) z5 ~' |! y8 b* O% f6 o
location / {
8 T3 Z; `! E! A/ N H
proxy_pass http://127.0.0.1:8080; # 转发规则
6 Q2 [: j, A1 P
proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
; T$ q' D; j: A) E1 c
proxy_set_header X-Real-IP $remote_addr;
) l0 l2 H) m- i
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
% e5 j* B$ Y) q9 M( Q; O! Z
}
6 ^, I S! X) V N: h& t/ G9 N
}
复制代码
这样访问
http://tomcat.cncml.com
时就会转发到本地的 8080 端口
: t# ]6 k# f. s7 T1 L- D
9 a3 R1 x2 I# d' C4 f
将域名转发到另一个域名
3 Q8 |; ^- d' N7 w, N# O" G
server{
+ V( {0 `6 M: M
listen 80;
* C4 Y o% h, |( D
server_name baidu.cncml.com;
6 W/ M% R( W. x& W9 S
index index.php index.html index.htm;
4 P: c N9 C0 e" M2 Y! E) O. r
! y( B8 J! t, _2 H: q' v
location / {
) w$ ?6 N+ ?" E( ^" W3 [! d
proxy_pass http://www.baidu.com;
, R# O( N/ k# g! b+ J5 b
proxy_set_header Host $proxy_host;
+ f! D; x& U& J9 A
proxy_set_header X-Real-IP $remote_addr;
. ?; ]$ g7 E6 y9 Q. R# |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# `/ k: O, W( {. |5 o7 X; ]
}
" O; M8 e6 O$ |2 d, R
}
复制代码
[size=0.6]这样访问
http://baidu.cncml.com
时就会转发到
http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名
: h+ W/ {" C; G4 H5 ~
server{
* L# ^; ^# D% @* {9 T1 u
listen 80;
+ J* q4 b% J7 r z! B1 f* X' n& E9 W0 {
server_name 127.0.0.1; # 公网ip
9 | E8 Y$ p4 |( l8 L7 a1 d( |
index index.php index.html index.htm;
) U. \3 [7 H( n) m% S% B1 E# |
9 p6 `0 o1 i* [. V
location / {
! `6 E% c. n& U" [, `
proxy_pass http://127.0.0.1:8080; # 或 http://www.baidu.com
4 c; X! [2 w/ \4 {' @/ x( k" O/ \0 b
proxy_set_header Host $proxy_host;
! y, _6 @5 ~; d
proxy_set_header X-Real-IP $remote_addr;
; `' C0 Y/ m4 K0 @/ i, \8 H
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2 y8 e3 h0 ?/ i
}
3 |, I0 F. I9 P7 C( C
}
复制代码
这样访问
http://127.0.0.1
时就会转发到本地的 8080 端口或
http://www.baidu.com
) N$ H+ f) r N8 r
5 B" S5 [3 v9 I9 q- y7 i- ^
加 / 与不加 /
9 u# w' D, w, D; h3 W
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
- G. ~' Q x& H
; ~0 {7 X+ ~6 h( L h
例如
4 s' W8 c' C O4 @% c3 b/ T3 t/ ~, R, _
$ ~. Q, s* r4 ]+ R! w. K& i/ F% Y
加 /
) A# q R7 Q5 }: O
server_name cncml.com
Z2 D- ~/ q) E& u' w- F
location /data/ {
9 ~7 e9 |( i5 s" @( b. m7 c2 H
proxy_pass http://127.0.0.1/;
! o6 R5 }# s+ I
}
复制代码
访问
http://cncml.com/data/index.html
会转发到
http://127.0.0.1/index.html
9 c6 z( c; c& p$ w) d Y1 j Y$ _* E
7 J! z1 X3 Q) f: O$ o8 P3 K
不加 /
9 o* m& d& @! v& b" M' m
server_name cncml.com
1 u, s3 w; ?8 W' C3 o
location /data/ {
) e( c% v8 I% g/ A& P6 X+ a
proxy_pass http://127.0.0.1;
1 a% G9 {2 e6 p% Q
}
复制代码
访问
http://cncml.com/data/index.html
会转发到
http://127.0.0.1/data/index.html
) W. ~. d+ H' r9 E
8 U1 z) d. c c; Q- i+ f' j
% w- T9 p8 ^) D! W
欢迎光临 cncml手绘网 (http://bbs.cncml.com/)
Powered by Discuz! X3.2