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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2020-2-25 05:46:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发( Y# a  `' U+ q, }5 i( R+ U
' g( e9 V9 W- |
本文中,我们介绍Nginx如何做端口转发,还有各种转发规则
$ Y7 K& J8 e3 ]! m8 G
' l$ F# d* I" C: m5 Z将域名转发到本地端口& y& ~5 a7 l! U$ q7 e+ d* w
首先介绍最常用的,将域名转发到本地另一个端口上
- }8 u6 W4 o0 D0 t; J1 U
  1. server{8 u8 s" C9 e' n1 g* S' T, J
  2.   listen 80;2 X! J) {3 ]% ]8 n  E. F
  3.   server_name  tomcat.cncml.com;
    1 ?( \* u" }, h* r, k2 E* k
  4.   index  index.php index.html index.htm;
    7 `% b* ?, N* m0 V/ }  v

  5. 9 z: o% X- H+ S6 X
  6.   location / {
    - u" T6 |* V! J& T5 Z8 ~* ]
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则5 K4 \2 h' z2 S' V# U  O' Y
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求* }. u3 y4 a5 Q
  9.     proxy_set_header X-Real-IP $remote_addr;1 w2 C' H' d4 C# ]7 S$ y
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 U* b1 A9 j% K5 h  {
  11.   }
    8 J, m6 F: m+ ~# L% [
  12. }
复制代码
这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口1 t" l- _! \  ]& l$ J
1 Y7 W" t7 `& P* b1 f
将域名转发到另一个域名9 o8 @& J$ q" n- ?
  1. server{- m7 ^0 T2 C9 _) x4 a
  2.   listen 80;% r6 p: ?9 z2 V
  3.   server_name  baidu.cncml.com;
    9 k0 K0 k0 |( |2 L7 D- n
  4.   index  index.php index.html index.htm;' O+ o5 f, w/ G+ \- d- j. F) y4 K: r
  5. & w% N& d2 p, h9 z  D+ T9 F
  6.   location / {% A2 j5 r3 a2 s* @) C" W7 T; |6 g
  7.     proxy_pass  http://www.baidu.com;
    ; l0 L9 Q+ N' E1 n9 |5 _6 b& F; m$ I0 B! y
  8.     proxy_set_header Host $proxy_host;4 }- X8 O0 U2 }6 ~0 S/ k
  9.     proxy_set_header X-Real-IP $remote_addr;
    " V0 Z0 T8 P$ G# r* R  H
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    + `$ V8 U" v7 U+ ^& P) O$ M
  11.   }4 _# b+ e( a5 d. ~) M$ b3 T' Q
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名$ g" a9 ], t  b" h
  1. server{
    " S$ t' ?' q% b& b3 F/ z; z. M( y
  2.   listen 80;
    , u& f& F& {1 Y
  3.   server_name 127.0.0.1; # 公网ip% P1 Z8 Y3 D# P
  4.   index  index.php index.html index.htm;
    $ Q! [" @' H  @/ ^, ]  \- w, Y

  5. 7 f+ @7 q% t. l9 G
  6.   location / {9 K! R- a5 k$ o
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com
    ( X) g# I5 w9 Z0 i" L1 W' e
  8.     proxy_set_header Host $proxy_host;
    0 t- O  x) H+ a1 Z5 h& |6 a
  9.     proxy_set_header X-Real-IP $remote_addr;( X9 B3 C1 r5 f" \& q: u
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    % \3 X3 U; T2 v' i8 ?, [
  11.   }% q# M7 L6 K/ _0 k
  12. }
复制代码
这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com
, x  J  G) G0 j% S0 x* V
' s( H( c) |" t1 }/ ~  o加 / 与不加 /
- E7 }# S' k8 ^' S+ Y# @$ }. J5 T在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径$ m! [  U, U5 {! Y3 t

. [  {" V& n6 q4 b% B例如2 e+ S: b5 [) C6 b
9 _! d' D6 m( @5 p! w
加 /
. @" o. U6 p" g( v
  1. server_name cncml.com
    , d5 Z  M! L2 o( h* v' T  K
  2. location /data/ {
      R6 n: ~% j) J) V
  3. proxy_pass http://127.0.0.1/;  Q9 l( x0 j9 _2 s) h7 A* I
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html9 D; z6 }$ p. j2 K( V

5 `4 f1 I1 t9 J5 h, p不加 /6 e) u1 X1 {- f8 L: G
  1. server_name cncml.com" a9 ~0 x" y5 \. T/ F8 a
  2. location /data/ {
    2 D& m9 k( D/ G, ^9 T5 |( z
  3. proxy_pass http://127.0.0.1;
    ( U9 X" u+ F6 @9 W5 F0 `
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html8 T& R/ Y$ P  y1 s
- y* P0 X; c, r( g" O
游客,如果您要查看本帖隐藏内容请回复

" a+ D/ B* \3 D; p  C& V$ o! K8 f. ~
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 17:19 , Processed in 0.059644 second(s), 19 queries .

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