一次NGINX負載均衡的搭建

背景

3臺服務器

192.168.9.9 web1

192.168.8.8 web2 mysql

192.168.9.10 web3 Tengine



9.9和8.8配置是一樣的 服務器上的程序文件也是一樣的NGINX監聽在9999端口 如下:

[root@localhost conf]# cat nginx.conf
user  nginx nginx;
worker_processes auto;
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  204800;
    use epoll;
    multi_accept on;
}


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;

    server_tokens off; 
    tcp_nopush on; 
    #tcp_nodelay on; 
    keepalive_timeout 10; 
    client_header_timeout 10; 
    client_body_timeout 10; 
    reset_timedout_connection on; 
    send_timeout 10; 
    limit_conn_zone $binary_remote_addr zone=addr:10m; 
    limit_conn addr 100;
    gzip on;
    gzip_min_length 1000; 
    gzip_comp_level 4; 
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    include /usr/local/nginx/conf/vhost/*.conf;
    server {
        listen       8888;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log off;
        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
    #    }
    #}
server {
    listen  *:8890 default_server;
    server_name 14.152.90.119;
    location /ngx_status 
    {
        stub_status on;
        access_log off;
       # allow 127.0.0.1;
       # deny all;
    }
}
server {
    listen       80  default_server;
    server_name  _;
    return       500;
}
}
[root@localhost conf]#

Vhost文件配置

server {
    listen       192.168.8.8:9999;
    access_log   off;
    server_name  192.168.8.8;
       root      /usr/local/nginx/html;
       index index.html index.php;
      location / {
    if (!-e $request_filename){
      rewrite ^(.*)$ /index.php?s=$1 last;
        }
     }
    location ~ \.php$ {
    root   /usr/local/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
   }
}


兩臺WEB都如上配置

MYSQL是默認安裝my.cnf如下:

[root@localhost conf]# cat /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
max_connections=5000
bind-address=192.168.8.8
skip-name-resolve
back_log=1024
key_buffer_size=1500M
max_allowed_packet=10M
thread_stack=256K
table_cache=128K
sort_buffer_size=64M
read_buffer_size=64M
join_buffer_size=64M
myisam_sort_buffer_size=64M
table_cache=4096
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost conf]#

有PHP 採用fastcgi模式 php-fpm配置如下:

listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
user = nginx
group = nginx
pm = dynamic
pm.max_children = 2000
pm.start_servers = 50
pm.min_spare_servers = 50
pm.max_spare_servers = 1024
pm.max_requests = 204800
slowlog = /var/log/php-fpm/www-slow.log
rlimit_files = 51200
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache


Tengine負載均衡配置跟上面的NGINX配置差不多

在nginx.conf的http{段加上如下:

upstream wwwoca  {
        ip_hash;
        server 192.168.9.10:9999 weight=10 max_fails=2 fail_timeout=60s;
        server 192.168.8.8:9999 weight=5 max_fails=2 fail_timeout=60s;
        server 192.168.9.9:9999 weight=10 max_fails=2 fail_timeout=60s;
        check interval=5000 rise=2 fall=3 timeout=1000 type=http;
        #check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
        keepalive 128;
     }
    proxy_temp_path   /usr/local/tengine/proxy_temp;
    proxy_cache_path /usr/local/tengine/proxy_cache levels=1:2 keys_zone=cache_one:1000m inactive=2d max_size=30g;

負載均衡虛擬主機配置如下:

server {
    listen       80;
    access_log   off;
    server_name   www.abc.com;
      location / {
       #root /usr/local/tengine/html;
       index index.html index.php;
       proxy_pass http://wwwoca;
       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_cache cache_one;
            #add_header Nginx-Cache $upstream_cache_status;
            #proxy_cache_valid  200 304 301 302 8h;
            #proxy_cache_valid 404 1m;
            #proxy_cache_valid  any 2d;
            #proxy_cache_key $host$uri$is_args$args;
            #expires 10d;
     }

        location /status {
        check_status;
        access_log off;
        #allow 127.0.0.1;
        #deny all;
    }
        #location ~ /purge(/.*) {
        #    allow   127.0.0.1;
        #    allow   59.39.178.130/32;
        #    deny    all;
        #    proxy_cache_purge  cache_one $host$1$is_args$args;
        #    error_page 405 =200 /purge$1;
        #}
}

Tengine版本如下:

[root@www sbin]# ./nginx -V
Tengine version: Tengine/2.1.2 (nginx/1.6.2)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr/local/tengine --with-http_ssl_module --with-http_sub_module=shared --with-http_fastcgi_module=shared --with-http_rewrite_module=shared --with-http_gzip_static_module --with-http_auth_request_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_upstream_check_module --with-http_upstream_ip_hash_module=shared --user=nginx --group=nginx
loaded modules:
    ngx_core_module (static)
    ngx_errlog_module (static)
    ngx_conf_module (static)
    ngx_dso_module (static)
    ngx_syslog_module (static)
    ngx_events_module (static)
    ngx_event_core_module (static)
    ngx_epoll_module (static)
    ngx_procs_module (static)
    ngx_proc_core_module (static)
    ngx_openssl_module (static)
    ngx_regex_module (static)
    ngx_http_module (static)
    ngx_http_core_module (static)
    ngx_http_log_module (static)
    ngx_http_upstream_module (static)
    ngx_http_static_module (static)
    ngx_http_gzip_static_module (static)
    ngx_http_autoindex_module (static)
    ngx_http_index_module (static)
    ngx_http_auth_request_module (static)
    ngx_http_auth_basic_module (static)
    ngx_http_access_module (static)
    ngx_http_limit_conn_module (static)
    ngx_http_limit_req_module (static)
    ngx_http_geo_module (static)
    ngx_http_map_module (static)
    ngx_http_split_clients_module (static)
    ngx_http_referer_module (static)
    ngx_http_ssl_module (static)
    ngx_http_fastcgi_module (shared, 3.1)
    ngx_http_proxy_module (static)
    ngx_http_uwsgi_module (static)
    ngx_http_scgi_module (static)
    ngx_http_memcached_module (static)
    ngx_http_empty_gif_module (static)
    ngx_http_browser_module (static)
    ngx_http_upstream_ip_hash_module (shared, 3.1)
    ngx_http_user_agent_module (static)
    ngx_http_rewrite_module (shared, 3.1)
    ngx_http_upstream_consistent_hash_module (static)
    ngx_http_upstream_check_module (static)
    ngx_http_upstream_least_conn_module (static)
    ngx_http_upstream_keepalive_module (static)
    ngx_http_upstream_dynamic_module (static)
    ngx_http_stub_status_module (static)
    ngx_http_write_filter_module (static)
    ngx_http_header_filter_module (static)
    ngx_http_chunked_filter_module (static)
    ngx_http_range_header_filter_module (static)
    ngx_http_gzip_filter_module (static)
    ngx_http_postpone_filter_module (static)
    ngx_http_ssi_filter_module (static)
    ngx_http_charset_filter_module (static)
    ngx_http_sub_filter_module (shared, 3.1)
    ngx_http_gunzip_filter_module (static)
    ngx_http_userid_filter_module (static)
    ngx_http_footer_filter_module (static)
    ngx_http_trim_filter_module (static)
    ngx_http_headers_filter_module (static)
    ngx_http_upstream_session_sticky_module (static)
    ngx_http_reqstat_module (static)
    ngx_http_copy_filter_module (static)
    ngx_http_range_body_filter_module (static)
    ngx_http_not_modified_filter_module (static)
[root@www sbin]#

基本完成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章