生命之中的最大错误在于:终日担心犯错误。— 爱尔伯特·哈伯德 (Elbert Hubbard 1856-1915),《笔记》
nginx的location值里没有查询字符,所以在匹配地址时用不到查询字符
另外,rewrite时,默认会把查询字符带上的,加上问号即可解决。
假设要把 /question.php?qid=123 的地址跳转到 /question/123.html
我们会发现地址变成 /question/123.html?qid=123
你要的参数都可以找到 $arg_qid ( $arg_变量名称 )
nginx里面,在后面加上问号就可以不带上查询字符了
对值有要求的,可以匹配 $args ,比如(复制别人的)
另外,rewrite时,默认会把查询字符带上的,加上问号即可解决。
假设要把 /question.php?qid=123 的地址跳转到 /question/123.html
location /question.php {
rewrite ^ /question/$arg_qid.html redirect;
}
rewrite ^ /question/$arg_qid.html redirect;
}
我们会发现地址变成 /question/123.html?qid=123
你要的参数都可以找到 $arg_qid ( $arg_变量名称 )
nginx里面,在后面加上问号就可以不带上查询字符了
location /question.php {
rewrite ^ /question/$arg_qid.html? redirect;
}
rewrite ^ /question/$arg_qid.html? redirect;
}
对值有要求的,可以匹配 $args ,比如(复制别人的)
location / {
if ($args ~* "/?param1=val1¶m2=¶m3=[0-9]+¶m4=.+¶m5=[0-9]+") {
rewrite ^ http://www.example.com/newparam/$arg_param3/$arg_param4? last;
}
}
if ($args ~* "/?param1=val1¶m2=¶m3=[0-9]+¶m4=.+¶m5=[0-9]+") {
rewrite ^ http://www.example.com/newparam/$arg_param3/$arg_param4? last;
}
}
今天测试nginx的按时间缓存,发现两个问题:
1 proxy_cache_purge的1.0版本安装到0.8.54版本会出错,请安装1.3版本,否则清除缓存时curl会收到“curl: (52) Empty reply from server”,没法清除缓存
2 proxy_store和它是冲突的,proxy_store off才行,如果http段全局有on,那就在这里需要声明off
安装proxy_cache_purge你可以访问这里查看到官方提供的第三方插件http://wiki.nginx.org/3rdPartyModules
访问 http://labs.frickle.com/nginx_ngx_cache_purge/查看具体配置方式
关于安装的文章太多,不重复。
缓存的特点:
md5值分段截取为文件夹
1,2层级的目录是md5值的后面1个字符和2个字符作为文件夹的
有记KEY值和相应的头信息
清除缓存的响应:
1 proxy_cache_purge的1.0版本安装到0.8.54版本会出错,请安装1.3版本,否则清除缓存时curl会收到“curl: (52) Empty reply from server”,没法清除缓存
2 proxy_store和它是冲突的,proxy_store off才行,如果http段全局有on,那就在这里需要声明off
安装proxy_cache_purge你可以访问这里查看到官方提供的第三方插件http://wiki.nginx.org/3rdPartyModules
访问 http://labs.frickle.com/nginx_ngx_cache_purge/查看具体配置方式
引用
#### proxy_cache_path 指令指定缓存的路径和一些其他参数,缓存的数据存储在文件中。缓存的文件名和key为代理URL的MD5 码。levels参数指定缓存的子目录数,所有活动的key和元数据存储在共享的内存区域中,这个区域用keys_zone参数指定,keys_zone指定缓存的名字和共享内存大小,如果在inactive参数指定的时间内缓存的数据没有被请求则被删除,默认inactive为10分钟。cache manager进程控制磁盘的缓存大小,在max_size参数中定义,超过其大小后最少使用数据将被删除。
proxy_cache_path /www/ng_cache levels=1:2 keys_zone=tmp_cache:1000m inactive=1d max_size=10000m;
proxy_cache_path /www/ng_cache levels=1:2 keys_zone=tmp_cache:1000m inactive=1d max_size=10000m;
关于安装的文章太多,不重复。
缓存的特点:
md5值分段截取为文件夹
1,2层级的目录是md5值的后面1个字符和2个字符作为文件夹的
有记KEY值和相应的头信息
[root@aslibra nginx]# head d/8a/353329a20615078e7391e2c4d091e8ad
?;AN????O9AN??
?=???
KEY: www.aslibra.com/test/beijing/
HTTP/1.1 200 OK
Server: nginx/0.8.15
Date: Tue, 09 Aug 2011 13:42:40 GMT
Content-Type: text/html; charset=utf-8
Connection: close
X-Powered-By: PHP/5.2.10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">....
?;AN????O9AN??
?=???
KEY: www.aslibra.com/test/beijing/
HTTP/1.1 200 OK
Server: nginx/0.8.15
Date: Tue, 09 Aug 2011 13:42:40 GMT
Content-Type: text/html; charset=utf-8
Connection: close
X-Powered-By: PHP/5.2.10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">....
清除缓存的响应:
[root@aslibra nginx-0.8.54]# curl -H "host:www.aslibra.com" 127.0.0.1/purge/test/
<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : www.aslibra.com/test/
<br>Path: /Data/cache/nginx/d/8a/353329a20615078e7391e2c4d091e8ad
</center>
<hr><center>nginx/0.8.54</center>
</body>
</html>
[root@aslibra nginx-0.8.54]# curl -H "host:www.aslibra.com" 127.0.0.1/purge/test/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/0.8.54</center>
</body>
</html>
<html>
<head><title>Successful purge</title></head>
<body bgcolor="white">
<center><h1>Successful purge</h1>
<br>Key : www.aslibra.com/test/
<br>Path: /Data/cache/nginx/d/8a/353329a20615078e7391e2c4d091e8ad
</center>
<hr><center>nginx/0.8.54</center>
</body>
</html>
[root@aslibra nginx-0.8.54]# curl -H "host:www.aslibra.com" 127.0.0.1/purge/test/
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/0.8.54</center>
</body>
</html>
最近上传图片和发表内容比较多的文章都会出现500的错误提示。
php程序在第一句exit也出错,看来问题不是出现在php,而是nginx
看看nginx的错误日志
这个是出错的地方,显然是读写缓存的问题,大的请求需要临时目录的读写。
权限修改了也没有用,很奇怪,加了如下几个定义就好了,可以参考:
php程序在第一句exit也出错,看来问题不是出现在php,而是nginx
看看nginx的错误日志
2011/02/10 21:14:00 [crit] 9504#0: *204 open() "/Data/apps/nginx/fastcgi_temp/6/02/0000000026" failed (13: Permission denied) while reading upstream, client: 67.195.115.28, server: aslibra.com, request: "GET /blog/ HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:14:27 [crit] 9504#0: *216 open() "/Data/apps/nginx/fastcgi_temp/7/02/0000000027" failed (13: Permission denied) while reading upstream, client: 202.160.179.48, server: aslibra.com, request: "GET /blog/go.php/page/1/199/ HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:22:44 [crit] 9504#0: *408 open() "/Data/apps/nginx/fastcgi_temp/8/02/0000000028" failed (13: Permission denied) while reading upstream, client: 67.195.115.28, server: aslibra.com, request: "GET /blog/read.php/1114.htm HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:25:06 [crit] 9504#0: *477 open() "/Data/apps/nginx/fastcgi_temp/9/02/0000000029" failed (13: Permission denied) while reading upstream, client: 218.213.130.180, server: aslibra.com, request: "GET /blog/go.php/page/1/2/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:14:27 [crit] 9504#0: *216 open() "/Data/apps/nginx/fastcgi_temp/7/02/0000000027" failed (13: Permission denied) while reading upstream, client: 202.160.179.48, server: aslibra.com, request: "GET /blog/go.php/page/1/199/ HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:22:44 [crit] 9504#0: *408 open() "/Data/apps/nginx/fastcgi_temp/8/02/0000000028" failed (13: Permission denied) while reading upstream, client: 67.195.115.28, server: aslibra.com, request: "GET /blog/read.php/1114.htm HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
2011/02/10 21:25:06 [crit] 9504#0: *477 open() "/Data/apps/nginx/fastcgi_temp/9/02/0000000029" failed (13: Permission denied) while reading upstream, client: 218.213.130.180, server: aslibra.com, request: "GET /blog/go.php/page/1/2/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.aslibra.com"
这个是出错的地方,显然是读写缓存的问题,大的请求需要临时目录的读写。
权限修改了也没有用,很奇怪,加了如下几个定义就好了,可以参考:
相信有些服务器还是无可奈何的使用windows作为服务器,比如需要运行asp
可是要灵活rewrite和防盗链之类的,IIS可就麻烦了,一个不愿多碰的玩意
既然需要用,那就可以使用一个灵活的web服务在前面,后面放IIS,让它好好的退居二线。
一线位置的可选软件有:Nginx和apache
两者都可以提供足够的灵活性,如果还需要运行php,那不妨安装一些套件,比如:
我习惯用后者,用起来也还挺方便。
相对而言,apache的一些代理功能并非很灵活,用起来就有点复杂了。比如用正则匹配的方式条件性的进行代理,这个需要2.2.5版本以上才支持。nginx还是很不错的一个选择,代理功能非常强大,配置简洁,前端服务器的不二选择,各个大型网站都开始启用nginx,使用量风风火火的往上爬。
可是要灵活rewrite和防盗链之类的,IIS可就麻烦了,一个不愿多碰的玩意
既然需要用,那就可以使用一个灵活的web服务在前面,后面放IIS,让它好好的退居二线。
一线位置的可选软件有:Nginx和apache
两者都可以提供足够的灵活性,如果还需要运行php,那不妨安装一些套件,比如:
引用
XAMPP - XAMPP是一款具有中文说明的功能全面的集成环境,XAMPP并不仅仅针对Windows,而是一个适用于Linux、Windows、Mac OS X 和Solaris 的易于安装的Apache 发行版。软件包中包含Apache 服务器、MySQL、SQLite、PHP、Perl、FileZilla FTP Server、Tomcat等等。默认安装开放了所有功能,安全性有问题,需要进行额外的安全设定。
WampServer - WampServe集成了Apache、MySQL、PHP、phpmyadmin,支持Apache的mod_rewrite,PHP扩展、Apache模块只需要在菜单“开启/关闭”上点点就搞定,省去了修改配置文件的麻烦。
AppServ - 集成了Apache、PHP、MySQL、phpMyAdmin,较为轻量。
WampServer - WampServe集成了Apache、MySQL、PHP、phpmyadmin,支持Apache的mod_rewrite,PHP扩展、Apache模块只需要在菜单“开启/关闭”上点点就搞定,省去了修改配置文件的麻烦。
AppServ - 集成了Apache、PHP、MySQL、phpMyAdmin,较为轻量。
我习惯用后者,用起来也还挺方便。
相对而言,apache的一些代理功能并非很灵活,用起来就有点复杂了。比如用正则匹配的方式条件性的进行代理,这个需要2.2.5版本以上才支持。nginx还是很不错的一个选择,代理功能非常强大,配置简洁,前端服务器的不二选择,各个大型网站都开始启用nginx,使用量风风火火的往上爬。
1 lighttpd Statistics - 1.0
这个插件没有统计fastcgi,阿权计划出一个统计fastcgi的,先了解一下插件结构
2 memcached Cacti Template
这个插件需要python环境,阿权计划出一个纯php就能够运行的memcache版本,给类似我这样的不太懂python的人使用。
//UPDATE@0605 已经发布 cacti-memcached-template
3 scripts and templates for nginx
Provide graphing nginx clients statistics (active, reading, writing, waiting) and nginx socket statistics (accepts, handled, requests). It's a formal devision used only for graphs usability.
引用
These templates are based on the work of Rolf Poser (ApacheStats) [2],
using the version ApacheStats 0.6 (PHP Script Server Version) by
mahuani [3].
As lighttpd does not provide all informations in "?auto" mode of
the server status page and the variables are not named the same way,
I reimplemented the script server script and copied the existing
templates.
using the version ApacheStats 0.6 (PHP Script Server Version) by
mahuani [3].
As lighttpd does not provide all informations in "?auto" mode of
the server status page and the variables are not named the same way,
I reimplemented the script server script and copied the existing
templates.
这个插件没有统计fastcgi,阿权计划出一个统计fastcgi的,先了解一下插件结构
2 memcached Cacti Template
引用
This template provides a host template and associated graphs for graphing the output of the memcached stats command on individual memcached installations.
Graphs are provided for Bytes Used with total capacity, Cache Hits and Misses per second, Current Connections, Items Cached, Inbound and Outbound Network Traffic (bits per second), and Requests per Second for both the get and set commands.
Graphs are provided for Bytes Used with total capacity, Cache Hits and Misses per second, Current Connections, Items Cached, Inbound and Outbound Network Traffic (bits per second), and Requests per Second for both the get and set commands.
这个插件需要python环境,阿权计划出一个纯php就能够运行的memcache版本,给类似我这样的不太懂python的人使用。
//UPDATE@0605 已经发布 cacti-memcached-template
3 scripts and templates for nginx
Provide graphing nginx clients statistics (active, reading, writing, waiting) and nginx socket statistics (accepts, handled, requests). It's a formal devision used only for graphs usability.
参考《使用163开源镜像升级你的Centos 》
如果设置源服务器为163的,你有很多服务器需要处理,那建立一个本地缓存服务器是一个很好的选择,更新一次后,那之后的更新速度可就快的要命啊。。
配置nginx为类似如下样子,自行修改自己的需求:
yum的配置可以如下:
就这样OK了!
如果设置源服务器为163的,你有很多服务器需要处理,那建立一个本地缓存服务器是一个很好的选择,更新一次后,那之后的更新速度可就快的要命啊。。
配置nginx为类似如下样子,自行修改自己的需求:
upstream backend {
server mirrors.163.com:80 ;
}
server {
#server_name www.aslibra.com;
server_name mirrors.aslibra.com;
set $index 'index.htm';
set $store_file $request_filename;
#root /Data/www.aslibra.com;
root /Data/mirrors/mirrors.163.com;
if ($uri ~ /$ ){
set $store_file $request_filename$index;
}
location /centos/ {
index index.htm;
proxy_store on;
proxy_temp_path /Data/mirrors/tmp;
proxy_set_header Host mirrors.163.com;
proxy_store_access user:rw group:rw all:rw;
if ( !-e $store_file ) {
proxy_pass http://backend;
}
}
}
server mirrors.163.com:80 ;
}
server {
#server_name www.aslibra.com;
server_name mirrors.aslibra.com;
set $index 'index.htm';
set $store_file $request_filename;
#root /Data/www.aslibra.com;
root /Data/mirrors/mirrors.163.com;
if ($uri ~ /$ ){
set $store_file $request_filename$index;
}
location /centos/ {
index index.htm;
proxy_store on;
proxy_temp_path /Data/mirrors/tmp;
proxy_set_header Host mirrors.163.com;
proxy_store_access user:rw group:rw all:rw;
if ( !-e $store_file ) {
proxy_pass http://backend;
}
}
}
yum的配置可以如下:
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aslibra.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.aslibra.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[addons]
name=CentOS-$releasever - Addons
baseurl=http://mirrors.aslibra.com/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.aslibra.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.aslibra.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
name=CentOS-$releasever - Base
baseurl=http://mirrors.aslibra.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.aslibra.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[addons]
name=CentOS-$releasever - Addons
baseurl=http://mirrors.aslibra.com/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.aslibra.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.aslibra.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
就这样OK了!
以下几个是解决安装过程碰到的pcre和md5的问题
其中pcre-devel是解决pcre的,剩余两个解决md5的,很简便。
参考阅读:the HTTP rewrite module requires the PCRE library
yum -y install pcre-devel openssl openssl-devel
其中pcre-devel是解决pcre的,剩余两个解决md5的,很简便。
参考阅读:the HTTP rewrite module requires the PCRE library





