Nginx负载均衡使用

 

Here’s the table of contents:

  1. 安装
  2. 启动
  3. 重新载入配置文件【热加载】
  4. 重启nginx
  5. 停止nginx
  6. 配置多个IP映射到一个域名
  7. 配置支持TCP协议转发【bolt】
  8. 动态加载stream模块

安装

  • 下载压缩包上传服务器解压
  • 安装依赖模块
    sudo yum -y install openssl openssl-devel
    sudo yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    
  • 编译
    ./configure
    
    sudo make
    
  • 安装
    sudo make install
    

启动

cd /usr/local/nginx/sbin
sudo ./nginx

重新载入配置文件【热加载】

sudo ./nginx -s reload
nginx -s reload

重启nginx

sudo ./nginx -s reopen

停止nginx

sudo ./nginx -s stop

配置多个IP映射到一个域名

配置nginx.conf文件【ONgDB集群的HTTP服务使用Nginx服务做负载】

    #ONgDB SERVER
    upstream ongdb_http {
        least_conn;
        server 10.20.12.173:7474;
        server 10.20.13.146:7474;
        server 10.20.13.200:7474;
    }
    server {
        listen       80;
        server_name  testlab.ongdb.http.server;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://ongdb_http;
            # 返回真实IP或域名,不要返回服务组名称
            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;
        }
     }
  • Nginx负载均衡策略优化
    # 其它所有的非backup机器down或者忙的时候,请求backup机器
    # ONgDB SERVER
      upstream ongdb_http {
          least_conn;
          server 10.20.12.173:7474 backup;
          server 10.20.13.146:7474 backup;
          server 10.20.13.200:7474;
          server 10.20.0.157:7674;
      }
    

    配置支持TCP协议转发【bolt】

    # 该配置与http平级
    # ONgDB负载
    stream {
       upstream ongdb_7687 {
          #hash X-Forwarded-For;
          # hash X-Real-Ip;
          least_conn;
          server 10.20.12.173:7687 backup;
          server 10.20.13.146:7687 backup;
          server 10.20.13.200:7687;
          server 10.20.0.157:7687;
       }
       server {
          listen 7687;
          proxy_responses 1;
          proxy_connect_timeout 20s;
          proxy_bind $server_addr:$remote_port;
          proxy_pass ongdb_7687;
       }
    }
    

    动态加载stream模块

  • 查看动态模块
    ./configure --help | grep dynamic
    --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
    --with-http_image_filter_module=dynamic   enable dynamic ngx_http_image_filter_module
    --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
    --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
    --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
    --with-stream=dynamic              enable dynamic TCP/UDP proxy module
    --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
    --add-dynamic-module=PATH          enable dynamic external module
    --with-compat                      dynamic modules compatibility
    
  • stream模块加载
    sudo ./configure --with-stream=dynamic
    sudo make && sudo make install
    
  • 修改nginx.conf配置
    load_module  modules/ngx_stream_module.so;
    sudo reload nginx即可
    
  • nginx.conf一个完整的配置样例 ``` load_module modules/ngx_stream_module.so; #user nobody; worker_processes 8; worker_rlimit_nofile 65535; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 4096; } http { include mime.types; default_type application/octet-stream; #log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘ # ‘$status $body_bytes_sent “$http_referer” ‘ # ‘“$http_user_agent” “$http_x_forwarded_for”’; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; client_max_body_size 64m; proxy_read_timeout 3600s; #gzip on; #ONgDB SERVER upstream ongdb_http { least_conn; server 10.20.12.173:7474 backup; server 10.20.13.146:7474 backup; server 10.20.13.200:7474; server 10.20.0.157:7674; } server { listen 80; server_name testlab.ongdb.http.server; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://ongdb_http; 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; } #location = / { # proxy_pass http://ongdb_http; #} # location = /db/data/transaction/commit { # proxy_pass http://ongdb_http; #} #location = /db/data { # proxy_pass http://ongdb_http; #} #location = /db/manage { # proxy_pass http://ongdb_http; #} #location = /db/data/transaction { # proxy_pass http://ongdb_http; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache’s document root # concurs with nginx’s one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias;

    # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost;

    # ssl_certificate cert.pem; # ssl_certificate_key cert.key;

    # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m;

    # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on;

    # location / { # root html; # index index.html index.htm; # } #}

} stream { upstream ongdb_7687 { #hash X-Forwarded-For; # hash X-Real-Ip; least_conn; server 10.20.12.173:7687 backup; server 10.20.13.146:7687 backup; server 10.20.13.200:7687; server 10.20.0.157:7689; } server { listen 7687; proxy_responses 1; proxy_connect_timeout 20s; proxy_bind $server_addr:$remote_port; proxy_pass ongdb_7687; } } ```