Nginx 负载均衡实践

一、安装源码包

下载安装 pcre-8.32.tar.gz

1. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz

2. tar zxvf pcre-8.32.tar.gz

3. cd pcre-8.32

4. ./configure  (注:configure: error: You need a C++ compiler for C++ support.   yum install -y gcc gcc-c++)

5. make && make install


下载安装nginx-1.2.8.tar.gz

  1. cd /usr/local/src  

  2. wget http://nginx.org/download/nginx-1.2.8.tar.gz  

  3. tar -zxvf nginx-1.2.8.tar.gz  

  4. cd nginx-1.2.8  

  5. ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.32 --user=nginx --group=nginx --with-http_stub_status_module

    (注:./configure: error: the HTTP gzip module requires the zlib library.   yum install -y zlib-devel)  

  6. make  

  7. make install  

注:--with-pcre 指定pcre的源码目录,如果要安装zlib的话也是这样,添加个--with-zlib,后面加个源码路径


二、nginx配置(nginx负载均衡的最简化模型

  1. worker_processes 1;    

  2. events {    

  3. worker_connections 1024;    

  4. }    

  5. http{    

  6.    upstream myproject {    

  7.    #这里指定多个源服务器,ip:端口,80端口的话可写可不写    

  8.        server 192.168.43.158:80;    

  9.        server 192.168.41.167;    

  10.    }    

  11.    server {    

  12.        listen 8080;    

  13.        location / {    

  14.            proxy_pass http://myproject;    

  15.        }    

  16.    }    

  17. }


三、详细配置nginx

/usr/local/nginx/conf/nginx.conf

  1. #运行用户    

  2. user nginx nginx;    

  3. #启动进程    

  4. worker_processes 2;    

  5. #全局错误日志及PID文件    

  6. error_log logs/error.log notice;    

  7. pid logs/nginx.pid;    

  8. #工作模式及每个进程连接数上限    

  9. events {    

  10.    use epoll;    

  11.    worker_connections 1024;     #所以nginx支持的总连接数就等于worker_processes * worker_connections  

  12. }    

  13. #设定http服务器,利用它的反向代理功能提供负载均衡支持    

  14. http {    

  15.    #设定mime类型    

  16.    include mime.types;  #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体  

  17.    default_type application/octet-stream;   #默认的数据类型  

  18.    #设定日志格式    

  19.    log_format main '$remote_addr - $remote_user [$time_local] '  

  20.    '"$request" $status $bytes_sent '  

  21.    '"$http_referer" "$http_user_agent" '  

  22.    '"$gzip_ratio"';    

  23.    log_format download '$remote_addr - $remote_user [$time_local] '  

  24.    '"$request" $status $bytes_sent '  

  25.    '"$http_referer" "$http_user_agent" '  

  26.    '"$http_range" "$sent_http_content_range"';    

  27.    #设定请求缓冲    

  28.    client_header_buffer_size 1k;    

  29.    large_client_header_buffers 4 4k;    

  30.    #开启gzip模块    

  31.    #gzip on;    

  32.    #gzip_min_length 1100;    

  33.    #gzip_buffers 4 8k;    

  34.    #gzip_types text/plain;    

  35.    #output_buffers 1 32k;    

  36.    #postpone_output 1460;    

  37.    #设定access log    

  38.    access_log logs/access.log main;    

  39.    client_header_timeout 3m;    

  40.    client_body_timeout 3m;    

  41.    send_timeout 3m;    

  42.    sendfile on;    

  43.    tcp_nopush on;    

  44.    tcp_nodelay on;    

  45.    keepalive_timeout 65;    

  46.    #设定负载均衡的服务器列表    

  47.    upstream mysvr {    

  48.        #weigth参数表示权值,权值越高被分配到的几率越大  

  49.        server 192.168.207.129:80 weight=5;    

  50.        server 192.168.207.130:8080 weight=5;    

  51.        server 192.168.207.131:8080 weight=2;  

  52.    }    

  53.    server { #这个是设置web服务的,监听8080端口  

  54.        listen        8080;  

  55.        server_name    192.168.207.131;  

  56.        index     index.html index.htm;  

  57.        root        /var/www/html;  

  58.        #error_page     500 502 503 504    /50x.html;  

  59.        #location = /50x.html {  

  60.        #    root     html;  

  61.        #}  

  62.        }  

  63.    #设定虚拟主机    

  64.    server {    

  65.        listen 80;    

  66.        server_name 192.168.207.131;    

  67.        #charset gb2312;    

  68.        #设定本虚拟主机的访问日志    

  69.        access_log logs/three.web.access.log main;    

  70.        #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid    

  71.        #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好    

  72.        #location ~ ^/(img|js|css)/{    

  73.        #   root /data3/Html;    

  74.        #   expires 24h;  

  75.        #}  

  76.            #对 "/" 启用负载均衡    

  77.        location / {    

  78.            proxy_pass http://mysvr;  #以这种格式来使用后端的web服务器  

  79.            proxy_redirect off;    

  80.            proxy_set_header Host $host;    

  81.            proxy_set_header X-Real-IP $remote_addr;    

  82.            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    

  83.            client_max_body_size 10m;    

  84.            client_body_buffer_size 128k;    

  85.            proxy_connect_timeout 90;    

  86.            proxy_send_timeout 90;    

  87.            proxy_read_timeout 90;    

  88.            proxy_buffer_size 4k;    

  89.            proxy_buffers 4 32k;    

  90.            proxy_busy_buffers_size 64k;    

  91.            proxy_temp_file_write_size 64k;  

  92.        }    

  93.        #设定查看Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module参数  

  94.        location /NginxStatus {    

  95.            stub_status on;    

  96.            access_log on;    

  97.            auth_basic "NginxStatus";    

  98.            auth_basic_user_file conf/htpasswd;     #设置访问密码,htpasswd -bc filename username password  

  99.        }  

  100.    }  

  101. }  


四、启动nginx及查看效果

useradd nginx  

/usr/local/nginx/sbin/nginx   //启动


http://ip:port 多次访问可以查看效果(ie缓存会影响效果,必须清空缓存)

或者在linux 系统下使用elinks http://ip:port or curl http://ip:port