linux-nginx反代理tomcat
利用 nginx 反向代理实现全部转发置指定同一个虚拟主 机
设别
centos7 10.0.0.17 tomcat多虚拟主机
centos7 10.0.0.27 nginx 代理
centos7 10.0.0.7 客户端
域名解析
10.0.0.27 nginx 配置hosts文件
10.0.0.17 www.lia.com www.lib.com www.lic.com
10.0.0.7 客户端 访问 nginx代理服务器 配置hosts文件
10.0.0.27 www.lia.com www.lib.com www.lic.com
创建多虚拟主机的文件的访问路径 10.0.0.17 tomcat 服务器
[root@centos7-liyj /usr/local/tomcat]#mkdir /data/webapps{1,2,3} 创建目录 [root@centos7-liyj /usr/local/tomcat]#mkdir /data/webapps{1,2,3}/ROOT [root@centos7-liyj /usr/local/tomcat]#cd [root@centos7-liyj /data]#tree . ├── webapps1 │ └── ROOT ├── webapps2 │ └── ROOT └── webapps3 └── ROOT 6 directories, 0 files [root@centos7-liyj /data]#chown -R tomcat.tomcat /data/webapps1 #修改权限为tomcat [root@centos7-liyj /data]#chown -R tomcat.tomcat /data/webapps2 [root@centos7-liyj /data]#chown -R tomcat.tomcat /data/webapps3 [root@centos7-liyj /data]#echo www.lia.com > /data/webapps1/ROOT/index.html #建立访问文件 [root@centos7-liyj /data]#echo www.lib.com > /data/webapps2/ROOT/index.html [root@centos7-liyj /data]#echo www.lic.com > /data/webapps3/ROOT/index.html
修改tomcat配置文件
[root@centos7-liyj /usr/local/tomcat]#vim conf/server.xml <Host name="localhost" appBase="webapps" #复制此行修改 unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" #此处为日志信息,有需求也可复制 prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="www.lia.com" appBase="/data/webapps1/" #修改name为要访问的域名,appBase修改为域名访问的文件路径 unpackWARs="true" autoDeploy="true"> </Host> <Host name="www.lib.com" appBase="/data/webapps2/" unpackWARs="true" autoDeploy="true"> </Host> <Host name="www.lic.com" appBase="/data/webapps3/" unpackWARs="true" autoDeploy="true"> </Host>
重启服务
[root@centos7-liyj /usr/local/tomcat]#systemctl restart tomcat
多虚拟主机测试参照 https://www.cnblogs.com/lyj1023/p/16390065.html
nginx代理服务器配置
[root@centos7-liyj ~]#vim /usr/local/nginx/conf/nginx.conf location / { # root html; # index index.html index.htm; proxy_pass http://www.lia.com:8080; #添加代理配置,指定域名 }
访问测试
[root@centos7-liyj ~]#curl www.lia.com www.lia.com [root@centos7-liyj ~]#curl www.lib.com www.lia.com [root@centos7-liyj ~]#curl www.lic.com www.lia.com