关于nginx的安装,可查看上篇
负载均衡+动静分离修改nginx/conf/nginx.conf
打开原文件,vim /usr/local/nginx/conf/nginx.conf
负载均衡:
找到server{},在server{}上添加
upstream tomcat {
server 192.168.142.131:8080 weight=1 max_fails=1 fail_timeout=30s;
server 192.168.142.132:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.142.133:8080 weight=1 max_fails=1 fail_timeout=30s;
}server的ip地址根据你的ip地址定义,可添加多个
location / {
root html;
index index.html index.htm;
}
替换为
location / {
root html;
index index.html index.htm;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://tomcat; //tomcat和上边定义的upstream tomcat保持一致
}server下的listen 80;可以自己修改端口号

修改完成后,保存,重启。
访问localhost:8888,显示其他三台机器的tomcat首页。
动静分离:
vim /usr/local/nginx/conf/nginx.conf
在location / {} 上方添加
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
root staticDate;#staticDate文件夹在Nginx目录下没有,需创建,和conf文件夹同级
expires 30d;
}
location ~ .*\.(js|css)?$ {
root staticDate;
expires 1h;
}
保存,重启Nginx,访问localhost:8888,
Tomcat主页上边显示的图片css一类的文件都不会显示,把文件拷贝到Nginx下staticDate文件下,则就会显示。
以上就介绍了Nginx 的负载均衡和动静分离,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号