文章目录
环境
主机 | IP |
---|---|
nginx | 192.168.159.100 |
httpd1 | 192.168.159.101 |
httpd2 | 192.168.159.102 |
tomcat | 192.168.159.200 |
nginx反向代理和负载均衡
关闭防火墙和selinux
部署nginx
RS1安装httpd
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo "This is RS1" > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd
RS2安装httpd
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "This is RS2" >/var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd
nginx上修改配置文件
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream stat {
server 192.168.159.101;
server 192.168.159.102 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://stat;
}
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
访问测试
访问RS2两次后才会轮询到RS1
nginx动静分离
部署tomcat
[root@tomcat ]# yum -y install java-11-openjdk
[root@tomcat ~]# tar -zxf apache-tomcat-10.0.23.tar.gz -C /usr/local/
[root@tomcat ~]# cd /usr/local/
[root@tomcat local]# ln -s apache-tomcat-10.0.23/ tomcat
[root@tomcat local]# cd tomcat/
[root@tomcat tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# mkdir test
[root@tomcat webapps]# echo "teeeeeest" >test/index.html
[root@tomcat webapps]# cd ..
[root@tomcat tomcat]# bin/catalina.sh stop
[root@tomcat tomcat]# bin/catalina.sh start
配置nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream stat {
server 192.168.159.101;
server 192.168.159.102 weight=2;
}
upstream tomcat {
server 192.168.159.200:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://stat;
}
location /test {
proxy_pass http://tomcat;
}
[root@nginx ~]# nginx -s reload
测试访问