nginx程序location配置方法
作用: 匹配指定的uri信息,可以根据访问不同的uri信息,做出不同处理方案
举例:
访问/oldboy 目录时, 禁止10.0.0.0/24 网段访问
访问/oldgirl目录时, 禁止172.16.1.0/24 网段访问
访问站点下面其他目录, 没有任何限制
如何匹配uri信息:
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
Default: —
Context: server, location
= 精确匹配指定uri信息 == grep -o 只要匹配上的信息
~ 模糊匹配指定uri信息(区分信息大小写) == grep 匹配过滤信息
~* 模糊匹配指定uri信息(不区分大小写) == grep -i 匹配过滤信息
^~ 进行优先匹配/不识别扩展正则信息
location = / {
[ configuration A ]
}
location / { --- 进行默认匹配???
[ configuration B ]
}
location /documents/ { --- 根据资源目录信息进行匹配
[ configuration C ]
}
location ^~ /images/ { --- 进行优先匹配 /images/目录
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
- 测验:
oldboy.jpg ---> /html/www/oldboy/ www.oldboy.com/meinv01.html 显示 oldboy.jpg 图片
oldgirl.jpg ---> /html/www/oldgirl/ www.oldboy.com/meinv02.html 显示 oldgirl.jpg 图片
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
}
location /meinv01.html {
return 301 http://www.oldboy.com/oldboy/oldboy.jpg;
}
location /meinv02.html {
return 301 http://www.oldboy.com/oldgirl/oldgirl.jpg;
}
}
总结:
01. 配置多个location时, 需要有一个默认的location
02. 访问的uri文件信息必须存在
03. 访问指定资源不存在,可以利用return功能进行跳转访问
nginx程序rewrite跳转功能
用户浏览器 输入域名地址信息A -- web服务器 -- 自动处理 -- 访问域名地址信息B
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
regex: 正则匹配的信息(url/uri)
replacement: 替换成什么信息/跳转成什么地址信息
[flag]: 指定进行跳转的方式
- 方式一: last 用户访问网站,进行跳转之后,会重启再次发起访问 不会改变url/uri信息
-
方式二: break 用户访问网站,进行跳转之后,会直接访问跳转之后的资源信息 不会改变url/uri信息
nginx中last和break跳转原理.png
实践说明:
server {
listen 80;
server_name www.oldboy.com;
root /html/www;
# www.oldboy.com/break/ -- 跳转(break) -- www.oldboy.com/test/
location ~ ^/break/ {
rewrite ^/break/ /test/ break;
}
# www.oldboy.com/last/ -- 跳转(last) -- www.oldboy.com/test/
location ~ ^/last/ {
rewrite ^/last/ /test/ last;
}
# www.oldboy.com/test/ --- 页面显示 ok
location /test/ {
default_type application/json;
return 200 'ok';
}
}
- redirect (临时跳转) 不会让浏览器记录跳转信息(url信息跳转)
- permanent(永久跳转) 会让浏览器记录跳转信息(url信息跳转)
例子:
www.jd.com/shouji/index.html -- www.jd.com/shouji01/index.html
www.jd.com/shouji/index.html -- www.jd.com/shouji02/index.html
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
# www.oldboy.com/break/ -- 跳转(break) -- www.oldboy.com/test/
location ~ ^/break/ {
root /html/www;
index index.html;
rewrite ^/break/ /test/ redirect;
}
# www.oldboy.com/last/ -- 跳转(last) -- www.oldboy.com/test/
location ~ ^/last/ {
root /html/www;
index index.html;
rewrite ^/last/ /test/ permanent;
}
# www.oldboy.com/test/ --- 页面显示 ok
location /test/ {
root /html/www;
index index.html;
default_type application/json;
return 200 'ok';
}
}
uri地址跳转练习
- 例1: 用户访问www.oldboy.com/abc/1.html 实际上真实访问是/ccc/bbb/2.html
http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html A -> B
http://www.oldboy.com/abc/1.html ==> http://www.oldboy.com/ccc/bbb/2.html A -> B
第一个历程: 创建站点目录环境信息
跳转前环境: mkdir /html/www/abc -p; echo oldboy >/html/www/abc/1.html
跳转后环境: mkdir /html/www/ccc/bbb -p; echo oldboy >/html/www/ccc/bbb/2.html
第二个历程: 编写配置文件
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
location /abc/ {
root /html/www;
index index.html;
rewrite (.*) /ccc/bbb/2.html redirect;
}
}
- 例2:用户访问www.oldboy.com/2014/ccc/bbb/2.html 实际上真实访问是/2018/ccc/bbb/2.html AB --> CB A(.*) -- C(\1)
http://www.oldboy.com/2014/ccc/bbb/2.html ==> http://www.oldboy.com/2018/ccc/bbb/2.html
第一个历程: 创建站点目录环境信息
跳转后环境: mkdir /html/www/2018/ccc/bbb/ -p; echo 2018 >/html/www/2018/ccc/bbb/2.html
第二个历程: 编写配置文件信息
[root@web02 conf.d]# cat www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www;
index index.html;
}
location /2014/ {
root /html/www;
index index.html;
rewrite ^/2014/(.*) /2018/$1 redirect;
}
}
