首页 > 代码库 > Nginx反向代理、缓存、 负载均衡、upstream以及fastcgi模块应用
Nginx反向代理、缓存、 负载均衡、upstream以及fastcgi模块应用
Nginx反向代理,缓存, 负载均衡, upstream及fastcgi模块应用
Nginx版本为nginx-1.6.2-1.el6.ngx.x86_64.rpm可以去官网下载: http://nginx.org/packages/centos/6/x86_64/RPMS/
[root@node1 ~]# rpm -ivhnginx-1.6.2-1.el6.ngx.x86_64.rpm
[root@node2 ~]# vim/var/www/html/index.html
[root@node2 ~]# service httpd start
[root@node1 ~]# curl http://172.16.18.20
[root@node1 ~]# cd /etc/nginx/
[root@node1 nginx]# cd conf.d/
[root@node1 conf.d]# cp default.confdefault.conf.bak
[root@node1 conf.d]# vim default.conf
location / {
#root /usr/share/nginx/html;
proxy_pass http://172.16.18.20/; (添加一个反向代理至172.16.18.20)
index index.html index.htm;
}
[root@node1 conf.d]# service nginx start
[root@node1 conf.d]# service nginxconfigtest (检查语法错误)
[root@node1 conf.d]# service nginx reload (重新加载配置文件)
只代理部分页面:
[root@node2 ~]# cd /var/www/html/
[root@node2 html]# mkdir bbs
[root@node2 html]# cd bbs/
[root@node2 bbs]# vim index.html
[root@node1 conf.d]# vim default.conf
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /bbs/ { (这里bbs可以用其他字符替换,例如替换成abc,我们就访问http://172.16.18.20/abc)
proxy_pass http://172.16.18.20/bbs/;
}
[root@node1 conf.d]# service nginxconfigtest
[root@node1 conf.d]# service nginx reload (再次访问页面,)
设置所有以XXX结尾的文件都
[root@node1 conf.d]# vim default.conf
location ~*\.(jpg|png|gif)$ {
proxy_pass http://172.16.18.20;
}
[root@node1 conf.d]# service nginx reload
[root@node2 html]# pwd
/var/www/html
[root@node2 html]# ls (html目录下放一张图片)
2.jpg bbs index.html
使被代理的服务器显示真实访问IP而不是显示代理服务器的IP;
[root@node1 conf.d]# vim default.conf
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /form/ {
proxy_pass http://172.16.18.20//;
proxy_set_header Host $host;
proxy_set_header X-Real-IP$remote_addr;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://172.16.18.20;
proxy_set_header X-Real-IP $remote_addr;
}
[root@node1 conf.d]# service nginxconfigtest
[root@node1 conf.d]# service nginx reload
[root@node2 html]# tail /var/log/httpd/access_log
[root@node2 html]# vim/etc/httpd/conf/httpd.conf (修改日志类型)
LogFormat "%{X-Real-IP}i%l %u %t \"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined
[root@node2 html]# service httpd restart
[root@node2 html]# service httpd reload
[root@node2 html]# tail/var/log/httpd/access_log (查看访问日志可以看到真实的访问IP)
172.16.250.18 - - [07/Jan/2015:03:13:26 +0800] "GET // HTTP/1.0" 200 12"-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
代理缓存功能实现方法!
[root@node1 nginx]# vim nginx.conf
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;
proxy_cache_path /cache/nginx/ levels=1:1keys_zone=mycache:32m;
注释:/cache/nginx/ :路径,注意属组和属主都必须是nginx (必须设置)
levels=1:1 :允许使用的一级目录的个数和二级目录的个数(可以定义更多),有默认值,可以不设置;
keys_zone=mycache32m;:内存空间的名字(不得相同)及大小(用来存键)(必须设置)
[root@node1 nginx]# mkdir -pv /cache/nginx
[root@node1 conf.d]# chown -R nginx:nginx/cache/nginx/
[root@node1 conf.d]# vim default.conf
location /form/ { (如果是在server下定义,那么对所有的页面都执行,如果是在location下定义那仅对指定页面缓存)
proxy_cache mycache; (表示使用mycache进行缓存)
proxy_cache_valid 200 1d; (出现200状态响应码时的缓存时间)
proxy_cache_valid 301 302 10m; (出现300状态响应码时的缓存时间)
proxy_cache_valid any 1m; (出现其他状态响应码时的缓存时间)
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; (哪种场景中使用腐败(过期)缓存)
proxy_pass http://172.16.18.20//;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
[root@node1 conf.d]# service nginxconfigtest
[root@node1 conf.d]# service nginx reload
[root@node1 conf.d]# cd /cache/nginx/ (现在目录内还没有内容,可以使用ls查看)
访问此页面http://172.16.18.10/form/,然后在查看是否生成缓存文件!
[root@node1 nginx]# ls
[root@node1 nginx]# tree (这里已经成功生成一级二级子目录和缓存文件)
.
4
6
6d07d149c540898146fb4263f67f9764
[root@node2 html]# vim index.html (尝试修改文件试试,随便添加一些内容)
再次访问,仍然是修改前的结果,这是缓存造成的效果;
[root@node1 nginx]# rm4/6/6d07d149c540898146fb4263f67f9764 (在代理服务器把缓存文件删除,再次访问)
再次访问又会生成新的缓存文件,如果是文件要经常修改的,可以吧缓存时间设置短一些!
实现负载均衡效果
[root@node3 ~]# vim /var/www/html/index.html
[root@node3 ~]# service httpd star
[root@node1 nginx]# cd /etc/nginx/
[root@node1 nginx]# vim nginx.conf
proxy_cache_path /cache/nginx/ levels=1:1 keys_zone=mycache:32m;
upstream upservers { 定义上游服务器,指分别对哪几台服务器实行负载均衡
server 172.16.18.30;
server 172.16.18.20;
}
[root@node1 nginx]# vim conf.d/default.conf
:.,$s@[:space:]*proxy_cache@#&@g (全局搜索把[:space:]*proxy_cache开头的行前面加个#)
location /form/ {
#proxy_cache mycache; (取消缓存功能,不然负载均衡看不到效果)
#proxy_cache_valid 200 1d;
#proxy_cache_valid 301 302 10m;
#proxy_cache_valid any 1m;
#proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_pass http://upservers/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location ~* \.(jpg|png|gif)$ {
proxy_pass http://upservers;
}
[root@node1 nginx]# service nginxconfigtest
[root@node1 nginx]# service nginx reload (再次访问http://172.16.18.10/form并刷新就可以看到负载均衡效果了)
定义负载均衡权重
[root@node1 nginx]# vim nginx.conf
upstream upservers {
server 172.16.18.30 weight=2; (修改权重等于2)
server 172.16.18.20; (不输入则默认是1)
}
[root@node1 nginx]# service nginx reload
再次访问http://172.16.18.10/form/ 检测结果!(记得多刷新几次才能看出来)
[root@node1 nginx]# vim nginx.conf
upstream upservers {
ip_hash; (根据IP进行调度,绑定ip,同一个IP访问时会一直调度到同一个服务器)
server 172.16.18.30 weight=2;
server 172.16.18.20;
}
[root@node1 nginx]# service nginx reload
再次访问http://172.16.18.10/form/ 检测结果!(记得多刷新几次才能看出来)
[root@node1 nginx]# vim nginx.conf
upstream upservers {
server172.16.18.30 max_fails=2 fail_timeout=1; (访问失败两次则剔除此服务器)
server172.16.18.20 max_fails=2 fail_timeout=1;
}
[root@node1 nginx]# service nginx reload
再次访问http://172.16.18.10/form/ 检测结果!(这是权重是一样的,会出现负载均衡效果)
[root@node3 ~]# service httpd stop (模拟其中一台服务器出现故障)
再次访问http://172.16.18.10/form/ 检测结果! (坏掉的服务器已经被剔除,刷新已经不会显示了)
[root@node3 ~]# service httpd start (服务器重新上线,代理服务器会自动把修好的服务器加进来,访问网页会再次出现node3)
[root@node1 nginx]# vim nginx.conf
upstream upservers {
server 172.16.18.30 max_fails=2 fail_timeout=1;
server 172.16.18.20 max_fails=2 fail_timeout=1backup; (把服务器标记为备用,在node3无法访问时,会把用户访问代理至此服务器)
}
[root@node1 nginx]# service nginx reload (此时访问就只有node3的页面了)
[root@node3 ~]# service httpd stop (尝试吧node3服务停止,这时会启用备用服务器,访问页面就是node2的页面了)
实现自定义响应首部
[root@node1 nginx]# vim conf.d/default.conf
server{
listen 80;
server_name localhost;
add_header X-Via $server_addr; (访问的web服务器)
[root@node1 nginx]#service nginx configtest
[root@node1nginx]# service nginx reload
(我使用Google浏览器,按F12会出现源码内容)
[root@node1 nginx]# vimconf.d/default.conf
server {
listen 80;
server_name localhost;
add_headerX-Via $server_addr;
add_header X-Cache $upstream_cache_status; (查看缓存状态,是否成功启用缓存)
location/form/ { (开启缓存功能)
proxy_cache mycache;
proxy_cache_valid 200 1d;
proxy_cache_valid 301 302 10m;
proxy_cache_valid any 1m;
proxy_cache_use_stale errortimeout http_500 http_502 http_503http_504;
[root@node1 nginx]# service nginxconfigtest
[root@node1 nginx]# service nginx reload
让linux支持Lump
nginx的upstream及fastcgi模块应用
[root@node1 nginx]# yum install php-fpm –y
[root@node1 nginx]# service php-fpm start
[root@node1 nginx]# ss -tnl
LISTEN 0 128 127.0.0.1:9000 (监听的端口)
[root@node1 nginx]# cd conf.d/
[root@node1 nginx]# vim conf.d/default.conf
location /{
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
location ~ \.php$ { (如果用户请求.php结尾)
root /usr/share/nginx/html; (网页文件路径)
fastcgi_pass 127.0.0.1:9000; (反向代理至什么位置)
fastcgi_index index.php; (主页)
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; (向后端传递的参数,SCRIPT_FILENAME:参数名,通过/scripts路径向后端传递)
include fastcgi_params; (扩展fastcgi_params)
}
[root@node1 conf.d]# service nginxconfigtest
[root@node1 conf.d]# service nginx reload
[root@node1 conf.d]# cd ..
[root@node1 nginx]# vim fastcgi_params (原文件有问题,这里可以全部替换)
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
[root@node1 nginx]# cd/usr/share/nginx/html/
[root@node1html]# mv index.html nginx.html (为了测试,把原来的页面换个名字,)
[root@node1 html]# vim index.php (重新建一个php结尾的页面显示php信息)
<?php
phpinfo()
?>
连接mysql
[root@node1 nginx]# yum install php-mysql
[root@node1 nginx]# service php-fpm restart
(再次访问http://172.16.18.10/页面已经可以看mysql)
[root@node1 nginx]# yuminstall mysql-server (安装mysql服务)
[root@node1 nginx]# service mysqld start
[root@node1 nginx]# ss –tnl (mysql监听于3306端口)
LISTEN 0 50 *:3306
[root@node1 nginx]# vim/usr/share/nginx/html/index.php
<?php
$conn =mysql_connect(‘127.0.0.1‘,‘root‘,‘‘); (连接数据库)
if ($conn)
echo succ; (成功输出succ)
else
echo fail; (失败输出fail)
mysql_close();
?>
设置动态页面缓存
[root@node1 nginx]# vim nginx.conf
proxy_cache_path /cache/nginx/ levels=1:1keys_zone=mycache:32m;
fastcgi_cache_path /cache/fastcgi/ levels=1:1keys_zone=fcgicache:10m inactive=3mmax_size=1g;
[root@node1 nginx]# vim conf.d/default.conf
location ~\.php$ {
fastcgi_cachefcgicache; (调用)
fastcgi_cache_valid200 10m;
fastcgi_cache_valid302 2m;
fastcgi_cache_valid any 1m;
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
[root@node1 nginx]# service nginx reload
[root@node1 nginx]# vim /usr/share/nginx/html/test.php
<?php
phpinfo();
?>
访问页面http://172.16.18.10/test.php
压力测试缓存是否有实际效果!
[root@node1 nginx]# vim conf.d/default.conf
location ~ \.php$ {
#fastcgi_cachefcgicache;
fastcgi_cache off; (暂时关闭缓存功能)
#fastcgi_cache_valid 200 10m;
fastcgi_cache_valid 302 2m;
fastcgi_cache_valid any 1m;
[root@node1 nginx]# service nginx reload
[root@node1 nginx]# rm /cache/fastcgi/* -rf (删除所有缓存文件)
[root@node1 nginx]# ab -n 2000 -c 100 http://172.16.18.10/test.php (进行ab测试,2000个请求,并发100)
Requests per second: 987.57 [#/sec] (mean) 每秒请求:987.57(# /秒)(平均)
Time perrequest: 101.258 [ms] (mean) 每个请求时间:101.258(ms)(平均)
Time per request: 1.013 [ms] (mean, acrossall concurrent requests) 每个请求时间:1.013(ms)(所有并发请求)
Transfer rate: 43837.03 [Kbytes/sec] received 传输速率:43837.03(kb /秒)
[root@node1 nginx]# vim conf.d/default.conf(再开启缓存功能测试)
location ~ \.php$ {
fastcgi_cachefcgicache;
#fastcgi_cache off; (再开启缓存功能测试)
fastcgi_cache_valid 200 10m;
fastcgi_cache_valid 302 2m;
fastcgi_cache_valid any 1m;
[root@node1 nginx]# service nginx reload
[root@node1 nginx]# ab -n 2000 -c 100 http://172.16.18.10/test.php (这次感觉访问速度快了很多)
Requests per second: 8540.88 [#/sec] (mean) 每秒请求: 8540.88(# /秒)(平均)
Time per request: 11.708 [ms] (mean) 每个请求时间: 11.708(ms)(平均)
Time per request: 0.117 [ms] (mean, across all concurrentrequests) 每个请求时间: 0.117 (ms)(意思是,所有并发请求)
Transfer rate: 379234.94 [Kbytes/sec] received 传输速率: 379234.94 (kb /秒)
Nginx反向代理、缓存、 负载均衡、upstream以及fastcgi模块应用