• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Nginx 的负载均衡和动静分离

php 搞代码 4年前 (2022-01-23) 35次浏览 已收录 0个评论

关于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|@本文9来源gao($daima.com搞@代@#码8网^搞代gaodaima码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教程有兴趣的朋友有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Nginx 的负载均衡和动静分离

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址