Nginx编译升级过程
# 下载解压nginx-upstream-fair $ curl https://codeload.github.com/gnosek/nginx-upstream-fair/zip/master > nginx-upstream-fair.zip $ unzip nginx-upstream-fair.zip $ cd nginx-1.6.2 # 查看当前nginx 编译参数 $ nginx -V # 输出 configure arguments: --prefix=/usr/local/nginx # 加上编译参数,加入刚才下载的模块 $ ./configure --prefix=/usr/local/nginx --add-module=../nginx-upstream-fair-master # 编译 $ make # 如果没有什么问题,应该是编译成功的 # 先把nginx停了,然后备份一下 $ nginx -s stop $ cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.6 # 把刚才编译好的nginx二进制文件,覆盖现在文件 $ cp -f ./objs/nginx /usr/local/nginx/sbin/nginx $ 启动nginx $ nginx # 查看当前nginx 编译参数 $ nginx -V # 输出 configure arguments: --prefix=/usr/local/nginx --add-module=../nginx-upstream-fair-master # 说明编译成功了
nginx负载均衡配置
后端 webservice 配置,由于后端接口跟状态无关,所以这里用fair策略,也就是刚才编译的模块。
upstream webservice { fair; server localhost:8180; server localhost:8181; } server { listen 9999; server_name localhost; location / { proxy_pass http://webservice; #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; ## set upload file size client_max_body_size 20m; }
前端网站
配置,前端一般都涉及session问题,要做负载均衡,要么做session同步,要么就将用户请求都转发都一个tomcat,我这里用的是后者,简单是一种美,简单也不容易出错。
upstream site { ip_hash; server localhost:8280; server localhost:8281; } server { listen 80; server_name localhost; location / { proxy_pass http://site; #Proxy Settings proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; ## set upload file size client_max_body_size 20m; }
OK,一切顺利,重启nginx
即可,如果访问量大,继续增加节点即可,最重要的还是要做缓存,并且把静态文件分离出来,这样后台的tomcat任务基本就是访问后端,然后返回页面,只需要一个请求,压力大大减小了。