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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2020-2-25 05:46:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发
2 m1 f; P/ ?+ |' N! J+ R# }
1 e5 a9 `4 f1 s5 q! C本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
( B4 b: R' r& @" ?
1 Y2 ]" Y. f* C3 e- h将域名转发到本地端口. e9 P( G5 `, C
首先介绍最常用的,将域名转发到本地另一个端口上! V/ g- d6 j% ]
  1. server{
    % b( q/ y" ]. F, O
  2.   listen 80;6 R" Y) N' s# }6 b7 k7 }/ \+ U
  3.   server_name  tomcat.cncml.com;
    : g+ Q! t) f$ d0 p
  4.   index  index.php index.html index.htm;
    0 M3 L) m3 i  e5 o) F& B

  5. ' v  b8 v: Z3 u6 t6 ~) D, L% T4 m
  6.   location / {" R! J) T; w$ K: \
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则5 y7 [0 }2 Q$ P/ K% m/ M9 E
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
    ; C) f6 J( ^( Y4 G
  9.     proxy_set_header X-Real-IP $remote_addr;9 w0 K$ W* G" R3 [
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; P1 g( B5 [2 ]6 z' c5 f
  11.   }
    6 f1 A1 M/ {$ o  n# Y1 e+ l
  12. }
复制代码
这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口( c! ^( ~- r2 ^, ^
$ d/ Q( r. ^6 k3 h  F  z& u
将域名转发到另一个域名9 i) K. d$ A, U: Q6 v+ H
  1. server{
    # T) J" h+ D1 }' }% K  L
  2.   listen 80;9 {8 g# c$ W% @4 g
  3.   server_name  baidu.cncml.com;5 H! v  n; Y- M! h4 \' Z9 k$ X
  4.   index  index.php index.html index.htm;/ n% D$ x( v1 |" q0 I  P/ }9 ]( g

  5. 9 d4 \. E& i" e$ U3 _
  6.   location / {
    2 t4 z' Z( J+ a2 n1 X7 e
  7.     proxy_pass  http://www.baidu.com;, i2 ~- i: d# P7 h7 w
  8.     proxy_set_header Host $proxy_host;
    & C" P! r; t, d
  9.     proxy_set_header X-Real-IP $remote_addr;0 x8 m! l4 E6 i0 x
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;- C9 b  }, O* o; e
  11.   }
    8 {- F1 J! o- S, K
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名! b" ]2 j  V& o" l; o& s, \
  1. server{; E" M+ U) W$ e) f/ S; W
  2.   listen 80;
    9 h5 B& P  G" V( o" p
  3.   server_name 127.0.0.1; # 公网ip1 O0 F! j/ E9 A2 X
  4.   index  index.php index.html index.htm;5 U4 P5 ?6 g' y
  5. , @9 f1 w/ m# f3 w0 S
  6.   location / {1 U! g, R' O* x. Q( Y/ F2 I1 D
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com
    7 y" j/ v; d' O$ K2 ]
  8.     proxy_set_header Host $proxy_host;
    ) }1 h' q: w1 k
  9.     proxy_set_header X-Real-IP $remote_addr;! A  o9 K+ ?+ p6 G! s5 E( }
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    - N) L9 a! ~+ ?3 {
  11.   }
    ; n( ^0 f+ |+ S# M5 E( `1 Z" l1 b! X2 m+ R
  12. }
复制代码
这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
; ?: H# f4 d' ~5 G+ O3 v$ @  \- g7 s  }1 ^
加 / 与不加 /$ H. \* T. l! T
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径" Y+ i9 B! _0 }/ B% x- j1 l
" R4 m0 e- W% |6 a) r
例如
* j8 w# O3 S& ?4 r
9 D7 ^$ h" m, s) V0 W6 [7 \  A加 /
  V/ q7 x# g# p. v6 l
  1. server_name cncml.com
      B: N& t, k2 C
  2. location /data/ {* j. P0 o0 x8 `  x/ A$ v  {7 s3 K
  3. proxy_pass http://127.0.0.1/;* m1 H+ H! W/ y& F4 x2 X" V
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html4 q1 p% c. s: ?8 \: h
% s$ O6 n, l3 g% ]% p
不加 /# {4 G% A0 Y4 G' P
  1. server_name cncml.com
    5 a8 b! N+ W9 p& t
  2. location /data/ {
    0 X* k7 g( R7 e9 w3 b! b! o8 i" v
  3. proxy_pass http://127.0.0.1;" n4 f7 p: d- A4 E9 h# g9 P
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html: P2 x# I7 w# `# d# W4 A1 ?$ r
( @# K! s% L5 y) r  d. h& s% ]
游客,如果您要查看本帖隐藏内容请回复
  V; x+ _1 b" q4 D; W- X
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 15:55 , Processed in 0.054718 second(s), 20 queries .

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