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"
这个是出错的地方,显然是读写缓存的问题,大的请求需要临时目录的读写。
权限修改了也没有用,很奇怪,加了如下几个定义就好了,可以参考:
参考《使用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