• 一、Nginx配置文件详解


    配置文件介绍
      主要有两部分:分别是
        main:主体部分
        http{}:虚拟主机配置部分
        
       配置指令主要以分号结尾;配置语法:directive value1 [value2 ....]
       支持使用的变量
           模块内置的变量
           自定义变量:set var_name value
       
       主配置段的指令类别:
      用于调试和定位问题:
       (1)daemon [on|off]:  是否以守护进程的方式启动nginx;
       (2)master_press [on|off]:  是否以master/worker模型来运行nginx;
       (3)error_log /path/to/error_loglevel:  指明错误日志文件级别,处于调试目的,可以使用debug级别,但次级别只有在编译nginx时使用了--with-debug选项才有效 ;
           
      正常运行必备的配置:
       (1)user USERNAME [GROUPNAME]:指定运行worker的用户和用户组;例如 user nginx nginx 
       (2)pid /path/to/nginx.pid : 指定pid文件
       (3)worker_rlimit_nofile # : 指定一个worker进程能够打开的最大文件句柄数
       (4)worker_rlimit_sigpending # : 指定每个用户能够发往worker信号的数量
           
      优化性能相关的配置:
       (1)worker_processes # :worker进程的个数,通常是cpu核心数减1
       (2)worker_cpu_affinity cpuumask :绑定worker进程至指定的CPU上
       (3)timer-resolution t :时间解析度,在x86服务器上可以不用配置
       (4)worker_priority NICE :调整nice值(-20,19);nice值越大,越优先分配cpu
           
      事件相关的配置;
       (1)accept_mutex [on|off] :内部调动用户请求至各worker时的负载均衡锁;启用时表示能够让多个worker轮流的、序列化的响应请求
       (2)lock_file /path/to/lock_file :指定锁文件
       (3)accept_mutex_delay #ms: 取得负载均衡锁的时间
       (4)use [epoll|poll|select|rgsig]:定义使用的事件模型,建议让nginx自动选择
       (5)worker_connections #:每个worker进程所能够响应的最大并发请求数

    nginx.conf

     1 worker_connections 64;
     2 }
     3 
     4 http {
     5 include /usr/local/nginx/conf/mime.types;
     6 default_type application/octet-stream;
     7 
     8 #charset gb2312;
     9 
    10 server_names_hash_bucket_size 128;
    11 client_header_buffer_size 32k;
    12 large_client_header_buffers 4 32k;
    13 
    14 keepalive_timeout 60;
    15 
    16 fastcgi_connect_timeout 300;
    17 fastcgi_send_timeout 300;
    18 fastcgi_read_timeout 300;
    19 fastcgi_buffer_size 128k;
    20 fastcgi_buffers 4 128k;
    21 fastcgi_busy_buffers_size 128k;
    22 fastcgi_temp_file_write_size 128k;
    23 client_body_temp_path /usr/local/nginx/client_body_temp;
    24 proxy_temp_path /usr/local/nginx/proxy_temp;
    25 fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    26 
    27 gzip on;
    28 gzip_min_length 1k;
    29 gzip_buffers 4 16k;
    30 gzip_http_version 1.0;
    31 gzip_comp_level 2;
    32 gzip_types text/plain application/x-javascript text/css application/xml;
    33 gzip_vary on;
    34 
    35 client_header_timeout 3m;
    36 client_body_timeout 3m;
    37 send_timeout 3m;
    38 sendfile on;
    39 tcp_nopush on;
    40 tcp_nodelay on;
    41 #设定虚拟主机
    42 include /usr/local/nginx/conf/vhost/aa.test.com;
    43 }

     一、Nginx的主配置文件详解(nginx.conf)

      1 一、Nginx的主配置文件详解(nginx.conf)
      2 
      3 user nobody nobody;   #运行用户与组
      4 
      5 worker_processes 6;   #启动进程,通常设置与CPU数相同
      6 
      7 worder_cpu_affinity 4;  #明确指定使用哪些CPU 如使用1、3核心: 1000 0010
      8 
      9 worker_rlimit_nofile 51200; #设置最大系统连接数
     10 
     11 worker_priority 1;   #指定调用CPU优先级;取值范围(-20~20)
     12 
     13 #error_log logs/error.log; 
     14 
     15 #error_log logs/error.log notice; #错误日志及日志级别;日志级别有:(debug|info|notice|warn|error|crit|alert|emerg)
     16 
     17 #error_log logs/error.log info;
     18 
     19 #pid logs/nginx.pid;   #PID文件路径
     20 
     21 lock_file logs/nginx.lock;  #锁文件路径
     22 
     23 events {    #事件模块
     24 
     25 use epoll;   #指定事件驱动模型;(kqueue|rtsig|epoll|select|poll|eventport)
     26 
     27 worker_connections 51200; #定义连接数限制;当超过1024时,须使用"ulimit -n"来解除系统连接限制
     28 
     29 }
     30 
     31 http {      #设置HTTP服务器模块
     32 
     33 include mime.types; #设置MIME类型
     34 
     35 default_type application/octet-stream; #默认文件系统
     36 
     37 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #设置日志格式
     38 
     39 #   '$status $body_bytes_sent "$http_referer" '
     40 
     41 #   '"$http_user_agent" "$http_x_forwarded_for"';
     42 
     43 #access_log logs/access.log main; #设置访问日志及日志格式
     44 
     45 sendfile on;   #指定Nginx是否调用sedfile函数
     46 
     47 #autoindex on;  #开启索引功能,适合下载服务器,默认关闭
     48 
     49 #tcp_nopush on;  #防止网络阻塞
     50 
     51 #tcp_nodelay on;  #防止网络阻塞
     52 
     53 #keepalive_timeout 0;
     54 
     55 keepalive_timeout 65;    #连接超时时间,单位秒
     56 
     57 server_names_hash_bucket_size 128; #服务器名字Hash表大小
     58 
     59 large_client_header_buffers 4 32k; #设定请求缓存大小
     60 
     61 client_header_buffer_size 2k; #客户端请求头部缓冲区大小
     62 
     63 client_body_buffer_size 512k; #客户端请求主休缓冲区大小
     64 
     65 client_header_timeout 60;  #客户端请求头部超时时间
     66 
     67 client_max_body_size 8M;  #设置通过Nginx上传的文件大小
     68 
     69 proxy_connect_timeout 20; #代理连接超时时间
     70 
     71 proxy_send_timeout 60;  #代理发送超时时间
     72 
     73 proxy_read_timeout 20;  #代理接收超时时间
     74 
     75 proxy_buffer_size 16k;  #代理服务器保存用户头信息的缓冲区大小
     76 
     77 proxy_buffers  4 64k;  #存放后台服务器缓冲区数量与大小
     78 
     79 proxy_busy_buffers_size 128k;  #高负荷下缓冲大小
     80 
     81 proxy_temp_file_write_size 128k; #设置缓存文件夹大小
     82 
     83 proxy_temp_path /usr/cache_one; #设置缓存文件夹位置
     84 
     85 ######指定缓冲区的路径、存储方式及大小
     86 
     87 proxy_cache_path /usr/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
     88 
     89 gzip on;     #开启Gzip压缩功能
     90 
     91 gzip_min_length 1k;  #最小压缩文件大小
     92 
     93 gzip_buffers 4 16k;  #压缩缓存区
     94 
     95 gzip_http_version 1.1;  #压缩版本
     96 
     97 gzip_proxied off;   #在代理中是否启用压缩功能
     98 
     99 gzip_comp_level 5;   #压缩级别;默认为1,取值范围(1-9)
    100 
    101 ######压缩文件类型
    102 
    103 gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
    104 
    105 gzip_vary on;
    106 
    107 upstream allen { #定义负载均衡配置,必须定义在"server"之外
    108 
    109 ip_hash;  #基于客户端IP地址完成请求的分发,可以实现同一个客户端的请求发送到同一台服务器;有三种调度算法:轮询(round-robin)、ip哈希(ip_hash)和最少连接(least_conn)
    110 
    111 server 172.16.14.2 weight=1;#定义一个后端服务器;weight:权重 max_fails:最大失败连接次数,失败连接超时时间由fail_timeout设置 fail_timeout:等待请求目标服务器发送响应时长 backup:所有服务器都故障时才启用此服务器 down:手动记录此服务器不在做任何处理请求
    112 
    113 server 172.16.14.3 weight=1;
    114 
    115 }  
    116 
    117 server {      #定义一个虚拟主机,可以定义多个
    118 
    119 listen 80;    #监听端口
    120 
    121 server_name www.allen.com; #定义主机名称
    122 
    123 #charset koi8-r;   #设置字符集
    124 
    125 #access_log logs/host.access.log main;
    126 
    127 location / {    #可以定义多个,也可以嵌套
    128 
    129 root /web;    #设置网站文件存放目录
    130 
    131 index index.php index.html index.htm; #设置默认访问主页
    132 
    133 }
    134 
    135 location /status { 
    136 
    137   stub_status on;  #开启Nginx状态监测
    138 
    139 access_log off;  #关闭日志访问
    140 
    141 }
    142 
    143 location ~ .php$ {     #定义以".php"结尾的文件
    144 
    145 root  /web;      #定义php服务器网站存放目录
    146 
    147 fastcgi_pass 172.16.14.2:9000;  #定义fastcgi服务器地址及端口
    148 
    149 fastcgi_index index.php; #定义默认访问主页
    150 
    151 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    152 
    153   include fastcgi_params;  #定义fastcgi访问文件
    154 
    155 }
    156 
    157 #location ~ .*.(gif|jep|jpeg|png|bmp|swf|js|css|js|)$ {
    158 
    159 # proxy_cache cache_one;   #设置缓存区
    160 
    161 # proxy_cache_valid 200 304 12h; #设置缓存时间
    162 
    163 # proxy_cache_valid 301 302 1m;
    164 
    165 # proxy_cache_valid any 1m;
    166 
    167 # proxy_set_header Host $host; #设置发送到代理服务器请求头部添加额外信息
    168 
    169 # proxy_set_header X-Real-IP $remote_addr;
    170 
    171 # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    172 
    173 # proxy_pass http://allen;
    174 
    175 #}
    176 
    177 #location / {
    178 
    179 # if ($request_method == “PUT”) { #如果客户请求动作为"PUT"就转到定义的地址
    180 
    181 # proxy_pass http://www.allen.com:8080;
    182 
    183 # }
    184 
    185 # if ($request_uri ~ ".(jpg|gif|jpeg|png)$") { #如果客户端请求"uri"有已经定义的文件结尾的文件就转到定义好的地址然后跳出
    186 
    187 # proxy_pass http://imageservers;
    188 
    189 # break;
    190 
    191 # }
    192 
    193 #if ($http_user_agent ~ MSIE) { #如果客户端请求使用的浏览器是IE,不管请求地址是什么都转到"/msie/"下面并跳出
    194 
    195 #rewrite ^(.*)$ /msie/$1 break;
    196 
    197 注释:
    198 
    199 ·last - 完成重写指令,之后搜索相应的URI或location。
    200 
    201 ·break - 完成重写指令。
    202 
    203 ·redirect - 返回302临时重定向,如果替换字段用http://开头则被使用。
    204 
    205 ·permanent - 返回301永久重定向
    206 
    207 #}
    208 
    209 #}
    210 
    211 #error_page 404  /404.html; 
    212 
    213 # redirect server error pages to the static page /50x.html
    214 
    215 #
    216 
    217 error_page 500 502 503 504 /50x.html; #定义错误返回页面
    218 
    219 location = /50x.html {
    220 
    221   root html;
    222 
    223 }
    224 
    225 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    226 
    227 #
    228 
    229 #location ~ .php$ {
    230 
    231 # proxy_pass http://127.0.0.1;  #定义以".php"结尾的全部转到指定的地址
    232 
    233 #}
    234 
    235 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    236 
    237 #
    238 
    239 # deny access to .htaccess files, if Apache's document root
    240 
    241 # concurs with nginx's one
    242 
    243 #
    244 
    245 #location ~ /.ht {
    246 
    247 # deny all;
    248 
    249 #}
    250 
    251 }
    252 
    253 # another virtual host using mix of IP-, name-, and port-based configuration
    254 
    255 #
    256 
    257 #server {
    258 
    259 # listen 8000;
    260 
    261 # listen somename:8080;
    262 
    263 # server_name somename alias another.alias;
    264 
    265 # location / {
    266 
    267 # root html;
    268 
    269 # index index.html index.htm;
    270 
    271 # }
    272 
    273 #}
    274 
    275 # HTTPS server
    276 
    277 #
    278 
    279 #server {    #定义一个HTTPS服务器
    280 
    281 # listen 443;
    282 
    283 # server_name localhost;
    284 
    285 # ssl   on;
    286 
    287 # ssl_certificate cert.pem;  #私钥文件
    288 
    289 # ssl_certificate_key cert.key;  #颁发的证书
    290 
    291 # ssl_session_timeout 5m;   #会话超时时间
    292 
    293 # ssl_protocols SSLv2 SSLv3 TLSv1; #SSL协议版本
    294 
    295 # ssl_ciphers HIGH:!aNULL:!MD5;
    296 
    297 # ssl_prefer_server_ciphers on;
    298 
    299 # location / {
    300 
    301 # root html;
    302 
    303 # index index.html index.htm;
    304 
    305 # }
    306 
    307 #}
    308 
    309 }
    310 
    311 if语句
    312 
    313 语法:if (condition) { ... }
    314 
    315 默认值:none
    316 
    317 使用字段:server, location
    318 
    319 判断一个条件,如果条件成立,则后面的大括号内的语句将执行,相关配置从上级继承。
    320 
    321 可以在判断语句中指定下列值:
    322 
    323 ·一个变量的名称;不成立的值为:空字符传""或者一些用“0”开始的字符串。
    324 
    325 ·一个使用=或者!=运算符的比较语句。
    326 
    327 ·使用符号~*和~模式匹配的正则表达式:
    328 
    329 URL重写模块(Rewrite)
    330 
    331 ·~为区分大小写的匹配。
    332 
    333 ·~*不区分大小写的匹配(allen匹配Allen)。
    334 
    335 ·!~和!~*意为“不匹配的”。
    336 
    337 ·使用-f和!-f检查一个文件是否存在。
    338 
    339 ·使用-d和!-d检查一个目录是否存在。
    340 
    341 ·使用-e和!-e检查一个文件,目录或者软链接是否存在。
    342 
    343 ·使用-x和!-x检查一个文件是否为可执行文件。
    344 
    345 正则表达式的一部分可以用圆括号,方便之后按照顺序用$1-$9来引用。
    346 
    347 示例配置:
    348 
    349 if ($http_cookie ~* "id=([^;] +)(?:;|$)" ) {
    350 set $id $1;
    351 }
    352 if ($request_method = POST ) {
    353 return 405;
    354 }
    355 if (!-f $request_filename) {
    356 break;
    357 proxy_pass http://127.0.0.1;
    358 }
    359 if ($slow) {
    360 limit_rate 10k;
    361 }
    362 if ($invalid_referer) {
    363 return 403;
    364 }
    365 if ($args ^~ post=140){
    366 rewrite ^ http://example.com/ permanent;
    367 }
    Nginx.conf

    Nginx负载均衡配置

    下面使用一个案例来介绍Nginx如何配置负载均衡:

    1、编译安装Nginx

     1 ######安装Nginx依赖环境
     2 [root@nginx ~]# yum -y groupinstall "Development tools" "Server Platform Development"
     3 [root@nginx ~]# yum -y install pcre-devel
     4 =====================================================
     5 ######添加Nginx运行用户
     6 [root@nginx ~]# useradd -r nginx
     7 [root@nginx ~]# tar xf nginx-1.4.2.tar.gz
     8 [root@nginx ~]# cd nginx-1.4.2
     9 ######编译安装Nginx
    10 [root@nginx nginx-1.4.2]# ./configure
    11 >   --prefix=/usr                      #Nginx安装目录
    12 >   --sbin-path=/usr/sbin/nginx        #nginx执行程序安装路径
    13 >   --conf-path=/etc/nginx/nginx.conf  #Nginx主配置文件存放路径
    14 >   --error-log-path=/var/log/nginx/error.log   #日志文件存放路径
    15 >   --http-log-path=/var/log/nginx/access.log
    16 >   --pid-path=/var/run/nginx/nginx.pid         #PID文件存放路径
    17 >   --lock-path=/var/lock/nginx.lock            #锁文件存放路径
    18 >   --user=nginx                                #指定运行Nginx用户
    19 >   --group=nginx                               #指定运行Nginx组
    20 >   --with-http_ssl_module                      #开启SSL加密模块
    21 >   --with-http_flv_module                      #支持flv流媒体模块
    22 >   --with-http_stub_status_module              #开启状态检测模块
    23 >   --with-http_gzip_static_module              #开启gzip静态压缩模块
    24 >   --http-client-body-temp-path=/var/tmp/nginx/client/ #客户端请求的缓存目录
    25 >   --http-proxy-temp-path=/var/tmp/nginx/proxy/        #代理缓存目录
    26 >   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/       #fcgi缓存目录
    27 >   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi         #uwsgi缓存目录
    28 >   --http-scgi-temp-path=/var/tmp/nginx/scgi           #scgi缓存目录
    29 >   --with-pcre                                          #启动正则表达式
    30 [root@nginx nginx-1.4.2]# make && make install

    2、为Nginx提供Sysv服务脚本 

      1 #!/bin/sh
      2 # nginx - this script starts and stops the nginx daemon
      3 # chkconfig:   - 85 15
      4 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse
      5 #               proxy and IMAP/POP3 proxy server
      6 # processname: nginx
      7 # config:      /etc/nginx/nginx.conf
      8 # config:      /etc/sysconfig/nginx
      9 # pidfile:     /var/run/nginx.pid
     10 # Source function library.
     11 . /etc/rc.d/init.d/functions
     12 # Source networking configuration.
     13 . /etc/sysconfig/network
     14 # Check that networking is up.
     15 [ "$NETWORKING"= "no" ] && exit 0
     16 nginx="/usr/sbin/nginx"
     17 prog=$(basename$nginx)
     18 NGINX_CONF_FILE="/etc/nginx/nginx.conf"
     19 [ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx
     20 lockfile=/var/lock/subsys/nginx
     21 make_dirs() {
     22    # make required directories
     23    user=`nginx -V 2>&1 |grep "configure arguments:"| sed 's/[^*]*--user=([^ ]*).*/1/g' -`
     24    options=`$nginx -V 2>&1 |grep 'configure arguments:'`
     25    foropt in $options; do
     26        if[ `echo $opt | grep '.*-temp-path'` ]; then
     27            value=`echo $opt | cut -d"=" -f 2`
     28            if [ ! -d "$value"]; then
     29                # echo "creating" $value
     30                mkdir -p $value && chown -R $user $value
     31            fi
     32        fi
     33    done
     34 }
     35 start() {
     36     [ -x $nginx ] ||exit 5
     37     [ -f $NGINX_CONF_FILE ] ||exit 6
     38     make_dirs
     39     echo-n $"Starting $prog: "
     40     daemon $nginx -c $NGINX_CONF_FILE
     41     retval=$?
     42     echo
     43     [ $retval -eq0 ] && touch $lockfile
     44     return$retval
     45 }
     46 stop() {
     47     echo-n $"Stopping $prog: "
     48     killproc $prog -QUIT
     49     retval=$?
     50     echo
     51     [ $retval -eq0 ] && rm -f $lockfile
     52     return$retval
     53 }
     54 restart() {
     55     configtest ||return $?
     56     stop
     57     sleep1
     58     start
     59 }
     60 reload() {
     61     configtest ||return $?
     62     echo-n $"Reloading $prog: "
     63     killproc $nginx -HUP
     64     RETVAL=$?
     65     echo
     66 }
     67 force_reload() {
     68     restart
     69 }
     70 configtest() {
     71   $nginx -t -c $NGINX_CONF_FILE
     72 }
     73 rh_status() {
     74     status $prog
     75 }
     76 rh_status_q() {
     77     rh_status >/dev/null2>&1
     78 }
     79 case "$1" in
     80     start)
     81         rh_status_q && exit0
     82         $1
     83         ;;
     84     stop)
     85         rh_status_q || exit0
     86         $1
     87         ;;
     88     restart|configtest)
     89         $1
     90         ;;
     91     reload)
     92         rh_status_q || exit7
     93         $1
     94         ;;
     95     force-reload)
     96         force_reload
     97         ;;
     98     status)
     99         rh_status
    100         ;;
    101     condrestart|try-restart)
    102         rh_status_q || exit0
    103             ;;
    104     *)
    105         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    106         exit 2
    107 esac
    为Nginx提供Sysv服务脚本

    3、将Nginx加入到系统服务并启动

    1 [root@nginx ~]# chmod +x /etc/rc.d/init.d/nginx
    2 [root@nginx ~]# chkconfig --add nginx
    3 [root@nginx ~]# service nginx start
    正在启动 nginx:                                           [确定]
    4、查看Nginx进程并访问测试
    [root@nginx ~]# ps aux|grep nginx
    root      2557  0.0  0.2  44444   816 ?        Ss   15:20   0:00 nginx: master process/usr/sbin/nginx -c/etc/nginx/nginx.conf
    nginx     2558  0.7  0.5  44852  1508 ?        S    15:20   0:00 nginx: worker process

     1 5、在WEB1服务器上安装Httpd并启动
     2 
     3 1
     4 2
     5 [root@web1 ~]# yum -y install httpd
     6 [root@web1 ~]# service httpd start
     7 6、为WEB1服务器提供一个测试页面并做访问测试
     8 
     9 1
    10 [root@web1 ~]# echo 'web1 172.16.14.2' > /var/www/html/index.html

    1 7、在WEB2服务器上面安装Httpd并启动
    2 
    3 [root@web2 ~]# yum -y install httpd
    4 [root@web2 ~]# service httpd start
    5 8、为WEB2服务器提供一个测试页面并做访问测试
    6 
    7 [root@web2 ~]# echo 'web1 172.16.14.3' > /var/www/html/index.html

     1 修改主配置文件
     2 
     3 #user  nobody;
     4 worker_processes  4;
     5 #error_log  logs/error.log;
     6 #error_log  logs/error.log  notice;
     7 #error_log  logs/error.log  info;
     8 worker_rlimit_nofile    51200;
     9 #pid        logs/nginx.pid;
    10 events {
    11     use epoll;
    12     worker_connections  1024;
    13 }
    14 http {
    15     include       mime.types;
    16     default_type  application/octet-stream;
    17     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    18     #                  '$status $body_bytes_sent "$http_referer" '
    19     #                  '"$http_user_agent" "$http_x_forwarded_for"';
    20     #access_log  logs/access.log  main;
    21     server_names_hash_bucket_size 128;
    22     client_header_buffer_size 32k;
    23     large_client_header_buffers 4 32k;
    24     client_max_body_size 300m;
    25     client_body_buffer_size 512k;
    26     sendfile        on;
    27     tcp_nopush      on;
    28     keepalive_timeout  65;
    29     proxy_connect_timeout   20;
    30     proxy_send_timeout      60;
    31     proxy_read_timeout      20;
    32     proxy_buffer_size       16k;
    33     proxy_buffers           4 64k;
    34     proxy_busy_buffers_size 128k;
    35     proxy_temp_file_write_size 128k;
    36     gzip on;
    37     gzip_min_length  1k;
    38     gzip_buffers     4 16k;
    39     gzip_http_version 1.1;
    40     gzip_comp_level 5;
    41     gzip_vary on;
    42     upstream allen {
    43         server 172.16.14.2;
    44         server 172.16.14.3;
    45      }
    46     server {
    47         listen       80;
    48         server_name  localhost;
    49         #charset koi8-r;
    50         #access_log  logs/host.access.log  main;
    51         location / {
    52             proxy_pass  http://allen;
    53             index  index.html index.htm;
    54         }
    55         # redirect server error pages to the static page /50x.html
    56         #
    57         error_page   500 502 503 504  /50x.html;
    58         location = /50x.html {
    59             root   html;
    60         }
    61 62

    2、测试Nginx主配置文件语法,并重新加载

    1
    2
    3
    4
    [root@nginx ~]# nginx -t
    nginx: the configurationfile /etc/nginx/nginx.conf syntax is ok
    nginx: configurationfile /etc/nginx/nginx.conftest is successful
    [root@nginx ~]# service nginx reload

    3、访问Nginx主机验证是否实现负载均衡,有时可能需要多刷新几下

     1 Nginx实现后端健康状态检测
     2 
     3 1、修改Nginx主配置文件如下
     4 
     5     upstream allen {
     6         server 172.16.14.2      backup;
     7         server 172.16.14.3      max_fails=3 fail_timeout=2s;
     8  9 注释:
    10 backup:作为备份服务器,当所有服务器都停止了服务,此时backup备份机会启用
    11 max_fails:请求超时后,最大检测后端服务器次数
    12 fail_timeout:超时时间;默认为10s
    13 2、重新加载配置文件
    14 
    15 1
    16 [root@nginx ~]# service nginx reload
    17 3、测试是否访问的页面为WEB2服务器的

    4、停止WEB2服务器的Httpd服务,验证是否请求的为WEB1服务器内容

    1
    2
    [root@web2 ~]# service httpd stop
    Stopping httpd:                                            [  OK  ]

    5、把WEB2服务器Httpd服务启动,此时请求到的内容为WEB2服务器;这里就不在测试了

    给Nginx添加第三方模块实现过载保护
     1 给Nginx添加第三方模块实现过载保护
     2 
     3 1、下载扩展补丁包,解压并打补丁  点此下载
     4 
     5 [root@nginx ~]# unzip nginx-http-sysguard-master.zip
     6 [root@nginx ~]# cd nginx-1.4.2
     7 [root@nginx nginx-1.4.2]# patch -p1 < ../nginx-http-sysguard-master/nginx_sysguard_1.3.9.patch
     8 2、重新编译安装Nginx
     9 
    10 [root@nginx nginx-1.4.2]# ./configure
    11 >   --prefix=/usr
    12 >   --sbin-path=/usr/sbin/nginx
    13 >   --conf-path=/etc/nginx/nginx.conf
    14 >   --error-log-path=/var/log/nginx/error.log
    15 >   --http-log-path=/var/log/nginx/access.log
    16 >   --pid-path=/var/run/nginx/nginx.pid 
    17 >   --lock-path=/var/lock/nginx.lock
    18 >   --user=nginx
    19 >   --group=nginx
    20 >   --with-http_ssl_module
    21 >   --with-http_flv_module
    22 >   --with-http_stub_status_module
    23 >   --with-http_gzip_static_module
    24 >   --http-client-body-temp-path=/var/tmp/nginx/client/
    25 >   --http-proxy-temp-path=/var/tmp/nginx/proxy/
    26 >   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
    27 >   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
    28 >   --http-scgi-temp-path=/var/tmp/nginx/scgi
    29 >   --with-pcre
    30 >   --add-module=/root/nginx-http-sysguard-master/
    31 [root@nginx nginx-1.4.2]# make && make install
    32 3、修改Nginx配置文件添加过载保护;这里说明下为了测试方便,把CPU负载调的比较低
    33 
    34 
    35 [root@nginx nginx-1.4.2]# cd /etc/nginx/
    36 [root@nginx nginx]# vim nginx.conf
    37 ######在"server"模块中添加如下内容
    38         sysguard on;   #开启过载保护功能
    39         sysguard_load load=0.3 action=/loadlimit;   #cpu负载超过0.3就保护,"action"过载保护动作
    40         #sysguard_mem swapratio=20% action=/swaplimit; #交换分区过载保
    41         #sysguard_mem free=100M action=/freelimit;     #内存过载保护
    42         location/loadlimit {
    43             return 503;    #系统过载返回503
    44         }
    45 注释:由于新添加了功能需要重启Nginx服务
    46 [root@nginx nginx]# service nginx restart
    47 4、安装"htop"工具,监测负载
    48 
    49 1
    50 2
    51 [root@nginx ~]# yum -y install htop
    52 [root@nginx ~]# htop

    5、在另一台主机使用apache自带的压力测试工具ab做压测;在压测前先访问一下是否正常如:

    1
    2
    ######做压力测试
    [root@web1 ~]# ab -n 50000 -c 1000 http://172.16.14.1/index.html

    6、查看Nginx服务器的负载状况

    7、测试访问Nginx服务器是否能正常访问

  • 相关阅读:
    Python笔记 【无序】 【五】
    Python笔记 【无序】 【四】
    Python 面向对象【2】
    Python 面向对象【1】
    OpenCV 入门
    Python笔记 【无序】 【三】
    js制作秒表
    C语言No such file or directory错误
    js注册实现
    js中setTimeout和setInterval的应用方法(转)
  • 原文地址:https://www.cnblogs.com/nb-blog/p/5268870.html
Copyright © 2020-2023  润新知