实验环境:
NGINX CentOS 7.2x86_64 IP:172.16.253.94
LAMP CentOS 6.7x86_64 IP:192.168.1.20
测试端 CentOS 7.2x86_64 IP:172.16.251.138
构建LAMP:
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install httpd php php-mysql mysql-server
启动服务:
[[email protected] ~]# service httpd restart
[[email protected] ~]# service httpd mysql
[[email protected] ~]# echo "Backup Server 1" >> /var/www/html/index.html
安装NGINX:
[[email protected] ~]# iptables -F
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install
nginx-1.10.1-1.el7.ngx.x86_64.rpm
[[email protected] ~]# rpm -ql nginx
启动服务:
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# nginx
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
反向代理至后端LAMP:
[[email protected] ~]# vim
/etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://192.168.1.20; //转发至后端服务器
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
[[email protected] ~]#
nginx -s reload
客户端测试:
[[email protected] ~]# curl http://172.16.253.94
Backup Server 1
设定发往后端主机请求报文首部的值:
[[email protected] ~]#
vim /etc/nginx/conf.d/default.conf
server {
省略部分…
proxy_set_header X-IP $remote_addr;
省略部分…
}
[[email protected] ~]#
nginx -s reload
[[email protected] ~]# vim /etc/httpd/conf/httpd.conf
省略部分…
LogFormat "%{X-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
省略部分…
[[email protected] ~]# service httpd restart
测试后查看日志:
[[email protected]
~]# tail /var/log/httpd/access_log
192.168.1.10 - -
[02/Aug/2016:13:57:18 +0800] "GET / HTTP/1.0" 304 - "-"
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/45.0.2454.101 Safari/537.36"
192.168.1.10 - -
[02/Aug/2016:13:57:19 +0800] "GET / HTTP/1.0" 304 - "-"
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/45.0.2454.101 Safari/537.36"
172.16.251.138 - - [02/Aug/2016:17:01:15 +0800] "GET / HTTP/1.0" 200 16 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
172.16.251.138 - - [02/Aug/2016:17:01:15 +0800] "GET /favicon.ico HTTP/1.0" 404 287 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0"
代理PHP动态页面:
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
省略部分…
location ~* \.php$ {
proxy_pass http://192.168.1.20;
}
省略部分…
[[email protected] ~]# nginx -s reload
[[email protected] ~]# vim /var/www/html/test.php
<?php
phpinfo();
?>
客户端测试:
[[email protected] ~]# curl http://172.16.253.94/test.php
AB压力测试:
[[email protected] ~]# ab -n 5000 -c 100 http://172.16.253.94/test.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.16.253.94 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/1.10.1
Server Hostname: 172.16.253.94
Server Port: 80
Document Path: /test.php
Document Length: 49151 bytes
Concurrency Level: 100
Time taken for tests: 19.042 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 246565000 bytes
HTML transferred: 245755000 bytes
Requests per second: 262.57 [#/sec] (mean)
Time per request: 380.847 [ms] (mean)
Time per request: 3.808 [ms] (mean, across all concurrent requests)
Transfer rate: 12644.77 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 9.2 1 179
Processing: 47 375 155.1 341 3182
Waiting: 6 292 112.3 282 1709
Total: 54 378 155.8 342 3188
Percentage of the requests served within a certain time (ms)
50% 342
66% 372
75% 401
80% 421
90% 502
95% 612
98% 883
99% 990
100% 3188 (longest request)
定义缓存:
[[email protected] ~]# vim /etc/nginx/nginx.conf
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=pcache:10m max_size=1g;
include /etc/nginx/conf.d/*.conf;
}
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
server {
省略部分…
proxy_cache pcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
省略部分…
}
测试缓存效果:
[[email protected] ~]# ab -n 5000 -c 100 http://172.16.253.94/test.php
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 172.16.253.94 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests
Server Software: nginx/1.10.1
Server Hostname: 172.16.253.94
Server Port: 80
Document Path: /test.php
Document Length: 49151 bytes
Concurrency Level: 100
Time taken for tests: 3.304 seconds
Complete requests: 5000
Failed requests: 0
Write errors: 0
Total transferred: 246565000 bytes
HTML transferred: 245755000 bytes
Requests per second: 1513.35 [#/sec] (mean)
Time per request: 66.078 [ms] (mean)
Time per request: 0.661 [ms] (mean, across all concurrent requests)
Transfer rate: 72878.88 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 14 64.9 10 1014
Processing: 7 51 8.3 51 82
Waiting: 1 13 6.1 12 46
Total: 23 66 66.2 62 1083
Percentage of the requests served within a certain time (ms)
50% 62
66% 64
75% 66
80% 68
90% 71
95% 75
98% 82
99% 87
100% 1083 (longest request)
定义响应报文值:
[[email protected] ~]# vim /etc/nginx/conf.d/default.conf
server {
省略部分…
add_header X-Via $server_addr;
add_header X-Accel $server_addr;
省略部分…
}
[[email protected] ~]# nginx -s reload
客户端测试:
[[email protected] ~]# curl http://172.16.253.94/test.php