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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2020-2-25 05:46:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有时我们会使用一些java或node应用,但又不想让他们直接监听80端口,这时就需要用到端口转发9 t$ @, {, f. O1 Y( {
: h$ k8 k6 k. G5 m5 t+ N
本文中,我们介绍Nginx如何做端口转发,还有各种转发规则  Q  ^3 o0 l# I

, [1 W* l( U8 C/ y7 p将域名转发到本地端口% M: w! O4 K0 G; L- e+ I* h
首先介绍最常用的,将域名转发到本地另一个端口上* I4 ~4 ?; U9 y# Q
  1. server{3 N' ^. d5 N! W( b
  2.   listen 80;2 _' U0 ?8 m; x6 N. p1 L# |! p
  3.   server_name  tomcat.cncml.com;( C1 T2 ^6 O  I3 T  f
  4.   index  index.php index.html index.htm;* A2 u9 B( f+ l+ k2 {! m

  5. ! b. U5 m) h" i
  6.   location / {# ^5 V% ?3 @8 s2 n: m$ P9 o) i
  7.     proxy_pass  http://127.0.0.1:8080; # 转发规则8 G$ C7 E5 p) a
  8.     proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求' }" s9 M, ^, P9 }- i+ \
  9.     proxy_set_header X-Real-IP $remote_addr;9 I# E1 V# E3 o( `4 G6 a
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 T  a# O# q9 _  ]$ V3 V
  11.   }
    2 Y- `  I+ v% |, e1 q3 V
  12. }
复制代码
这样访问 http://tomcat.cncml.com 时就会转发到本地的 8080 端口: Y' y# d$ Q( g$ k, s9 M0 E; }  a
$ E- c+ S! X5 E$ D7 l
将域名转发到另一个域名
4 u5 y$ M3 ~/ S2 [5 T) t
  1. server{
    : W; X& P7 D; m+ t
  2.   listen 80;
    " \4 X+ C! q- e
  3.   server_name  baidu.cncml.com;
    6 m5 E, j- F; G/ n2 Y# P2 d6 q! L
  4.   index  index.php index.html index.htm;
    5 j5 T- T. w1 _# I5 Y" V. n
  5. " B0 F: ?) K, [& @/ `! h* K
  6.   location / {
    3 q2 o+ |2 R+ z/ P$ K1 I$ \
  7.     proxy_pass  http://www.baidu.com;) m" B" T* P8 P  I. H3 c: v
  8.     proxy_set_header Host $proxy_host;
    * y+ n. f: w" L, D- S
  9.     proxy_set_header X-Real-IP $remote_addr;
    ( ]7 @8 r6 g( b. O" r: `& d5 G1 k
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" s  {0 w: z7 Z$ _  H% @: |2 D1 V
  11.   }
    3 V3 @) c7 E6 c. M  a
  12. }
复制代码
[size=0.6]这样访问 http://baidu.cncml.com 时就会转发到 http://www.baidu.com
本地一个端口转发到另一个端口或另一个域名/ I8 V) A! H# a
  1. server{
    + Z6 l) v. j3 Q0 t" O/ S# W
  2.   listen 80;6 e& M8 d) }0 p4 _) V" f4 n3 c
  3.   server_name 127.0.0.1; # 公网ip
    / H, I6 @0 Q6 P5 E" g
  4.   index  index.php index.html index.htm;
    ) M( \5 T5 c! M) h$ W

  5. 2 a& t; l! D$ I4 W
  6.   location / {- I' I' n: k+ K* B
  7.     proxy_pass  http://127.0.0.1:8080; # 或 http://www.baidu.com/ e  Q* r. D0 `: p
  8.     proxy_set_header Host $proxy_host;! F( v  J, i( N
  9.     proxy_set_header X-Real-IP $remote_addr;! E$ _1 G( c* }8 F+ T8 t; U) b9 t% b
  10.     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;( c5 J. m" W9 D0 Y+ @# B
  11.   }/ |0 U: D; K* T1 n7 B- J$ g
  12. }
复制代码
这样访问 http://127.0.0.1 时就会转发到本地的 8080 端口或 http://www.baidu.com  H) b; [  k5 V! c0 F
, L: \9 b7 ?' N# D
加 / 与不加 /- A8 |. g, b! ~. }* V3 o
在配置proxy_pass代理转发时,如果后面的url加/,表示绝对根路径;如果没有/,表示相对路径) L* j: |* l4 Y6 P
& ?" X4 n( m& x9 F
例如! X9 j6 l$ s5 T) b; G! H( ^
# _- p+ F, H) C, Y2 `1 |- x+ b
加 /4 w( a, k' }: D, P! u7 U: B
  1. server_name cncml.com$ B8 ~1 L! z, v. E  V; f
  2. location /data/ {
    / ?  B1 E* w7 p& X
  3. proxy_pass http://127.0.0.1/;7 Y+ t$ U" G8 `* r0 P" T4 Z
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/index.html
, W9 Q- Y7 b  @- @4 h5 G
7 F' S2 A6 s1 ^* ~不加 /& \2 N) J7 U5 E  n) S
  1. server_name cncml.com
    " R3 O9 _# _+ g: l+ l- {9 j3 J
  2. location /data/ {+ U5 E6 E: r1 E. y2 Q0 u3 l0 L
  3. proxy_pass http://127.0.0.1;
    " [( J2 R# V. i# q
  4. }
复制代码
访问 http://cncml.com/data/index.html 会转发到 http://127.0.0.1/data/index.html& z" B  E/ c; E) x1 p" k

: f0 y2 t& _5 Q% b( ~/ q
游客,如果您要查看本帖隐藏内容请回复
5 q) ?3 y0 ?- R
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-19 12:30 , Processed in 0.107603 second(s), 19 queries .

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