Linux ·

Nginx负载均衡配置实战

今天给网站加上负载均衡,一切顺利,由于需要用到第三方的模块,所以需要重新编译Nginx,写一下过程,刚刚写了一个 Nginx升级过程,这里沿用刚才的环境,还需要下载nginx-upstream-fair

编译升级过程

 # 下载解压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
 # 说明编译成功了

接下来就是配置了

后端 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任务基本就是访问后端,然后返回页面,只需要一个请求,压力大大减小了

--------------------------------------分割线 --------------------------------------

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

--------------------------------------分割线 --------------------------------------

参与评论