快照目录文件太多,准备安装一个方式分目录。但是又要能保证原来的访问方式不变化!使用rewrite和try_files成功实现。
目录结构:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
winse@Lenovo-PC /cygdrive/f/temp
$ ls -R
.:
1.jpg snapshot snapshot-1 snapshot-2 snapshot-3 snapshot-4
./snapshot:
0.html
./snapshot-1:
1.html
./snapshot-2:
2.html
./snapshot-3:
3.html
./snapshot-4:
4.html
Nginx配置尝试一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /snapshot {
root /home/hadoop/html-snapshot;
add_header content-type "text/html";
rewrite ^/snapshot/.*/(.*)$ /snapshot/$1 break ;
try_files $uri /snapshot-1/$uri /snapshot-3/$uri;
}
location ~ /snapshot-\d+ {
root /home/hadoop/html-snapshot;
rewrite ^/(.*)/.*/(.*)$ /$1/$2 break;
}
这种方式是不行的,try_files要求除最后一个配置外其他都是文件!
It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. [http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files ]
也就是说,中间配置路径,nginx只把他们当做本地的去看待!文件存在就返回结果,否则直接重定向到最后一个路径!!
Nginx配置尝试二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
location /snapshot {
root F:/temp;
add_header content-type "text/html";
rewrite ^/snapshot/.*/(.*)$ /snapshot/$1 break ;
try_files $uri @backup;
}
location ~ /snapshot-\d+ {
root F:/temp;
try_files $uri @backup;
}
location @backup {
# 这里的顺序不能颠倒,[.*]会匹配所有的!
rewrite ^/(.*)-3/(.*)$ /$1-4/$2 last;
rewrite ^/(.*)-2/(.*)$ /$1-3/$2 last;
rewrite ^/(.*)-1/(.*)$ /$1-2/$2 last;
rewrite ^/(.*)/(.*)$ /$1-1/$2 last;
}
这里使用循环的方式在backup的location中进行处理,一个个的循环查找。使用了正则表达式和一个统一rewrite的location。
Nginx配置尝试三:
上面发现,其实try_files都是去查找文件,其实目录结构和访问路径是匹配的,只是请求一开始就带snaphost,倒是每次都需要处理。如果请求过来的就没有带snaphost的话!
1
2
3
4
5
6
location / {
root F:/temp;
add_header content-type "text/html";
try_files /snapshot/$uri /snapshot-1/$uri /snapshot-2/$uri /snapshot-3/$uri /snapshot-4/$uri;
}
一个location配置就行了!
Nginx配置完善版:
转变思路后,最开始就把请求的前置snapshot去掉rewrite去掉就行了!
1
2
3
4
5
6
7
8
location /snapshot {
root F:/temp;
add_header content-type "text/html";
rewrite ^/snapshot/.*/(.*)$ /$1 break ;
try_files /snapshot/$uri /snapshot-1/$uri /snapshot-2/$uri /snapshot-3/$uri /snapshot-4/$uri;
}
nginx添加模块
当我们启用 –with-debug 选项重新构建好调试版的 Nginx 之后,还需要同时在配置文件中通过标准的 error_log 配置指令为错误日志使用 debug 日志级别(这同时也是最低的日志级别):
1
error_log logs/error.log debug;
添加echo模块:
下载zlib、pcre、echo:
1
2
3
4
5
6
7
8
9
tar zxvf zlib-1.2.8.tar.gz
mv zlib-1.2.8 src/zlib
tar zxvf pcre-8.36.tar.gz
mv pcre-8.36 src/pcre
./configure --prefix=/home/hadoop/nginx --add-module=/home/hadoop/echo-nginx-module-0.58 --with-pcre=src/pcre --with-zlib=src/zlib --with-debug
#[hadoop@cu2 nginx-1.7.10]$ ./configure --prefix=/home/hadoop/nginx --with-http_ssl_module --with-pcre=src/pcre/ --with-zlib=src/zlib/ --with-debug
make -j2
make install
编译成功后,就能在location里面直接echo,页面访问时就能看到echo内容了。
更新编译1.9
1
2
3
4
5
[root@cu1 ~]# yum install openssl openssl-devel -y
[root@cu1 ~]# yum install gcc gcc-c++ -y
[hadoop@cu1 nginx-1.9.12]$ ./configure --prefix=/home/hadoop/nginx --with-pcre=/home/hadoop/pcre-8.36 --with-http_ssl_module
[hadoop@cu1 nginx-1.9.12]$ make && make install
参考
–END