Location 基础知识:
1、概念:
- 我们可以通过配置Location指令块,来决定客户端发过来的请求URI如何处理。
2、 语法:
- Syntax: location [ = | ~ | ~* | ^~ ] uri { … }
- location @name { … }
- Default: —
- Context: server, location
- location 配置可以有两种配置方法,可以在server指令块和location指令块配置。
1、修饰符 + uri(资源路径)
2、@ + name
3、修饰符
- = :精确匹配(必须全部相等)
- ~ :大小写敏感(正则表达式)
- ~* :忽略大小写(正则表达式),这里要注意忽略大小写的意思是请求的字符大小写都可以, 但是不会进行大小转换,请求的大小写对应的文件必须存在。
- ^~ :只需匹配uri部分
- @ :内部服务跳转
Location 配置实例
1、精准匹配:
- =,精准匹配,一般是匹配某个具体文件。
server {
listen 80;
server_name www.ys.com;
location = /index.html {
}
}
匹配到http://www.ys.com/index.html
- 还有一种精确匹配 / 可以加快首页访问速度
server {
listen 80;
server_name www.ys.com;
location = /{
root html;
index index.html;
}
}
注意:如果想变更根目录,比如把根目录设置为/usr/local/nginx/html/ys,那么直接这 么写可能会出问题,假如原根目录(html)没有index.html文件,会报404。
server {
listen 80;
server_name www.ys.com;
location = / {
root /usr/local/nginx/html/ys;
index index.html;
}
}
如果写成这样的话,即使/usr/local/nginx/html/ys目录下有index.html,也会直接报错, 报找不到文件,可是里面明明有文件呀
- 解释:
当我们访问www.ys.com/时,确实匹配到了这个location, 但是这个时候请求的前缀会变成/index.html,所以已经不再匹配这个localtion规则。 因为找不到其他匹配规则,所以默认会去匹配根目录下(html)的文件,但是这时根目录下的index.html不存在, 所以报错404。
- 同样的,如果/usr/local/nginx/html/ys目录里面有其他的文件,我们通过这个localtion规则也是无法访问的, 因为它只匹配/,其他的url都不再是它匹配。
- 那么怎么解决这个问题呢?可以在加一个location或者在http指令块添加全局路径。
server {
listen 80;
server_name www.ys.com;
location = / {
root /usr/local/nginx/html/ys;
index index.html;
}
location / {
root /usr/local/nginx/html/ys;
index index.html;
}
通过加这个location,凡是没有匹配到的资源会到/目录下去找 ,根的目录重新定义了,所以可以实现这个需求。
- 方法二:
在http指令块添加:
root /usr/local/nginx/html/ys;
重启服务即可
2、大小写敏感匹配:
- ~ 大小写敏感(正则表达式)。
location ~ /YS/ {
[ configuration ]
}
#请求示例
#http://www.ys.com/YS/ [成功]
#http://www.ys.com/ys/ [失败]
3、大小写不敏感匹配:
~* 大小写忽略(正则表达式)。
location ~* /ys.html {
[ configuration ]
}
# 则会忽略 uri 部分的大小写
#http://www.ys.com/ys.html [成功] 可以成功匹配,但是目录中要ys.html文 件
#http://www.ys.com/YS.html [成功] 可以成功匹配,但是目录中要YS.html文 件
4、指定后缀匹配
- 匹配以‘点’gif、jpg、jpeg结尾的文件
location ~* \.(gif|jpg|jpeg)$ {
[ configuration ]
}
#http://www.ys.com/img/ys.jpg [成功]
5、忽略正则匹配:
- ^~ 只匹配以 uri 开头,匹配成功以后,会停止搜索后面的正则表达式匹配。
location ^~ /img/ {
[ configuration ]
}
#以 /img/ 开头的请求,都会匹配上
#http://www.ys.com/img/ys.jpg [成功]
#http://www.ys.com/img/ys.png [成功]
- 如果配置了5,那么所有请求 /img/ 下的图片会被上面的处理,因为 ^~ 指令匹配到了,则不检查正则 表达式。对比这两个location,可以设置不同目录,相同文件进行实验。
location优先级:
- 完整范例:
location / {
echo "this is $request_uri";
}
location ~* \.(jpg|png) {
echo "this is ~* \.(jpg|png)";
}
location ~ \.(jpg|png) {
echo "this is ~ \.(jpg|png)";
}
location ^~ /img/ys.jpg {
echo "this is ^~ /img/ys.jpg";
}
location = /img/ys.jpg {
echo "this is = /img/ys.jpg";
}
- 如果客户端的请求是:
curl 192.168.2.10/img/ys.jpg
- 那么按照匹配规则顺序应该是这样的:
第一步: 取出uri:/img/ys.jpg
第二步: 去匹配localtion规则,查找有没有 = /img/ys.jpg 的规则,有则停止匹配。
[root@localhost ~]# curl 192.168.2.10/img/jfedu.jpg
this is = /img/jfedu.jpg
第三步: 将location = /img/ys.jpg 规则注释,继续查找有没有 ^~ /img/ 的规则,
[root@www ~]# curl 192.168.2.10/img/ys.jpg
this is ^~ /img/ys.jpg
第四步: 将 location ^~ /img/注释,这是它会去查找有没有正则匹配规则。
location / {
echo "this is $request_uri";
}
location ~* \.(jpg|png)$ {
echo "this is ~* \.(jpg|png)";
}
location ~ \.(jpg|png)$ {
echo "this is ~ \.(jpg|png)";
}
#location ^~ /img/ys.jpg {
# echo "this is ^~ /img/ys.jpg";
#}
#location = /img/ys.jpg {
# echo "this is = /img/ys.jpg";
#}
其中,第二个和的第三个规则都是正则,这时会按照至上而下的顺序匹配。
[root@localhost ~]# curl 192.168.2.10/img/ys.jpg
this is ~* \.(jpg|png)
第五步: 其他的都注释后,因为优先匹配规则都没有找到,最后匹配到 / 规则。
[root@localhost ~]# curl 192.168.75.131/img/ys.jpg
this is /img/ys.jpg
rewrite规则:
- Nginx的rewrite功能需要pcre软件的支持,即通过perl兼容正则表达式语句进行规则匹配的。 默认参数编译nginx就会支持rewrite的模块,但是也必须要pcre的支持。
- rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结 尾是flag标记
2、rewrite语法:
rewrite <regex> <replacement> [flag];
正则 替代内容 flag标记
正则:perl兼容正则表达式语句进行规则匹配
替代内容:将正则匹配的内容替换成replacement
flag标记:rewrite支持的flag标记
flag标记说明:
last #本条规则匹配完成终止当前location的规则,继续向下匹配新的location URI规则
break #本条规则匹配完成即终止,不再匹配后面的任何规则
redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent #返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
rewrite实例:
1、实现域名跳转:
1.1、 修改nginx配置文件
[root@v1 ~]# cd /usr/local/nginx/
[root@v1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name sxy.com;
rewrite ^/(.*) http://www.sxy.com/$1 permanent;
}
server {
listen 80;
server_name www.sxy.com;
location / {
root /data/www/;
index index.html index.htm;
}
}
或者:
server {
listen 80;
server_name www.sxy.com sxy.com;
if ( $host != 'www.sxy.com' ) {
rewrite ^/(.*) http://www.sxy.com/$1 permanent;
}
location / {
root /data/www/;
index index.html index.htm;
}
}
注意:以上的两种配置文件修改方法选择其一即可。
1.2、创建网页文件
[root@v1 nginx]# mkdir -p /data/www/
[root@v1 nginx]# echo "我是域名跳转" > /data/www/index.html
1.3、重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
1.4、验证
1.4.1、修改Windows系统的hosts文件
- 在hosts文件后面添加这一行:
192.168.2.10 www.sxy.com sxy.com
1.4.1、打开谷歌浏览器验证
2、实现浏览器的语言跳转:
- 根据浏览器的语言跳转到指定URL:
2.1、修改配置文件
[root@v1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name www.sxy.com sxy.com;
location / {
root /data/www/;
index index.html;
}
if ( $http_accept_language ~ "zh" ) {
rewrite ^/$ /zh;
}
if ( $http_accept_language ~ "en" ) {
rewrite ^/$ /en;
}
}
2.2、创建网页文件:
[root@v1 nginx]# mkdir -p /data/www/zh
[root@v1 nginx]# mkdir -p /data/www/en
[root@v1 nginx]# echo "this is 中文" > /data/www/zh/index.html
[root@v1 nginx]# echo "this is english" > /data/www/en/index.html
2.3、重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
2.4、验证:
- 打开谷歌浏览器把语言修为中文验证如下:
- 修改谷歌浏览器语言为英文验证如下:
3、实现不同终端跳转
3.1、修改配置文件
[root@v1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name www.sxy.com sxy.com;
location / {
root /data/web/;
index index.html;
}
if ( $http_user_agent ~* "iphone|android" ) {
rewrite ^/(.*)$ http://m.sxy.com/$1;
}
}
server {
listen 80;
server_name m.sxy.com;
location / {
root /data/m/;
index index.html;
}
}
3.2、创建网页文件
[root@v1 nginx]# mkdir -p /data/web/
[root@v1 nginx]# mkdir -p /data/m
[root@v1 nginx]# echo "我是pc终端" > /data/web/index.html
[root@v1 nginx]# echo "我是iphone|android" > /data/m/index.html
3.3、重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
3.4、修改Windows系统hosts文件
- 添加如下:
192.168.2.10 www.sxy.com sxy.com m.sxy.com
3.5、验证pc端
3.6、验证手机终端
4、实现错误页面返回首页:
4.1、修改配置文件:
[root@v1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name www.sxy.com sxy.com;
access_log logs/sxy.access.log main;
error_page 404 =200 /index.html;
location / {
root html;
index index.html index.htm;
}
4.2、创建网页文件
[root@v1 nginx]# echo "我是错误页面返回首页" > html/index.html
4.3、重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload
4.4、验证:
5、实现错误页面返回腾讯公益页面:
5.1、修改配置文件
[root@v1 nginx]# vim conf/nginx.conf
server {
listen 80;
server_name www.sxy.com sxy.com;
access_log logs/sxy.access.log main;
error_page 404 =200 /404.html;
location / {
root html;
index index.html index.htm;
}
5.2、创建网页文件
[root@v1 nginx]# vim html/404.html
<!DOCTYPE HTML> <html>
<head>
<meta charset="UTF-8" />
<title>公益404</title>
</head>
<body>
<!--<script type="text/javascript" src="http://www.qq.com/404/search_children.js"></script>-->
<script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/hybrid/app/404/search_children.js"
charset="utf-8"></script>
</body>
</html
5.3、重新加载配置文件
[root@v1 nginx]# /usr/local/nginx/sbin/nginx -s reload