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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2020-2-25 05:46:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发+ ]1 {# p* W7 a, ?$ N& x& I+ ~, }) Y

  _1 p/ z+ x7 J) m, ?$ O/ C" }本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
2 m0 R* v0 L, x6 e% r; z0 j5 x0 h$ A* o, M
将域名转发到本地端口
2 w6 T# s  Q3 q2 t, ~" ^首先介绍最常用的,将域名转发到本地另一个端口上. Y' ^8 c% b# I* K  ]
  1. server{' g9 e3 ^' z& I, u! Z
  2.   listen 80;
    . t) A- `8 a* M6 W# x7 A- H
  3.   server_name  tomcat.cncml.com;3 B- Z% \" ?; b" j
  4.   index  index.php index.html index.htm;
    * K' @/ |8 u+ i7 M9 G
  5. " k7 B( s2 ~. u  C9 `3 Y7 F
  6.   location / {! b: d& }) \9 E! o$ d. L
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则
    3 Q3 |) d* e( w9 o
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求6 V/ I" R, {( R* @6 }9 w
  9.     proxy_set_header X-Real-IP $remote_addr;9 K% K( @9 V3 H* w
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, Y# Y4 r( C7 o: |$ Z* w7 K, [
  11.   }: A2 e% K# R# P  l
  12. }
复制代码
这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口- w  Q4 Y, |# ]
6 E7 c) K8 F% \4 n
将域名转发到另一个域名
" }3 Z/ F4 N. l9 R! ~" b. D
  1. server{
    4 N8 s2 a3 m; q) B0 W
  2.   listen 80;
    " c! f( @" R- V
  3.   server_name  baidu.cncml.com;& p! Y) i! h( h- H
  4.   index  index.php index.html index.htm;8 E- j5 V5 c6 g
  5. % [! H% G1 a. F1 z$ S+ [
  6.   location / {
    * }( w8 ^, V8 A' K
  7.     proxy_pass  http://www.baidu.com;1 e0 U: B  @: Z8 ]+ y% o+ v
  8.     proxy_set_header Host $proxy_host;
    1 R* W6 c0 R- x+ F' Z+ Y
  9.     proxy_set_header X-Real-IP $remote_addr;, r  U/ e# Q! a% q9 b# _4 {" c, Y2 Q0 g
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; I) n" v4 T4 t+ i1 a; V- n% p
  11.   }! R4 J6 K$ Q: K
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名& V3 c1 V  U+ X: C4 |
  1. server{
    % I  U2 m! O' e/ ^3 @; `& |5 g
  2.   listen 80;' l7 o) v; ~: H7 E6 S) @. O
  3.   server_name 127.0.0.1; # 公网ip1 Y# H& O' n, D+ v
  4.   index  index.php index.html index.htm;
    4 {& i! G; s: v+ o* G! c

  5. ; N5 f5 l% t3 U
  6.   location / {& B+ V& f/ @8 _. z8 X7 p
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com
    ! n+ c5 r5 Q+ Y1 \3 C
  8.     proxy_set_header Host $proxy_host;& T  {8 `' K/ d2 c
  9.     proxy_set_header X-Real-IP $remote_addr;5 ?7 q& E& ~1 e
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" G( Y* j5 b* _& w0 A) \
  11.   }
    2 ?* L- z* U/ L' [& ?+ {4 g0 F
  12. }
复制代码
这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com& l, \1 d" C4 g7 M* s1 c

0 Z! K% Y! z) Z5 c' l" k( K加 / 与不加 /0 U* H& k8 n1 ^
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径
" h# f$ j7 s( V, G' j  K8 _4 [% S% e8 D& K, a$ [5 r
例如  a: L& \9 N! I* d

$ y0 d/ x# j/ ]4 b9 _8 w* {- Q加 /
' g" c) `- l( E# z$ |
  1. server_name cncml.com
    & ^2 C$ W; s% I1 b$ {/ c
  2. location /data/ {, Z" Y7 N6 z5 W
  3. proxy_pass http://127.0.0.1/;
    : O0 _5 D$ U4 l8 B8 S1 g( Q( }
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html/ f% `' W$ ]" ~# @
- O. l7 }8 @, ^9 b, I; x* w
不加 // q4 u6 S2 K! R: _/ _
  1. server_name cncml.com/ d1 M  O7 Z  U
  2. location /data/ {, c# V/ ~0 E4 O" _1 y0 {# J
  3. proxy_pass http://127.0.0.1;4 Z! d9 d3 T/ x0 e' y
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html3 L$ M6 j4 E4 G% g' e+ o
/ N9 Y" Q& W1 ?# d0 O+ L
游客,如果您要查看本帖隐藏内容请回复

+ n4 ?1 @" C! Z4 V+ u( w' W% ^
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 12:22 , Processed in 0.053175 second(s), 19 queries .

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