09 . Nginx配置LNMP和LNMT架构 (3)

客户端所有请求全部代理到后端Tomcat服务器

修改配置文件,重启服务 # 为了测试看效果使用不同的页面 mkdir /webapp{1..3} mkdir /webapps1/ROOT mkdir /webapps2/ROOT mkdir /webapps3/ROOT echo webapp1 > /webapps1/ROOT/index.jsp echo webapp2 > /webapps2/ROOT/index.jsp echo webapp3 > /webapps3/ROOT/index.jsp vim /usr/local/tomcat/instance1/conf/server.xml # 分别修改三个配置文件的appBase vim /usr/local/tomcat/instance2/conf/server.xml vim /usr/local/tomcat/instance3/conf/server.xml # 除了修改配置文件appBase,为保证后端Tomcat服务器的日志可以记录客户端真实IP vim /usr/local/tomcat/instance1/conf/server.xml prefix="localhost_access_log" suffix=".txt" pattern="**%{x-real-ip}i** %l %u %t "%r" %s %b" /> /usr/local/tomcat/instance3/ins1.sh start /usr/local/tomcat/instance2/ins1.sh start /usr/local/tomcat/instance1/ins1.sh start 配置Nginx vim /etc/nginx/nginx.conf http { upstream tomcatsrv { server 39.108.140.0:8081 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8082 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8083 weight=1 max_fails=2 fail_timeout=2; } vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; location / { proxy_pass ; proxy_set_header x-real-ip $remote_addr; root /usr/share/nginx/html; index index.html index.htm; } nginx -s reload elinks --dump 39.108.140.0 webapp1 elinks --dump 39.108.140.0 webapp2 elinks --dump 39.108.140.0 webapp3 # 我们去看下tomcat日志是否记录了真实日志 tail -2 /usr/local/tomcat/instance1/logs/localhost_access_log.2019-11-05.txt 39.108.140.0 - - [05/Nov/2019:12:28:17 +0800] "GET / HTTP/1.0" 200 8 47.92.24.137 - - [05/Nov/2019:12:35:13 +0800] "GET / HTTP/1.0" 200 8

客户端访问静态页面由Nginx解析,客户端如访问jsp页面访问请求代理到后端Tomcat服务器
只需要准备一台能解析静态页面的nginx,或者httpd,修改下配置文件即可

vim /etc/nginx/nginx.conf http { upstream nginxsrv { server 49.233.69.195:80 weight=1 max_fails=2 fail_timeout=2; } upstream tomcatsrv { server 39.108.140.0:8081 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8082 weight=1 max_fails=2 fail_timeout=2; server 39.108.140.0:8083 weight=1 max_fails=2 fail_timeout=2; } vim /etc/nginx/conf.d/default.conf location ~* \.html$ { root /usr/share/nginx/html; index index.html index.htm; proxy_pass ; proxy_set_header x-real-ip $remote_addr; } location ~* \.jsp$ { proxy_pass ; proxy_set_header x-real-ip $remote_addr; } nginx -s reload elinks --dump 39.108.140.0/index.html welcome to nginx elinks --dump 39.108.140.0/index.jsp webapp1 elinks --dump 39.108.140.0/index.jsp webapp2

如果tomcat上面搭建的是一个实际的网站,点击登录去登录,却发现登录不成功,可能是session会话不一致的问题
session(会话) 暂时没有使用共享方式,目前采用的会话保持,软件方面可以通过会话同步到数据库是实现session会话共享。

或者前方代理比如Nginx使用ip_hash之类的算法,一个用户固定访问后端的一个web服务器

即将同一个client的访问始终调度到同一后端实例.后面文章有写如何使用redis共享会话,请看下面这篇链接

https://www.cnblogs.com/you-men/p/13045736.html

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjdyj.html