@kevinZheng
2015-01-10T08:47:00.000000Z
字数 2751
阅读 3049
cms网站 nginx 负载均衡 反向代理
经过两天时间的配置和测试原计划的利用nginx实现cms的反向代理和负载均衡基本成功了,现将过程记录如下:
利用nginx的反向代理和负载均衡功能加上cms程序文件共享来实现
JC_SITE 表 修改 DOMAIN 字段为 tycms.com记下待会会用到
修改DOMAIN_ALIAS 为 192.168.5.15,192.168.5.250 跟主机地址一致
修改ROOT/WEB-INF目录下servicesServlet-servlet.xml中allowVisitIP属性中的值增加一个测试地址
比如<value>192.168.5.250</value>
两台主机上各自配置tomcat可运行环境然后清空webapps目录下的内容
通过conf目录下server.xml中Host节点内配置Context 的方式来指定共享的cms程序目录例如:
<Context docBase="/home/ruizhao/公共的/ROOT" path="/" reloadable="false" workDir=""/>
如果不是两台主机而是一台机器多个tomcat应用测试的话记得修改server.xml中端口配置防止冲突
两台主机分别启动tomcat测试能不能通过各自http://ip:port 来访问,正常访问之后继续配置nginx
ubuntu下nginx常用操作管理:
sudo apt-get install nginxsudo nginx -c /etc/nginx/nginx.conf -c参数指定配置文件sudo kill -QUIT `cat /run/nginx.pid` 根据具体pid文件位置来使用sudo service nginx startsudo service nginx stopsudo service nginx restart网上的一般教程都是直接修改nginx.conf 个人觉得这么做是不合适的
以linux 下的配置为例
/etc/nginx/nginx.conf 中http 节点中有这样的内容
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
说明网站的负载和代理都应该在单独配置然后被引入到主(默认)配置中来
以下为/etc/nginx/sites-enabled/default内容
##缓存配置proxy_cache_path /tmp levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=1g;##两台主机上做负载,负载名称跟cms数据中配置的域名要一致upstream tycms.com {##具体负载的地址和端口server 192.168.5.15:18081;server 192.168.5.250:18080;ip_hash;##负载分配方式为ip哈希也就是相同ip每次访问相同的负载,方便处理session的问题}server {##反向代理服务指定端口listen 80 ;root /usr/share/nginx/html;index index.html index.htm;##反向代理的域名 ipserver_name 192.168.5.250;##代理规则支持正则表达式###根目录访问最多直接匹配效率更高location =/ {root /;index index.jhtml;proxy_pass http://tycms.com;##此处是负载配置的名称,没有负载可以直接写代理的域名端口##重定向配置proxy_redirect http://tycms.com http://$host:$server_port;##访问ip记录proxy_set_header X-Real-IP $remote_addr;##缓存配置proxy_cache cache_one;proxy_cache_valid 200 302 1h;proxy_cache_valid 301 1d;proxy_cache_valid any 1m;}##网站资源、插件、后台登陆、接口服务location ~ ^/(r|u|res|thirdparty|cmsadmin|services)/ {root /;index index.jhtml;proxy_pass http://tycms.com;proxy_redirect http://tycms.com http://$host:$server_port;proxy_set_header X-Real-IP $remote_addr;}##后缀匹配location ~ \.(jhtml|do|svl|jspx) {root /;index index.jhtml;proxy_pass http://tycms.com;proxy_redirect http://tycms.com http://$host:$server_port;proxy_set_header X-Real-IP $remote_addr;}##状态监控location /status {stub_status on;access_log off;}##默认匹配location /{root /;index index.jhtml;proxy_pass http://tycms.com;proxy_redirect http://tycms.com http://$host:$server_port;proxy_set_header X-Real-IP $remote_addr;# First attempt to serve request as file, then# as directory, then fall back to displaying a 404.try_files $uri $uri/ =404;# Uncomment to enable naxsi on this location# include /etc/nginx/naxsi.rules}
配置完成之后可以正常的浏览公告,后台管理公告,接口发布公告。
nginx配置中负载一般情况下默认或者ip_hash就可以,比较困难和麻烦的就是location 的正则表达式 匹配需要反复测试调整,使用和入门应该算是比较简单优化和晋级配置需要更多的动手实验