Nginx反向代理时传递客户端真实IP&Nginx配置使用缓存机制&压力测试

win7客户端请求web服务,win7 ;

nginx作为反向代理服务器:192.168.88.130:8080 test.field.com ;

nginx作为后端web服务器:192.168.88.131:8080 www.field.com ;

httpd作为后端web服务器:192.168.88.131:80  www.field.com ;


1Nginx反向代理时传递客户端真实IP

ngx_http_proxy_module模块:实现反向代理的功能

http://nginx.org/en/docs/http/ngx_http_proxy_module.html

其中proxy_set_header指令就是该模块需要读取的配置文件。

Syntax:       proxy_set_header field value;

Default:     

proxy_set_headerHost $proxy_host;

proxy_set_headerConnection close;

Context:     http, server, location

参数说明:

proxy_set_header指令中,所有设置的值的含义和http请求中的含义完全相同, 常用有Host参数和X-Forward-For参数。

Host字段表示请求的主机名,当nginx作为反向代理服务器使用,如果后端服务器设置有防盗链或者根据http请求头中的host字段来进行路由或功能判断时,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败【默认反向代理服务器会向后端真实服务器发送请求,并且请求头中的host字段应为proxy_pass指令设置的服务器】。

X_Forward_For字段表示该条http请求是由谁发起的,如果反向代理服务器不重写该请求头,那么后端真实服务器在处理时会认为所有的请求都来自反向代理服务器,如果后端配置有防攻击策略,会导致机器就被封掉。因此,在nginx配置用作反向代理服务器时一般会增加两条配置,修改http的请求头:

proxy_set_header Host $http_host;

proxy_set_header X-Forward-For $remote_addr;

其中$http_host和$remote_addr都是nginx的导出变量,可以在配置文件中直接使用。如果Host请求头部没有出现在请求头中,则$http_host值为空,而$host值为主域名。因此,经常会用$host代替$http_host变量,从而避免http请求中丢失Host头部的情况下Host不被重写的失误。

 

proxy_set_header X-Forward-For $remote_addr;

将$remote_addr的值放进变量X-Real-IP中,此变量名可变,$remote_addr的值为客户端的ip

反向代理服务器IP传递过程见下图:


案例1、反向代理,不记录真实IP地址。

编辑配置文件:不记录真实IP地址。

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name localhost;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       root  /usr/share/nginx/html;

       # proxy_pass http://192.168.88.130/;

        index index.html index.htm;

    }

    location /field/ {

    proxy_pass http://192.168.88.131:8080/;

    }

    location ~* \.(mp3|avi|jpg|png|gif)$ {

     proxy_pass http://192.168.88.131;

    }

}

[[email protected]]# service nginx restart

停止 nginx:[确定]

正在启动 nginx:[确定]

win7访问:http://192.168.88.130:8080/field/

Nginx代理服务器访问记录

[[email protected]]# tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:19:00:26 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

192.168.88.1 - -[19/Apr/2018:19:01:06 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

192.168.88.1 - -[19/Apr/2018:19:03:30 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

Nginx后端服务器访问记录

由访问记录,未记录客户端真实IP,只记录反向代理服务器IP地址(192.168.88.130

[root@www html]#tail -f /var/log/nginx/access.log

192.168.88.130 - -[19/Apr/2018:19:00:26 +0800] "GET / HTTP/1.0" 304 0 "-""Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/65.0.3325.181 Safari/537.36""-"

192.168.88.130 -- [19/Apr/2018:19:01:06 +0800] "GET / HTTP/1.0" 304 0 "-""Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/65.0.3325.181 Safari/537.36""-"

192.168.88.130 -- [19/Apr/2018:19:03:30 +0800] "GET / HTTP/1.0" 304 0 "-""Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/65.0.3325.181 Safari/537.36" "-"

案例2、反向代理,配置记录客户端真实IP地址。

编辑配置文件:记录真实IP地址

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name localhost;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       root  /usr/share/nginx/html;

       # proxy_pass http://192.168.88.130/;

        index index.html index.htm;

    }

    location /field/ {

     proxy_pass http://192.168.88.131:8080/;

     proxy_set_headerHost $host;

     proxy_set_headerX-Real-IP $remote_addr;

#remote_addr代表客户端的IP,但它的值不是由客户端提供的,而是服务端根据客户端的ip指定的,当你的浏览器访问某个网站时,假设中间没有任何代理,那么网站的web服务器(NginxApache等)就会把remote_addr设为你的机器IP,如果你用了某个代理,那么你的浏览器会先访问这个代理,然后再由这个代理转发到网站,这样web服务器就会把remote_addr设为这台代理机器的IP    

#proxy_set_header X-Real-IP$remote_addr;可以真实的显示出客户端原始ip     

    proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;

#可以真实的显示出客户端原始ip    

    }

    location ~* \.(mp3|avi|jpg|png|gif)$ {

     proxy_pass http://192.168.88.131;

     proxy_set_header X-Real-IP $remote_addr;

    }

}

[root@test conf.d]#service nginx restart

停止 nginx:[确定]

正在启动 nginx:[确定]

Windows7访问:http://192.168.88.130:8080/field/

Nginx代理服务器访问记录:

[[email protected]]# tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:19:09:03 +0800] "GET /field/ HTTP/1.1" 192.168.88.130:8080304 0 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36""-"

192.168.88.1 - -[19/Apr/2018:19:09:04 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

192.168.88.1 - -[19/Apr/2018:19:09:04 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

192.168.88.1 - -[19/Apr/2018:19:09:04 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 304 0 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181Safari/537.36" "-"

Nginx后端服务器访问记录:

由访问记录,记录客户端真实IP192.168.88.1),以及反向代理服务器IP地址(192.168.88.130

[root@www html]#tail -f /var/log/nginx/access.log

192.168.88.130 - -[19/Apr/2018:19:09:03 +0800] "GET / HTTP/1.0" 192.168.88.130  304 0 "-" "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/65.0.3325.181 Safari/537.36" "192.168.88.1"

192.168.88.130 -- [19/Apr/2018:19:09:04 +0800] "GET / HTTP/1.0" 192.168.88.130  304 0 "-" "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/65.0.3325.181 Safari/537.36" "192.168.88.1"

192.168.88.130 -- [19/Apr/2018:19:09:04 +0800] "GET / HTTP/1.0" 192.168.88.130  304 0 "-" "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/65.0.3325.181 Safari/537.36" "192.168.88.1"

192.168.88.130 -- [19/Apr/2018:19:09:04 +0800] "GET / HTTP/1.0" 192.168.88.130  304 0 "-" "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/65.0.3325.181 Safari/537.36" "192.168.88.1"

 

使用ab为未添加缓存的Nginx做压力测试

其中参数n为请求的次数,c为一次请求的并发次数,-k为keep_alive,一般出现链接被重置的错误时建议带上-k的参数。

可以使用ab --help 查看ab其他参数

[root@test nginx]#  ab-n 2500 -c 800 http://192.168.88.130:8080/3.gif

This isApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to TheApache Software Foundation, http://www.apache.org/

 

Benchmarking192.168.88.130 (be patient)

Completed 250requests

Completed 500requests

Completed 750requests

Completed 1000requests

Completed 1250requests

Completed 1500requests

Completed 1750requests

Completed 2000requests

Completed 2250requests

Completed 2500requests

Finished 2500requests

 

ServerSoftware:        nginx/1.8.1

ServerHostname:        192.168.88.130

ServerPort:            8080

 

DocumentPath:          /3.gif

DocumentLength:        192 bytes

 

ConcurrencyLevel:      800

Time taken for tests:  37.590 seconds

Completerequests:      2500

Failedrequests:        2182

   (Connect: 0, Receive: 0, Length: 2182,Exceptions: 0)

Writeerrors:           0

Non-2xxresponses:      318

Totaltransferred:      3879324167 bytes

HTMLtransferred:       3878723219 bytes

Requests persecond:    66.51 [#/sec] (mean)

Time perrequest:       12028.666 [ms] (mean)

Time perrequest:       15.036 [ms] (mean, acrossall concurrent requests)

Transferrate:          100783.31 [Kbytes/sec]received

 

Connection Times(ms)

              min  mean[+/-sd] median   max

Connect:        0 1805 5328.2     18  31001

Processing:    18 3626 2889.9   2987  35338

Waiting:        4 2355 2533.3   2216  34878

Total:         36 5432 5993.9   4074  36918

 

Percentage ofthe requests served within a certain time (ms)

  50%  4074

  66%  5445

  75%  6130

  80%  6847

  90%  9466

  95% 17276

  98% 34225

  99%  34694

 100% 36918 (longest request)

[root@testnginx]#

由压测结果,800并发,2500请求耗时37.590 seconds


2Nginx反向代理配置使用缓存机制

proxy_cache_path:设置Nginx缓存应答功能

Syntax:      proxy_cache_pathpath [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time][max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time][loader_files=number] [loader_sleep=time] [loader_threshold=time][purger=on|off] [purger_files=number] [purger_sleep=time][purger_threshold=time];

Default:      —

Context:     http

缓存文件路径定义levels=1:2,一级子目录一个字符表示,2级子目录两个字符表示

总共有62*62*62个文件

   proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

file names in acache will look like this:

   /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c

配置示例:

    map $request_method $purge_method {

       PURGE   1;

        default 0;

    }

    server {

        add_header X-Via $server_addr;

                add_headerX-Cache $upstream_cache_status;

        ...

        location / {

            proxy_pass http://backend;

            proxy_cache cache_zone;

            proxy_cache_key $uri;

            proxy_cache_purge $purge_method;

        }

    }


$upstream_cache_status 包含以下几种状态:

MISS:未命中,请求被传送到后端

HIT:缓存命中

EXPIRED:缓存已经过期请求被传送到后端

UPDATING:正在更新缓存,将使用旧的应答

简单配置如下: 

配置缓存:nginx.conf

 proxy_cache_path  /cache/nginx/ levels=1:1keys_zone=mycache:32m;

加入头信息:default.conf

    add_header X-Via $server_addr;

    add_header X-Cache $upstream_cache_status;

启用缓存:

     proxy_cache mycache;

     proxy_cache_valid 200 1d;

#200 1d 表示这个zone中返回200的缓存文件如果在1天内没有被访问,那么文件会被cache manager进程删除掉    

     proxy_cache_valid 301 302 10m;

     proxy_cache_valid any 1m;

     proxy_cache_use_stale error http_500http_502 http_503 http_504;


案例3、Nginx反向代理,配置使用缓存机制。

编辑主配置文件:nginx.conf

[root@testnginx]# vi nginx.conf

user  nginx;

worker_processes  1;

error_log  /var/log/nginx/error.log warn;

pid        /var/run/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

#设置日志记录格式,添加$http_host$http_host 的值对应proxy_set_header Host $hostHost变量的值。

    log_format  main '$remote_addr - $remote_user [$time_local] "$request"$http_host'

                      '$status $body_bytes_sent"$http_referer" '

                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log  main;

   proxy_cache_path  /cache/nginx/levels=1:2 keys_zone=mycache:32m;

#keys_zone=mycache:32m 表示这个zone名称为mycache,分配的内存大小为32MB

#/cache/nginx/ 表示mycache这个zone的文件要存放的目录

#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录2个字符,即类似/cache/nginx/6/d4这种形式

    sendfile        on;

    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;

}

[[email protected]]# vi default.conf

server {

    listen      8080;

    server_name localhost;

    add_header X-Via$server_addr;

    add_header X-Cache$upstream_cache_status;

    #charset koi8-r;

    #access_log /var/log/nginx/log/host.access.log main;

    location / {

       root  /usr/share/nginx/html;

       # proxy_pass http://192.168.88.130/;       

        index index.html index.htm;

    }

    location /field/ {

     proxy_cache mycache;

     proxy_cache_valid200 1d;

#200 1d 表示这个zone中返回200的缓存文件如果在1天内没有被访问,那么文件会被cache manager进程删除掉    

     proxy_cache_valid 301 302 10m;

     proxy_cache_validany 1m;

    proxy_cache_use_stale error http_500 http_502 http_503 http_504;

     proxy_pass http://192.168.88.131:8080/;

     proxy_set_header Host $host;

     proxy_set_header X-Real-IP $remote_addr;

     proxy_set_header        X-Forwarded-For$proxy_add_x_forwarded_for;

    }

    location ~* \.(mp3|avi|jpg|png|gif)$ {

     proxy_cachemycache;

     proxy_cache_valid200 1d;

#请求码为200的缓存1

     proxy_cache_valid301 302 10m;

     proxy_cache_validany 1m;

    proxy_cache_use_stale error http_500 http_502 http_503 http_504;

#定义哪种场景可以使用过期缓存

     proxy_pass http://192.168.88.131;

     proxy_set_header X-Real-IP $remote_addr;

#可以真实的显示出客户端原始ip

    }

}

[[email protected]]# service nginx restart

停止 nginx:[确定]

正在启动 nginx:[确定]

[[email protected]]#

Win7访问:http://192.168.88.130:8080/field/

Nginx代理服务器访问记录:

查看代理服务器和后端服务器的地址,可以发现$http_host对应的值为192.168.88.130:8080192.168.88.130

[[email protected]]# tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:20:34:35 +0800] "GET /field/ HTTP/1.1" 192.168.88.130:8080 200 115 "-""Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101Firefox/59.0" "-"

192.168.88.1 - -[19/Apr/2018:20:41:43 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 115 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

192.168.88.1 - -[19/Apr/2018:20:41:45 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 115 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

192.168.88.1 - -[19/Apr/2018:20:41:45 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 115 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

服务器访问记录,第一次访问有送达,后续访问被缓存

[root@www html]#tail -f /var/log/nginx/access.log

192.168.88.130 - -[19/Apr/2018:20:34:35 +0800] "GET / HTTP/1.0" 192.168.88.130  200 115 "-" "Mozilla/5.0(Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "192.168.88.1"

缓存文件:

[root@test 12]#pwd

/cache/nginx/c/12

[root@test 12]#ll

总用量 4

-rw------- 1nginx nginx 547 4月  19 20:34b82aee8256e5e6793a95fbd482f3912c

[root@test 12]#cat b82aee8256e5e6793a95fbd482f3912c

"5ad88c74-73"

KEY: http://192.168.88.131:8080/

HTTP/1.1 200 OK

Server:nginx/1.8.1

Date: Thu, 19Apr 2018 12:34:35 GMT

Content-Type:text/html

Content-Length:115

Last-Modified:Thu, 19 Apr 2018 12:32:52 GMT

Connection:close

ETag:"5ad88c74-73"

X-Via:192.168.88.131

Accept-Ranges:bytes

<h1> TestNginx Cache </h1>

<p>body:httpdon web1.field.com</p>

<p> testsuccess!</p>

<p><em>Thank you !</em></p>

[root@test 12]#

Win7再次访问:http://192.168.88.130:8080/field/,缓存命中。


案例3、修改页面,验证Nginx缓存机制

修改页面,验证页面是否被缓存

[root@www html]# vi index.html

<h1> Test page Cache </h1>

<p>body:Nginx on www.field.com</p>

<p> test success!</p>

<p><em> Thank you !</em></p>

Windows7访问:http://192.168.88.130:8080/field/,页面被缓存,修改的主页无效。

131主机访问:http://192.168.88.130:8080/field/

页面被缓存,修改无效

[root@test yzw]#curl http://192.168.88.130:8080/field/

<h1> TestNginx Cache </h1>

<p>body:httpdon web1.field.com</p>

<p> testsuccess!</p>

<p><em>Thank you !</em></p>

[root@test yzw]#curl http://192.168.88.130:8080/field/

<h1> TestNginx Cache </h1>

<p>body:httpdon web1.field.com</p>

<p> testsuccess!</p>

<p><em>Thank you !</em></p>

[root@test yzw]#

Nginx代理服务器访问记录

[[email protected]]# tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:20:49:41 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 115 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

192.168.88.130 -- [19/Apr/2018:20:51:51 +0800] "GET /field/ HTTP/1.1" 192.168.88.130:8080200 115 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu)libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""-"

192.168.88.130 -- [19/Apr/2018:20:51:54 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 115 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu)libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""-"

删除缓存文件,重新加载更新的主页

[root@test ~]#cd /cache/nginx/

[root@testnginx]# ll

总用量 4

drwx------ 3nginx nginx 4096 4月  19 20:34 c

[root@testnginx]# rm -rf *

Windows7访问:http://192.168.88.130:8080/field/,未命中缓存,主页更新成功。

131主机访问:http://192.168.88.130:8080/field/

[root@test yzw]# curl http://192.168.88.130:8080/field/

<h1> Testpage Cache </h1>

<p>body:Nginxon www.field.com</p>

<p> test success!</p>

<p><em>Thank you !</em></p>

[root@test yzw]# curl http://192.168.88.130:8080/field/

<h1> Testpage Cache </h1>

<p>body:Nginxon www.field.com</p>

<p> testsuccess!</p>

<p><em>Thank you !</em></p>

[root@test yzw]#

Nginx代理服务器访问记录:

[root@test conf.d]#tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:20:54:32 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 113 "-" "Mozilla/5.0 (Windows NT 6.1;Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

192.168.88.130 -- [19/Apr/2018:20:57:02 +0800] "GET /field/ HTTP/1.1"192.168.88.130:8080 200 113 "-" "curl/7.19.7(x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18libssh2/1.4.2" "-"

192.168.88.130 -- [19/Apr/2018:20:57:04 +0800] "GET /field/ HTTP/1.1" 192.168.88.130:8080200 113 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu)libcurl/7.19.7 NSS/3.27.1 zlib/1.2.3 libidn/1.18 libssh2/1.4.2""-"

Nginx后端服务器访问记录,第一次访问有送达,后续访问被缓存

[root@www html]#tail -f /var/log/nginx/access.log

192.168.88.130 - - [19/Apr/2018:20:54:32 +0800] "GET /HTTP/1.0" 192.168.88.130  200 113"-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0)Gecko/20100101 Firefox/59.0" "192.168.88.1"

缓存文件:

[root@test 12]#pwd

/cache/nginx/c/12

[root@test 12]#

[root@test 12]#cat b82aee8256e5e6793a95fbd482f3912c

"5ad8903a-71"

KEY:http://192.168.88.131:8080/

HTTP/1.1 200 OK

Server:nginx/1.8.1

Date: Thu, 19Apr 2018 12:54:32 GMT

Content-Type:text/html

Content-Length:113

Last-Modified:Thu, 19 Apr 2018 12:48:58 GMT

Connection:close

ETag:"5ad8903a-71"

X-Via:192.168.88.131

Accept-Ranges:bytes

<h1> Testpage Cache </h1>

<p>body:Nginxon www.field.com</p>

<p> testsuccess!</p>

<p><em>Thank you !</em></p>

案例5、反向代理到http服务器

Windows7访问:http://192.168.88.130:8080/3.gif

反向代理到131服务器80端口的http服务器

Nginx代理服务器访问记录

[[email protected]]# tail -f /var/log/nginx/access.log

192.168.88.1 - -[19/Apr/2018:21:00:23 +0800] "GET /3.gif HTTP/1.1"192.168.88.130:8080 200 1772348 "-" "Mozilla/5.0 (Windows NT6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

192.168.88.1 - -[19/Apr/2018:21:00:23 +0800] "GET /3.gif HTTP/1.1"192.168.88.130:8080 200 1772348 "-" "Mozilla/5.0 (Windows NT6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0" "-"

缓存文件

[root@test d4]#pwd

/cache/nginx/6/d4

[root@test d4]#head 4a9e77e765e70454a1294fe72ee8fd46

f??,?撘Z?c?2·"dbee2-1b0b3c-56a1dcc6f5f00"

KEY:http://192.168.88.131/3.gif

HTTP/1.1 200 OK

Date: Thu, 19Apr 2018 13:00:22 GMT

Server:Apache/2.2.15 (CentOS)

Last-Modified:Wed, 18 Apr 2018 11:31:40 GMT

ETag:"dbee2-1b0b3c-56a1dcc6f5f00"

Accept-Ranges:bytes

Content-Length:1772348

Connection:close

[root@test d4]#


3性能测试

使用ab为添加缓存的Nginx做压力测试

其中参数n为请求的次数,c为一次请求的并发次数,-k为keep_alive,一般出现链接被重置的错误时建议带上-k的参数。

可以使用ab --help 查看ab其他参数

[root@test ~]# ab -n 2500 -c 800http://192.168.88.130:8080/3.gif

This isApacheBench, Version 2.3 <$Revision: 655654 $>

Copyright 1996Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Licensed to TheApache Software Foundation, http://www.apache.org/

 

Benchmarking192.168.88.130 (be patient)

Completed 250requests

Completed 500requests

Completed 750requests

Completed 1000requests

Completed 1250requests

Completed 1500requests

Completed 1750requests

Completed 2000requests

Completed 2250requests

Completed 2500requests

Finished 2500requests

 

Server Software:        nginx/1.8.1

ServerHostname:        192.168.88.130

ServerPort:            8080

 

DocumentPath:          /3.gif

DocumentLength:        192 bytes

 

ConcurrencyLevel:      800

Time taken for tests:  2.256 seconds

Completerequests:      2500

Failedrequests:        1910

   (Connect: 0, Receive: 0, Length: 1910,Exceptions: 0)

Writeerrors:           0

Non-2xxresponses:      590

Totaltransferred:      3490650695 bytes

HTMLtransferred:       3489964772 bytes

Requests persecond:    1108.07 [#/sec] (mean)

Time perrequest:       721.977 [ms] (mean)

Time perrequest:       0.902 [ms] (mean, acrossall concurrent requests)

Transferrate:          1510890.12 [Kbytes/sec]received

 

Connection Times(ms)

              min  mean[+/-sd] median   max

Connect:        0 195 382.5      9    1006

Processing:    14 330 230.5    341     934

Waiting:        1  76 177.1      9     619

Total:         47 525 566.8    348    1937

 

Percentage ofthe requests served within a certain time (ms)

  50%   348

  66%   382

  75%   425

  80%   427

  90%  1713

  95%  1924

  98%  1933

  99%  1934

 100%  1937 (longest request)

[root@test ~]#

由压测结果,800并发,2500请求耗时2.256 seconds,效果明显。

 




猜你喜欢

转载自blog.csdn.net/field_yang/article/details/80017578