在工作中生产环境中,我们部署的应用可能是各种各样的服务,不过这些应用为了避免资源浪费,所以公用一个nginx做反向代理,这里就介绍一下同一个nginx下的多个不同的tomcat应用的调用,我们需要的环境有安装nginx可以参考:https://www.wulaoer.org/?p=295,至于tomcat的安装就看下面的部署吧,tomcat的应用依赖于java,这里就不多说了,我们只使用静态的页面代替即可。
[root@www.wulaoer.org ~]# wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/tomcat/tomcat-8/v8.5.54/bin/apache-tomcat-8.5.54.tar.gz [root@www.wulaoer.org ~]# tar -zxf apache-tomcat-8.5.54.tar.gz [root@www.wulaoer.org ~]# mv apache-tomcat-8.5.54 /usr/local/tomcat01 [root@www.wulaoer.org ~]# tar -zxf apache-tomcat-8.5.54.tar.gz [root@www.wulaoer.org ~]# mv apache-tomcat-8.5.54 /usr/local/tomcat02
配置tomcat
为了避免端口冲突,我们需要针对不同的服务修改一下端口,还有服务的应用展视。这里我把tomcat01的应用端口都改成1开口的端口,tomcat02的改成2开头的端口。改端口的意义是我在同一台服务上测试,为了区分。
[root@www.wulaoer.org ~]# cd /usr/local/tomcat01/conf [root@www.wulaoer.org conf]# sed -i 's/8009/18009/g' server.xml [root@www.wulaoer.org conf]# sed -i 's/8443/18443/g' server.xml [root@www.wulaoer.org conf]# sed -i 's/8080/18080/g' server.xml [root@www.wulaoer.org conf]# sed -i 's/8005/18005/g' server.xml [root@www.wulaoer.org tomcat01]# vim webapps/ROOT/index.html 01 www.wulaoer.org
配置tomcat02的信息
[root@www.wulaoer.org ~]# cd /usr/local/tomcat02/conf/ [root@www.wulaoer.org conf]# sed -i 's/8009/28009/g' server.xml [root@www.wulaoer.org conf]# sed -i 's/8443/28443/g' server.xml [root@www.wulaoer.org conf]# sed -i 's/8080/28080/g' server.xml You have new mail in /var/spool/mail/root [root@www.wulaoer.org conf]# sed -i 's/8005/28005/g' server.xml [root@www.wulaoer.org conf]# rm -rf ../webapps/ROOT/* You have new mail in /var/spool/mail/root [root@www.wulaoer.org conf]# vim ../webapps/ROOT/index.html 02 www.wulaoer.org
启动所有的tomcat服务
我们把两个tomcat启动一下,如果启动的过程中出现访问不了的情况,可以看一下日志。这里使用的静态文件不会出来访问不了的问题,最有的可能就是端口修改错误了,需要看一下。
[root@www.wulaoer.org ~]# /usr/local/tomcat01/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat01 Using CATALINA_HOME: /usr/local/tomcat01 Using CATALINA_TMPDIR: /usr/local/tomcat01/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat01/bin/bootstrap.jar:/usr/local/tomcat01/bin/tomcat-juli.jar Tomcat started. [root@www.wulaoer.org ~]# /usr/local/tomcat02/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat02 Using CATALINA_HOME: /usr/local/tomcat02 Using CATALINA_TMPDIR: /usr/local/tomcat02/temp Using JRE_HOME: /usr/local/jdk1.8 Using CLASSPATH: /usr/local/tomcat02/bin/bootstrap.jar:/usr/local/tomcat02/bin/tomcat-juli.jar Tomcat started.
验证一下tomcat的服务
这里就不在浏览器了查看了,如果你使用浏览器访问不了,说明你的防火墙开启了,关闭你的防火墙,或者在防火墙做一下策略。我这里已经关闭了,就在本机测试一下:
[root@www.wulaoer.org ~]# curl http://10.211.55.32:18080 01 www.wulaoer.org [root@www.wulaoer.org ~]# curl http://10.211.55.32:28080 02 www.wulaoer.org
服务搭建成功,下一步就是在nginx上设置反向代理不同的服务上了。为了方便在一个nginx服务上代理多个域名,所以我们需要对nginx做一个基础设置,我这里使用的是yum安装的nginx,不过配置都差不多。
[root@www.wulaoer.org ~]# cd /etc/nginx/ [root@www.wulaoer.org nginx]# cp nginx.conf conf.d/test.conf You have new mail in /var/spool/mail/root [root@www.wulaoer.org nginx]# vim nginx.conf user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. include /etc/nginx/conf.d/*.conf; }
这里copy了一个test.conf的文件作为一个测试,我把nginx的所有*.conf文件都指定了conf.d目录下了。下面看看test.conf文件的配置
[root@www.wulaoer.org nginx]# cd conf.d/ You have new mail in /var/spool/mail/root [root@www.wulaoer.org conf.d]# vim test.conf server { listen 80 default_server; listen [::]:80 default_server; server_name _; #root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location /tomcat01 { proxy_pass http://127.0.0.1:18080; } location /tomcat02 { proxy_pass http://127.0.0.1:28080; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
这样我就可以通过80端口带来到两个不同的18080端口和28080端口了,下面看一下测试结果:
[root@www.wulaoer.org ~]# curl http://localhost:18080 01 www.wulaoer.org [root@www.wulaoer.org ~]# curl http://localhost:28080 02 www.wulaoer.org
整个测试验证成功,如有什么不之处,可以留言,学了一点是一点。
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏