1. 首页 > 快讯

Nginx 反向代理实战: 多端口网站高效收拢

大家好,今天来为大家分享Nginx 反向代理实战: 多端口网站高效收拢的一些知识点,和的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

[[338314]]

一:用nginx做反向代理

为了解决这两个问题,自然第一反应想到的就是使用反向代理,我的理想构思下应该是下图这样的。

 

既用户所有的请求都经过nginx,让nginx来判断当前url需要跳转到哪一个后端代理上,比较好的策略应该是让nginx来判断当前的host是什么来决定跳转到后端的哪一个webserver上,比如a.mip.com 就跳转到apollo,j.mip.com 就跳转到jenkins. 以此类推,这样就可以完美解决了,是吧?在nginx中你完全可以使用rewrite模块下if指令来进行判断。

二:使用if指令

这里要提一下,nginx比较原始化,如果需使用第三方module,你还需要重新编译nginx,用起来很麻烦,所以这里干脆使用OpenResty,它扩展了nginx,并且集成了很多成熟的lua模块,自行下载最新的1.15.8,安装方式和nginx一模一样。

 

默认是安装到/usr/local/目录下,当你看到有一个openresty目录表示你安装成功。

  1. [root@localhost local]# ls 
  2. bin  etc  games  include  lib  lib64  libexec  openresty  sbin  share  src 
  3. [root@localhost local]# pwd 
  4. /usr/local 

接下来你可以使用 nginx -v 来看一下openresty版本号啥的。

  1. [root@localhost sbin]# pwd 
  2. /usr/local/openresty/nginx/sbin 
  3. [root@localhost sbin]# 
  4. [root@localhost sbin]# ./nginx -v 
  5. nginx version: openresty/1.15.8.1 

为了方便,我就直接使用nginx开启三个server:

192.168.23.129:80   nginx上开启的第一个网站,就是proxy了。

192.168.23.129:8001 nginx上开启的第二个网站,模拟apollo。

192.168.23.129:8002 nginx上开启的第三个网站,模拟jenkins。

1. apollo的模拟

  1. server { 
  2.         listen       8001; 
  3.         server_name  somename  alias  another.alias; 
  4.         location / { 
  5.             root   html; 
  6.             index  apollo.html; 
  7.         } 
  8.     } 

8001端口网站的默认页是apollo.html,这个apollo.html所在路径就是在nginx下的html目录,如下所示。

  1. [root@localhost html]# pwd 
  2. /usr/local/openresty/nginx/html 
  3. [root@localhost html]# ls 
  4. 50x.html  apollo.html  index.html  jenkins.html 

2. jenkins的模拟

  1. server { 
  2.        listen       8002; 
  3.        server_name  somename  alias  another.alias; 
  4.        location / { 
  5.            root   html; 
  6.            index  jenkins.html; 
  7.        } 
  8.    } 

jenkins.html的文件所在路径如上所示哈。不再赘述。

3. proxy的模拟

  1. server { 
  2.         listen       80; 
  3.         server_name  localhost; 
  4.  
  5.         location / { 
  6.  
  7.            if ($host = "a.mip.com") { 
  8.                proxy_pass http://localhost:8001; 
  9.            } 
  10.  
  11.            if ($host = "j.mip.com") { 
  12.                proxy_pass http://localhost:8002; 
  13.            } 
  14.     } 

可以看到,只需要使用rewrite模块下的if条件语句,通过$host系统变量判断当前的url中的host的值跳转到相应的网站。

4. host映射

好了,接下来只需要将 a.mip.com 和 j.mip.com 映射到nginx的ip地址192.168.23.129即可。因为这些域名方便记忆而不是真实存在的。

  1. 192.168.23.129 a.mip.com 
  2. 192.168.23.129 j.mip.com 

 

5. 启动nginx

  1. [root@localhost sbin]# ./nginx 
  2. [root@localhost sbin]# 
  3. [root@localhost sbin]# 
  4. [root@localhost sbin]# netstat -tlnp 
  5. Active Internet connections (only servers) 
  6. Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name 
  7. tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      3802/nginx: master 
  8. tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      3802/nginx: master 
  9. tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3802/nginx: master 
  10. tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1172/sshd 
  11. tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1724/master 
  12. tcp6       0      0 :::22                   :::*                    LISTEN      1172/sshd 
  13. tcp6       0      0 ::1:25                  :::*                    LISTEN      1724/master 

通过上图可以看到,80,8001,8002 端口都已经开启了,接下来大家可以到浏览器去验证一下了。

 

可以看到这个问题已经很完美的解决了,好了,这就是本篇和大家聊到的实际场景中遇到的一个问题,希望本篇对你有帮助,以下是全部的nginx.conf。

  1. user  nobody; 
  2. worker_processes  1; 
  3.  
  4. #error_log  logs/error.log; 
  5. #error_log  logs/error.log  notice; 
  6. #error_log  logs/error.log  info; 
  7.  
  8. #pid        logs/nginx.pid; 
  9.  
  10.  
  11. events { 
  12.     worker_connections  1024; 
  13.  
  14.  
  15. http { 
  16.     include       mime.types; 
  17.     default_type  application/octet-stream; 
  18.  
  19.     log_format  main  '$host ----> $remote_addr - $remote_user [$time_local] "$request" ' 
  20.                       '$status $body_bytes_sent "$http_referer" ' 
  21.                       '"$http_user_agent" "$http_x_forwarded_for"'
  22.  
  23.     access_log  logs/access.log  main; 
  24.  
  25.     sendfile        on
  26.     #tcp_nopush     on
  27.  
  28.     #keepalive_timeout  0; 
  29.     keepalive_timeout  65; 
  30.  
  31.     #gzip  on
  32.  
  33.     server { 
  34.         listen       80; 
  35.         server_name  localhost; 
  36.  
  37.         #charset koi8-r; 
  38.  
  39.         #access_log  logs/host.access.log  main; 
  40.  
  41.     # location = /get { 
  42.         #     set_unescape_uri $key $arg_key;  # this requires ngx_set_misc 
  43.         #     redis2_query get $key
  44.         #     redis2_pass 10.105.13.174:6379; 
  45.         # } 
  46.  
  47.         location / { 
  48.  
  49.            if ($host = "a.mip.com") { 
  50.                proxy_pass http://localhost:8001; 
  51.            } 
  52.  
  53.            if ($host = "j.mip.com") { 
  54.                proxy_pass http://localhost:8002; 
  55.            } 
  56.  
  57.            root   html; 
  58.            index  index.html index.htm; 
  59.  
  60.         } 
  61.  
  62.         #error_page  404              /404.html; 
  63.  
  64.         # redirect server error pages to the static page /50x.html 
  65.         # 
  66.         error_page   500 502 503 504  /50x.html; 
  67.         location = /50x.html { 
  68.             root   html; 
  69.         } 
  70.  
  71.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  72.         # 
  73.         #location ~ \.php$ { 
  74.         #    proxy_pass   http://127.0.0.1; 
  75.         #} 
  76.  
  77.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  78.         # 
  79.         #location ~ \.php$ { 
  80.         #    root           html; 
  81.         #    fastcgi_pass   127.0.0.1:9000; 
  82.         #    fastcgi_index  index.php; 
  83.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
  84.         #    include        fastcgi_params; 
  85.         #} 
  86.  
  87.         # deny access to .htaccess files, if Apache's document root 
  88.         # concurs with nginx's one 
  89.         # 
  90.         #location ~ /\.ht { 
  91.         #    deny  all
  92.         #} 
  93.     } 
  94.  
  95.  
  96.     # another virtual host using mix of IP-, name-, and port-based configuration 
  97.     # 
  98.     server { 
  99.         listen       8001; 
  100.         server_name  somename  alias  another.alias; 
  101.         location / { 
  102.             root   html; 
  103.             index  apollo.html; 
  104.         } 
  105.     } 
  106.  
  107.     server { 
  108.         listen       8002; 
  109.         server_name  somename  alias  another.alias; 
  110.         location / { 
  111.             root   html; 
  112.             index  jenkins.html; 
  113.         } 
  114.     } 
  115.  
  116.     # HTTPS server 
  117.     # 
  118.     #server { 
  119.     #    listen       443 ssl; 
  120.     #    server_name  localhost; 
  121.  
  122.     #    ssl_certificate      cert.pem; 
  123.     #    ssl_certificate_key  cert.key
  124.  
  125.     #    ssl_session_cache    shared:SSL:1m; 
  126.     #    ssl_session_timeout  5m; 
  127.  
  128.     #    ssl_ciphers  HIGH:!aNULL:!MD5; 
  129.     #    ssl_prefer_server_ciphers  on
  130.  
  131.     #    location / { 
  132.     #        root   html; 
  133.     #        index  index.html index.htm; 
  134.     #    } 
  135.     #} 
  136.  

 

用户评论

烬陌袅

太有用了!我现在有两台服务器,一个做API,一个做静态页展示,想统一管理,Nginx反向代理真是救星啊。

    有13位网友表示赞同!

最怕挣扎

我之前也用过这种方法,把多个应用都部署在同一个机器上,利用Nginx反向代理,感觉管理起来方便多了。

    有16位网友表示赞同!

几妆痕

这篇文章刚好解决了我今天遇到的问题!现在我的网站很多端口都被占用,不知道怎么统一配置访问。

    有17位网友表示赞同!

青衫负雪

这篇文章详细介绍了好多细节,新手看懂之后应该能自己操作了!

    有15位网友表示赞同!

孤独症

多端口反向代理这个功能确实很有用的地方啊,以后我可以试试把我的小项目用Nginx来管理。

    有6位网友表示赞同!

巷口酒肆

学习nginx反向代理一直是我的难题,这篇文章讲解比较浅显易懂,受益匪浅。

    有7位网友表示赞同!

伱德柔情是我的痛。

之前我都是直接通过域名访问不同的端口,现在想想使用Nginx 反向代理效率更高啊!

    有13位网友表示赞同!

此生一诺

我平时经常碰上多服务器部署的问题,这种反向代理方法看起来很实用。

    有5位网友表示赞同!

莫名的青春

这个实际案例分析太棒了!让我更清楚的了解了Nginx 的应用场景。

    有15位网友表示赞同!

还未走i

看了这篇文章之后,我的思路豁然开朗!原来可以用Nginx 这么简单就实现多端口转发!

    有7位网友表示赞同!

←极§速

现在好多网站都是多应用服务部署在一个服务器上,这种反向代理技术确实非常重要,以后我要好好学习学习。

    有13位网友表示赞同!

夜晟洛

希望以后还有更多像这样的文章分享实际案例,帮助我们更好的理解和使用Nginx!

    有17位网友表示赞同!

風景綫つ

这篇文章解决了我的头疼问题! 感谢作者的分享!

    有6位网友表示赞同!

麝香味

我觉得多端口反向代理是个很棒的技术,可以让网站架构更加简洁高效。

    有8位网友表示赞同!

我就是这样一个人

以后碰到类似的问题可以直接参考这篇博客写文章。

    有7位网友表示赞同!

花开丶若相惜

Nginx真是个好工具,功能强大,而且使用起来也很方便!

    有5位网友表示赞同!

惦着脚尖摘太阳

原来还有这种方法可以实现多端口转发,之前一直以为很简单就可以轻松配置了,结果还是需要学习一下才行。

    有7位网友表示赞同!

情字何解ヘ

这篇文章让我对Nginx的反向代理有了更深的理解!

    有20位网友表示赞同!

限量版女汉子

这篇文章的案例分析非常具体,对新手很有帮助!

    有5位网友表示赞同!

本文采摘于网络,不代表本站立场,转载联系作者并注明出处:https://www.iotsj.com//kuaixun/7742.html

联系我们

在线咨询:点击这里给我发消息

微信号:666666