004 软负载均衡器Nginx简介以及安装配置

Nginx简介:

工作在网络的7层之上(理论上只能工作在7层之上,只能针对应用层进行分流及反向代理策略),可以针对http应用做一些分流的策略,比如针对域名、目录结构;

Nginx对网络的依赖比较小,理论上能ping通就就能进行负载功能;

Nginx安装和配置比较简单,测试起来比较方便;

也可以承担高的负载压力且稳定,一般能支撑超过1万次的并发;

对后端服务器的健康检查,只支持通过端口来检测,不支持通过url来检测。

Nginx对请求的异步处理可以帮助节点服务器减轻负载;

Nginx仅能支持httphttpsEmail协议,这样就在适用范围较小。

Nginx 编译安装配置:

安装依赖:

yum install -y gcc gcc-c++

yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel

创建Nginx用户:

useradd -s /bin/false -M nginx

下载并解压安装包:

wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxf nginx-1.12.0.tar.gz

Nginx编译安装:

## 生成make文件
./configure --user=nginx --group=nginx --prefix=/etc/nginx-1.12.0 --with-http_v2_module --withhttp_
ssl_module --with-http_sub_module --with-http_stub_status_module --withhttp_
gzip_static_module --with-pcre
## 编译并安装
make && make install
## 添加软连接
ln -s /etc/nginx-1.12.0/ /etc/nginx
## 添加环境变量
ln -s /etc/nginx/sbin/* /usr/local/sbin/

Nginx目录介绍:

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

Nginx 配置文件详解:

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

004 软负载均衡器Nginx简介以及安装配置

location匹配模式以及顺序:

location = /uri =开头表示精确匹配,只有完全匹配上才能生效
location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前
location ~ pattern ~开头表示区分大小写的正则匹配
location ~* pattern ~*开头表示不区分大小写的正则匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

Nginx常用命令:

检查配置文件:nginx -t

指定其他配置文件启动nginx:nginx -c file

启动nginx:nginx

停止nginx:

       ## 快速停止nginx
      nginx -s stop
      ## 平滑停止nginx
      nginx -s quit

重启nginx:

      ## 平滑重载所有配置
      nginx -s reload

配置示例:

#user nobody;
worker_processes auto;
#worker_cpu_affinity auto;
worker_rlimit_nofile 655350;


#pid logs/nginx.pid;
error_log /data/logs/nginx/error.log error;


events {
       use epoll;
       worker_connections 655350;
}


http {
       include mime.types;
       default_type application/octet-stream;
       charset utf-8;
       log_format main '$remote_addr $server_addr $remote_user [$time_local] $host '
                                  '"$request" $status $body_bytes_sent $request_time $upstream_response_time '
                                  '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
                                  access_log /data/logs/nginx/access.log main;
       sendfile on;
       keepalive_timeout 90000;
      server_names_hash_max_size 1024;
      server_names_hash_bucket_size 512;
      client_header_buffer_size 16k;
      large_client_header_buffers 4 64k;
      client_header_timeout 300m;
      client_body_timeout 300m;
      send_timeout 300m;
      tcp_nopush on;
      tcp_nodelay on;
      client_max_body_size 100M;
      client_body_buffer_size 50m;
      proxy_connect_timeout 5;
      proxy_send_timeout 15;
      proxy_read_timeout 15;
      proxy_buffer_size 256k;
      proxy_buffers 8 256k;
      proxy_busy_buffers_size 256k;
      proxy_temp_file_write_size 256k;
      proxy_intercept_errors on;
      proxy_headers_hash_max_size 512;
      proxy_headers_hash_bucket_size 256;
     variables_hash_max_size 512;
     variables_hash_bucket_size 128;
     gzip on;
     gzip_min_length 1100;
     gzip_buffers 4 8k;
     gzip_comp_level 3;
     gzip_http_version 1.0;
     gzip_types text/plain application/x-javascript application/json application/javascript
text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;


     output_buffers 1 32k;
     postpone_output 1460;


     gzip_vary on;
     map_hash_max_size 102400;
     map_hash_bucket_size 256;


     fastcgi_intercept_errors on;


      server {
            listen 80;
            server_name carlosxiao.cc;
            location / {
                    root /data/www;
                    index index.html index.htm;
            }
           access_log /data/logs/nginx/carlosxiao.log;
     }
}

反向代理:

#user nobody;
worker_processes auto;
#worker_cpu_affinity auto;
worker_rlimit_nofile 655350;


#pid logs/nginx.pid;
error_log /data/logs/nginx/error.log error;


events {
      use epoll;
      worker_connections 655350;
}


http {
      include mime.types;
      default_type application/octet-stream;
      charset utf-8;
      log_format main '$remote_addr $server_addr $remote_user [$time_local] $host '
                                 '"$request" $status $body_bytes_sent $request_time $upstream_response_time '
                                 '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
                                 access_log /data/logs/nginx/access.log main;
     sendfile on;
     keepalive_timeout 90000;
     server_names_hash_max_size 1024;
     server_names_hash_bucket_size 512;
     client_header_buffer_size 16k;
     large_client_header_buffers 4 64k;
     client_header_timeout 300m;
     client_body_timeout 300m;
     send_timeout 300m;
     tcp_nopush on;
     tcp_nodelay on;
     client_max_body_size 100M;
     client_body_buffer_size 50m;
     proxy_connect_timeout 5;
     proxy_send_timeout 15;
     proxy_read_timeout 15;
     proxy_buffer_size 256k;
     proxy_buffers 8 256k;
     proxy_busy_buffers_size 256k;
     proxy_temp_file_write_size 256k;
     proxy_intercept_errors on;
     proxy_headers_hash_max_size 512;
     proxy_headers_hash_bucket_size 256;
     variables_hash_max_size 512;
     variables_hash_bucket_size 128;
     gzip on;
     gzip_min_length 1100;


     gzip_buffers 4 8k;
     gzip_comp_level 3;
    gzip_http_version 1.0;


     gzip_types text/plain application/x-javascript application/json application/javascript
text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;


     output_buffers 1 32k;
     postpone_output 1460;

     gzip_vary on;
     map_hash_max_size 102400;
     map_hash_bucket_size 256;


     fastcgi_intercept_errors on;


     upstream carlosxiao.cc{
           server 192.168.85.128:8080;
           server 192.168.85.131:8080;
     check interval=3000 rise=2 fall=3 timeout=3000 type=http;
     }
     server {
          listen 80;
          server_name carlosxiao.cc;
    location / {
           proxy_pass http://carlosxiao.cc;
           proxy_set_header Host $host;
           proxy_redirect off;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_connect_timeout 60;
           proxy_read_timeout 600;
           proxy_send_timeout 600;
          }
    access_log /data/logs/nginx/carlosxiao.log;
   }
}

动静分离:

#user nobody;
worker_processes auto;
#worker_cpu_affinity auto;
worker_rlimit_nofile 655350;


#pid logs/nginx.pid;
error_log /data/logs/nginx/error.log error;


events {
     use epoll;
     worker_connections 655350;
}


http {
     include mime.types;
     default_type application/octet-stream;
     charset utf-8;
     log_format main '$remote_addr $server_addr $remote_user [$time_local] $host '
                                '"$request" $status $body_bytes_sent $request_time $upstream_response_time '
                                '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
                                access_log /data/logs/nginx/access.log main;
     sendfile on;
     keepalive_timeout 90000;
     server_names_hash_max_size 1024;
     server_names_hash_bucket_size 512;
     client_header_buffer_size 16k;
     large_client_header_buffers 4 64k;
     client_header_timeout 300m;
     client_body_timeout 300m;
     send_timeout 300m;
     tcp_nopush on;
     tcp_nodelay on;
     client_max_body_size 100M;
     client_body_buffer_size 50m;
     proxy_connect_timeout 5;
     proxy_send_timeout 15;
     proxy_read_timeout 15;
     proxy_buffer_size 256k;
     proxy_buffers 8 256k;
     proxy_busy_buffers_size 256k;
     proxy_temp_file_write_size 256k;
     proxy_intercept_errors on;
     proxy_headers_hash_max_size 512;
     proxy_headers_hash_bucket_size 256;
     variables_hash_max_size 512;
     variables_hash_bucket_size 128;
     gzip on;
     gzip_min_length 1100;
     gzip_buffers 4 8k;
     gzip_comp_level 3;
     gzip_http_version 1.0;
     gzip_types text/plain application/x-javascript application/json application/javascript
text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;


     output_buffers 1 32k;
     postpone_output 1460;


     gzip_vary on;
     map_hash_max_size 102400;
     map_hash_bucket_size 256;
     fastcgi_intercept_errors on;
     upstream carlosxiao.cc{
             server 127.0.0.1:10179;
     }
    server {
          listen 80;
          server_name carlosxiao.cc;
          location /api {
                  proxy_pass http://carlosxiao.cc;
                  proxy_set_header Host $host;
                  proxy_redirect off;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  proxy_connect_timeout 60;
                  proxy_read_timeout 600;
                  proxy_send_timeout 600;
                  }
          location ~ .(jsp|jspx|do)?$ {
                  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_pass http://carlosxiao.cc;
                  }
       #所有静态文件由nginx直接读取不经过tomcat或resin
      location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|
      pdf|xls|mp3|wma)$
     {
         expires 15d;
     }
     location ~ .*.(js|css)?$
     {
         expires 1h;
     }
    access_log /data/logs/nginx/carlosxiao.log;
  }
}