您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12826|回复: 0
打印 上一主题 下一主题

[centos] 用Nginx做端口转发(反向代理)

[复制链接]
跳转到指定楼层
楼主
发表于 2020-2-25 05:46:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有时我们会使用一些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" _
  1. server{
    / M& q7 x7 F7 Q) O
  2.   listen 80;
    5 x$ U. w! h, D. [0 b1 J' b/ c( }
  3.   server_name  tomcat.cncml.com;
    " N& a1 h9 H2 b( S3 r& v8 S
  4.   index  index.php index.html index.htm;5 X  M  C8 W2 e
  5. 0 {. P: Z8 d+ @' K
  6.   location / {+ z* e9 l+ Z9 ?* S, q7 Y
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则
    + Q, E! H7 b9 x- b4 n3 h5 a
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求6 P1 K; K$ j  A" h5 r
  9.     proxy_set_header X-Real-IP $remote_addr;$ v3 H6 n3 u  Y  c6 }) k7 K) k7 I
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ X1 {, h( \" O$ ^# R/ m, [
  11.   }
    + {8 \, i6 A: ^5 F! T1 F( t3 c) S
  12. }
复制代码
这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口- J% g( s" \6 S# S
) w: [" ^! F' R) a5 F7 y7 u- L5 ]' _
将域名转发到另一个域名
- [  c: ], j; Z
  1. server{- k: s/ f' L* _
  2.   listen 80;
    9 ~! r' x0 g4 h5 u+ J5 ~
  3.   server_name  baidu.cncml.com;5 F& P' e5 t- R3 |% @  V
  4.   index  index.php index.html index.htm;( {6 Y' R  i3 K  N
  5. ' z" @& N+ r$ u; @) m  Y% i% Y9 U
  6.   location / {  S4 {0 m5 f  I: W% H% x1 S
  7.     proxy_pass  http://www.baidu.com;
    # f" I) \' G/ `6 J+ ~7 ~& O
  8.     proxy_set_header Host $proxy_host;
    5 |1 z7 d; n% ~' ~
  9.     proxy_set_header X-Real-IP $remote_addr;
      k/ w. a( R6 r8 \; ^: Y: ]
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  J$ l9 _. Y4 w& F
  11.   }
    ; o) T4 U8 `- A2 p7 N+ N
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名# d) u, z" h2 p4 O$ X
  1. server{
    * X+ K, p: @) q8 E) h! Q
  2.   listen 80;* @7 x+ `9 ~, k4 z( V& |: b0 d
  3.   server_name 127.0.0.1; # 公网ip
    $ c9 N+ M  m" ~8 X- ^
  4.   index  index.php index.html index.htm;
    ' c( |5 t: s, P9 F- X3 C- s

  5.   U6 s2 I% N0 `! q- T
  6.   location / {$ Q: [8 ]$ H7 N( y. Y! {
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com9 N/ ^; s: s5 @; G# [
  8.     proxy_set_header Host $proxy_host;
    / I1 i- {3 g* N5 [: h$ I! p, K
  9.     proxy_set_header X-Real-IP $remote_addr;
    ! Q: t9 [  [2 {( ], C
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;* v7 S6 I% a5 o6 F* p8 ]7 r
  11.   }8 r3 S& z: J( y) h. C% N$ t( d" ?
  12. }
复制代码
这样访问 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 _
  1. server_name cncml.com0 U3 ^* K4 b, ~: ~/ W: ]: E
  2. location /data/ {4 j& Z3 B4 C8 N# n( e
  3. proxy_pass http://127.0.0.1/;+ r" }# _" Z7 w9 I6 ?# t" |
  4. }
复制代码
访问 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
  1. server_name cncml.com( Z' K  n9 R, p# L4 w- F1 S
  2. location /data/ {! ^0 B; G& F6 C
  3. proxy_pass http://127.0.0.1;0 t- V; R  p+ m4 H) c; C
  4. }
复制代码
访问 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 w
游客,如果您要查看本帖隐藏内容请回复
2 Q+ @- L% {$ Y( S- E+ I
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 14:30 , Processed in 0.056595 second(s), 19 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!