Tomcat and nginx realize dynamic separation (including the deployment of nginx service)

1. Brief introduction

Tomcat is used to redirect dynamic web pages, while static web pages are still redirected on nginx. Prepare two hosts with Tomcat deployed, and one host with nginx deployed for testing. The deployment of Tomcat has been introduced in the previous article, the deployment of Tomcat

The core components of Tomcat are as follows:

Insert picture description here

2. Case environment

The dynamic web page here is very simple, just for testing purposes.
Host 1: Nginx service is deployed, IP: 192.168.247.130
Host 2: Tomcat service is deployed, IP: 192.168.247.140
Host 3: Tomcat service is deployed, IP "192.168.247.150

Three, dynamic and static separation configuration

Host 1:

Compile and install nginx:

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++
[root@localhost ~]# useradd -s /bin/false www
[root@localhost opt]# tar zxf nginx-1.6.0.tar.gz
[root@localhost opt]# cd nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=www \
> --group=www \
> --with-file-aio \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-http_flv_module \
> --with-http_ssl_module
[root@localhost nginx-1.6.0]# make && make install

Modify the configuration file, that is, the static and dynamic separation configuration

[root@localhost nginx-1.6.0]# vi /usr/local/nginx/conf/nginx.conf
#gzip  on;
#需要添加的模块,设置地址池,当访问动态网页时,跳转到地址池中的主机
(即配置了Tomcat服务的主机,网站目录里有动态页面),可以设置权重,来调整访问的顺序次数
这里都设置为1,则默认先访问140,下一次访问的就是150,轮流访问
    upstream tomcat_server {
    
    
            server 192.168.247.140:8080 weight=1;
            server 192.168.247.150:8080 weight=1;
    }
 server {
    
    
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
#在server模块里添加location,即访问以.jsp结尾的网页(动态网页)时,定位到这个模块
跳转至 http://192.168.247.140150,访问Tomcat服务器
        location ~ \.jsp$ {
    
    
            proxy_pass http://tomcat_server;
        }
#优化nginx路径
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin 

Make management scripts and start nginx service

[root@localhost ~]# vi /etc/init.d/nginx 
#!/bin/bash
#chkconfig: 35 20 80
#description: nginx server
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
       $PROG
       ;;
    stop)
       killall -s QUIT $(cat $PIDF)
       ;;
    restart)
       $0 stop
       $0 start
       ;;
    reload)
       killall -s HUP $(cat $PIDF)
       ;;
    *)
       echo "Usage: $0 {start|stop|reload|status}"
       exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15683/nginx: master

Static web page test: The web page uses the default access page, no additional creation is required, and the static web page can be directly accessed after deployment.

Insert picture description here

Hosts 2 and 3:

Directly use the dynamic web page deployed by the previous Tomcat for testing.
Don't make a jump between moving and static, and directly access the Tomcat server

Host 2:

Insert picture description here
Host 3:

Insert picture description here

Dynamic and static separation test

Want to access dynamic web pages through nginx

The result of the first visit: it is the dynamic web page of host 2

Insert picture description here
Refresh again, it is the webpage provided by host 3

Insert picture description here

Constantly refreshing is to visit hosts 2 and 3 in turn.
The above has achieved dynamic and static separation.

Guess you like

Origin blog.csdn.net/qq_41786285/article/details/109207672
Recommended