實驗環境:
NGINX CentOS 7.2x86_64 IP:172.16.253.94 192.168.1.10
LAMP CentOS 6.7x86_64 IP:192.168.1.20
測試端 CentOS 7.2x86_64 IP:172.16.251.138
構建LAMP:
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd php php-mysql mysql-server
啓動服務:
[root@localhost ~]# service httpd restart
[root@localhost ~]# service httpd mysql
[root@localhost ~]# echo "Backup Server 1" >> /var/www/html/index.html
安裝NGINX:
[root@pxe94 ~]# iptables -F
[root@pxe94 ~]# setenforce 0
[root@pxe94 ~]# yum -y install nginx-1.10.1-1.el7.ngx.x86_64.rpm
[root@pxe94 ~]# rpm -ql nginx
啓動服務:
[root@pxe94 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@pxe94 ~]# nginx
[root@pxe94 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
反向代理至後端LAMP:
[root@pxe94 ~]# 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;
}
[root@pxe94 ~]# nginx -s reload
客戶端測試:
[root@station138 ~]# curl http://172.16.253.94
Backup Server 1
設定發往後端主機請求報文首部的值:
[root@pxe94 ~]# vim /etc/nginx/conf.d/default.conf
server {
省略部分…
proxy_set_header X-IP $remote_addr;
省略部分…
}
[root@pxe94 ~]# nginx -s reload
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
省略部分…
LogFormat "%{X-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
省略部分…
[root@localhost ~]# service httpd restart
測試後查看日誌:
[root@localhost
~]# 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動態頁面:
[root@pxe94 ~]# vim /etc/nginx/conf.d/default.conf
省略部分…
location ~* \.php$ {
proxy_pass http://192.168.1.20;
}
省略部分…
[root@pxe94 ~]# nginx -s reload
[root@localhost ~]# vim /var/www/html/test.php
<?php
phpinfo();
?>
客戶端測試:
[root@station138 ~]# curl http://172.16.253.94/test.php
AB壓力測試:
[root@station138 ~]# 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)
定義緩存:
[root@pxe94 ~]# 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;
}
[root@pxe94 ~]# 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;
省略部分…
}
測試緩存效果:
[root@station138 ~]# 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)
定義響應報文值:
[root@pxe94 ~]# vim /etc/nginx/conf.d/default.conf
server {
省略部分…
add_header X-Via $server_addr;
add_header X-Accel $server_addr;
省略部分…
}
[root@pxe94 ~]# nginx -s reload
客戶端測試:
[root@station138 ~]# curl http://172.16.253.94/test.php