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
  1. server{9 o/ r% k( k& v4 @! s0 `; L* h; V. \
  2.   listen 80;) O* M& M: ?; @* h, y
  3.   server_name  tomcat.cncml.com;
    5 ?$ g# b- A$ z# K  X! Y) C
  4.   index  index.php index.html index.htm;
    ) f1 g* x4 w& t4 H" \
  5. * x) z5 ~' |! y8 b* O% f6 o
  6.   location / {
    8 T3 Z; `! E! A/ N  H
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则
    6 Q2 [: j, A1 P
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求; T$ q' D; j: A) E1 c
  9.     proxy_set_header X-Real-IP $remote_addr;
    ) l0 l2 H) m- i
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    % e5 j* B$ Y) q9 M( Q; O! Z
  11.   }
    6 ^, I  S! X) V  N: h& t/ G9 N
  12. }
复制代码
这样访问 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
  1. server{+ V( {0 `6 M: M
  2.   listen 80;
    * C4 Y  o% h, |( D
  3.   server_name  baidu.cncml.com;
    6 W/ M% R( W. x& W9 S
  4.   index  index.php index.html index.htm;4 P: c  N9 C0 e" M2 Y! E) O. r

  5. ! y( B8 J! t, _2 H: q' v
  6.   location / {) w$ ?6 N+ ?" E( ^" W3 [! d
  7.     proxy_pass  http://www.baidu.com;
    , R# O( N/ k# g! b+ J5 b
  8.     proxy_set_header Host $proxy_host;+ f! D; x& U& J9 A
  9.     proxy_set_header X-Real-IP $remote_addr;. ?; ]$ g7 E6 y9 Q. R# |
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # `/ k: O, W( {. |5 o7 X; ]
  11.   }
    " O; M8 e6 O$ |2 d, R
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名: h+ W/ {" C; G4 H5 ~
  1. server{* L# ^; ^# D% @* {9 T1 u
  2.   listen 80;
    + J* q4 b% J7 r  z! B1 f* X' n& E9 W0 {
  3.   server_name 127.0.0.1; # 公网ip9 |  E8 Y$ p4 |( l8 L7 a1 d( |
  4.   index  index.php index.html index.htm;
    ) U. \3 [7 H( n) m% S% B1 E# |

  5. 9 p6 `0 o1 i* [. V
  6.   location / {! `6 E% c. n& U" [, `
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com
    4 c; X! [2 w/ \4 {' @/ x( k" O/ \0 b
  8.     proxy_set_header Host $proxy_host;! y, _6 @5 ~; d
  9.     proxy_set_header X-Real-IP $remote_addr;; `' C0 Y/ m4 K0 @/ i, \8 H
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;2 y8 e3 h0 ?/ i
  11.   }
    3 |, I0 F. I9 P7 C( C
  12. }
复制代码
这样访问 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
  1. server_name cncml.com  Z2 D- ~/ q) E& u' w- F
  2. location /data/ {
    9 ~7 e9 |( i5 s" @( b. m7 c2 H
  3. proxy_pass http://127.0.0.1/;
    ! o6 R5 }# s+ I
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html9 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
  1. server_name cncml.com1 u, s3 w; ?8 W' C3 o
  2. location /data/ {
    ) e( c% v8 I% g/ A& P6 X+ a
  3. proxy_pass http://127.0.0.1;
    1 a% G9 {2 e6 p% Q
  4. }
复制代码
访问 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