当前位置: 首页 > 科技观察

多端口网站如何使用Nginx做反向代理实际场景分析

时间:2023-03-20 15:24:29 科技观察

前段时间公司要整合服务器资源,正好趁着这个机会把这些乱七八糟的服务器整理整合一下,断断续续的完成了迁移一个月大概优化了1/3的机器,完成后遇到了一些问题。比如曾今在生产中部署了一些可视化UI:apollo、kibana、grafana、jenkins等,要么使用了80端口,要么对外开放了其他端口,为了安全起见,80以外的公网端口不再打开。由于机器少,80端口不足,这些可视化UI已经不能直接访问了。所以我们需要另谋出路。一:使用nginx做反向代理为了解决这两个问题,首先想到的就是使用反向代理。我理想中的想法应该如下图所示。由于所有的用户请求都要经过nginx,所以让nginx来决定当前的URL需要跳转到哪个后端代理。更好的策略应该是让nginx判断当前主机是什么来决定跳转到哪个后端代理服务器上,比如a.mip.com会跳转到apollo,j.mip.com会跳转到詹金斯。以此类推,这个是可以完美解决的吧?可以使用nginx中的rewrite模块if命令来判断。二:使用if命令这里要提一下,nginx比较原始。如果需要使用第三方模块,需要重新编译nginx,使用起来很麻烦,所以这里就干脆使用OpenResty,它扩展了nginx,集成了很多成熟的。lua模块,自己下载最新的1.15.8,安装方法和nginx完全一样。默认安装到/usr/local/目录。当你看到一个openresty目录时,就意味着你安装成功了。[root@localhostlocal]#lsbinetcgamesincludeliblib64libexecopenrestysbinsharesrc[root@localhostlocal]#pwd/usr/local接下来可以使用nginx-v查看openresty的版本号。[root@localhostsbin]#pwd/usr/local/openresty/nginx/sbin[root@localhostsbin]#[root@localhostsbin]#./nginx-vnginxversion:openresty/1.15.8.1为了方便,我就直接用nginx开了三个A服务器:192.168.23.129:80 第一个在nginx上打开的网站就是代理。192.168.23.129:8001第二个网站在nginx上打开,模拟apollo。192.168.23.129:8002第三个网站在nginx上打开,模拟jenkins。1、Apollo的模拟服务器默认页面{listen8001;server_namesomenamealiasanother.alias;location/{roothtml;indexapollo.html;}}8001端口网站是apollo.html,这个apollo.html的路径是nginx下的html目录,如下所示。[root@localhosthtml]#pwd/usr/local/openresty/nginx/html[root@localhosthtml]#ls50x.htmlapollo.htmlindex.htmljenkins.html2。Jenkins模拟服务器{listen8002;server_namesomenamealiasanother.alias;location/{roothtml;indexjenkins.html;}}jenkins.html文件路径如上图。不再。3.代理模拟服务器{listen80;server_namelocalhost;location/{if($host="a.mip.com"){proxy_passhttp://localhost:8001;}if($host="j.mip.com"){proxy_passhttp://localhost:8002;}}可以看到只需要使用rewrite模块下的if条件语句,通过$host系统变量判断当前url中host的值,跳转到对应的网站即可。4、host映射完成后,只需要将a.mip.com和j.mip.com映射到nginx的ip地址192.168.23.129即可。因为这些域名好记,不真实。192.168.23.129a.mip.com192.168.23.129j.mip.com5.启动nginx[root@localhostsbin]#./nginx[root@localhostsbin]#[root@localhostsbin]#[root@localhostsbin]#netstat-tlnpActiveInternetconnections(仅服务器)ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programnameetcp000.0.0.0:80010.0.0.0:*LISTEN3802/nginx:mastertcp000.0.0.0:80020.0.0.0:*LISTEN3802/nginx:mastertcp800LISTEN800.0ngin0:08.0master/nginx:mastertcp800LISTEN800.0ngin0:08.0.0master.0.0.0:220.0.0.0:*LISTEN1172/sshdtcp00127.0.0.1:250.0.0.0:*LISTEN1724/mastertcp600:::22:::*LISTEN1172/sshdtcp600::1:25:::*LISTEN1724/masterAs从上图可以看到,80、8001、8002端口已经打开。接下来可以去浏览器验证一下。可以看出这个问题已经完美解决了。嗯,这是本文跟大家聊的实际场景中遇到的问题。希望这篇文章对您有所帮助。以下是整个nginx.conf。#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{includemime.types;default_typeapplication/octet-stream;log_formatmain'$host---->$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"';access_loglogs/access.logmain;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;#gzipon;server{listen80;server_namelocalhost;#charsetkoi8-r;#access_loglogs/host.access.logmain;#location=/get{#set_unescape_uri$key$arg_key;#thisrequiresngx_set_misc#redis2_queryget$key;#redis2_pass10.105.13.174:6379;#}location/{if($host="a.mip.com"){proxy_passhttp://localhost:8001;}if($host="j.mip.com"){proxy_passhttp://localhost:8002;}roothtml;indexindex.htmlindex.htm;}#error_page404/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{roothtml;}#proxythePHPscriptstoApachelisteningon127.0.0.1:80##location~\.php${#proxy_passhttp://127.0.0.1;#}#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000##location~\.php${#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_indexindex.php;#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#includefastcgi_params;#}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone##location~/\.ht{#denyall;#}}#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration#server{listen8001;server_namesomenamealiasanother.alias;location/{roothtml;indexapollo.html;}}server{listen8002;server_namesomenamealiasanother.alias;location/{roothtml;indexjenkins.html;}}#HTTPSserver##server{#listen443ssl;#server_namelocalhost;#ssl_certificatecert.pem;#ssl_certificate_keycert.key;#ssl_session_cacheshared:SSL:1m;#ssl_session_timeout5m;#ssl_ciphersHIGH:!aNULL:!MD5;#ssl_prefer_server_cipherson;#location/{#roothtml;#indexindex.htmlindex.htm;#}#}}本文转载自微信公众号《线线码农聊技术》,转载文章可通过微信关注下方二维码,请联系在线码友聊聊技术公众号。