Linux下Nginx+Tomcat负载均衡和动静分离

454次阅读
没有评论

Linux 下 Nginx+Tomcat 负载均衡和动静分离

Nginx 下载地址 http://nginx.org/

Tomcat 负载均衡配置

nginx.conf 配置

upstream xy {
     ip_hash;    #session 请求固定到一台服务器上面
     server 192.168.0.251:8080;
     server 192.168.0.251:8081;
     server 192.168.0.251:8082;
 }
location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://xy; #与 tomcats.conf 里配置的 upstream 同名
}
保存后重新加载配置:nginx -s reload
# 所有 js,css 相关的静态资源文件的请求由 Nginx 处理
 location ~.*\.(js|css)$ {
     root    /data/www; #指定文件路径
     expires     12h; #过期时间为 12 小时
 }
 #所有图片等多媒体相关静态资源文件的请求由 Nginx 处理
 location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
     root    /data/www; #指定文件路径
     expires     7d; #过期时间为 7 天
 }
[root@iZ25t7h86n9Z web1]# 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
正文完
 
ddn
版权声明:本站原创文章,由 ddn 2016-01-13发表,共计840字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。