怎样让多个差别范例的后端网站用一个nginx举行反向代办现实场景剖析
2019-11-18杂谈搜奇网21°c
A+ A-
前段时间公司依据要求须要将聚石塔上服务器从杭州团体迁徙到张家口,恰好趁此次机会将这些杂乱无章的服务器做一次梳理和整合,断断续续一个月迁徙完
成也许优化掉了1/3的机械,完成以后碰到了一些题目,比方曾今零零散散布置在生产上一些可视化UI:apollo,kibana,grafana,jenkins 等等要么采纳80端口,要
么对公开放了其他端口,为了平安,如今不再开放非80以外的公网端口,由于机械少了,80端口不够,这些可视化UI不再能直接接见到了。所以需另寻其他前途。
一:用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目次示意你装置胜利。
[root@localhost local]# ls bin etc games include lib lib64 libexec openresty sbin share src [root@localhost local]# pwd /usr/local
接下来你能够运用 nginx -v 来看一下openresty版本号啥的。
[root@localhost sbin]# pwd /usr/local/openresty/nginx/sbin [root@localhost sbin]# [root@localhost sbin]# ./nginx -v nginx version: openresty/1.15.8.1
为了轻易,我就直接运用nginx开启三个server:
<1> 192.168.23.129:80 nginx上开启的第一个网站,就是proxy了。
<2> 192.168.23.129:8001 nginx上开启的第二个网站,模仿apollo。
<3> 192.168.23.129:8002 nginx上开启的第三个网站,模仿jenkins。
1. apollo的模仿:
server { listen 8001; server_name somename alias another.alias; location / { root html; index apollo.html; } }
8001端口网站的默许页是apollo.html,这个apollo.html地点途径就是在nginx下的html目次,以下所示。
[root@localhost html]# pwd /usr/local/openresty/nginx/html [root@localhost html]# ls 50x.html apollo.html index.html jenkins.html
2. jenkins的模仿
server { listen 8002; server_name somename alias another.alias; location / { root html; index jenkins.html; } }
jenkins.html的文件地点途径如上所示哈。不再赘述。
3. proxy的模仿
server { listen 80; server_name localhost; location / { if ($host = "a.mip.com") { proxy_pass http://localhost:8001; } if ($host = "j.mip.com") { proxy_pass http://localhost:8002; } }
能够看到,只须要运用rewrite模块下的if前提语句,经由过程$host体系变量推断当前的url中的host的值跳转到响应的网站。
4. host映照
好了,接下来只须要将a.mip.com 和 j.mip.com 映照到nginx的ip地点192.168.23.129即可。由于这些域名轻易影象而不是实在存在的。
192.168.23.129 a.mip.com 192.168.23.129 j.mip.com
5. 启动nginx
[root@localhost sbin]# ./nginx [root@localhost sbin]# [root@localhost sbin]# [root@localhost sbin]# netstat -tlnp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 3802/nginx: master tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 3802/nginx: master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3802/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1172/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1724/master tcp6 0 0 :::22 :::* LISTEN 1172/sshd tcp6 0 0 ::1:25 :::* LISTEN 1724/master
经由过程上图能够看到,80,8001,8002 端口都已开启了,接下来人人能够到浏览器去考证一下了。
能够看到这个题目已很圆满的处理了,好了,这就是本篇和人人聊到的现实场景中碰到的一个题目,愿望本篇对你有协助,以下是悉数的nginx.conf。

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$host ----> $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # location = /get { # set_unescape_uri $key $arg_key; # this requires ngx_set_misc # redis2_query get $key; # redis2_pass 10.105.13.174:6379; # } location / { if ($host = "a.mip.com") { proxy_pass http://localhost:8001; } if ($host = "j.mip.com") { proxy_pass http://localhost:8002; } root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 8001; server_name somename alias another.alias; location / { root html; index apollo.html; } } server { listen 8002; server_name somename alias another.alias; location / { root html; index jenkins.html; } } # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }View Code