Nginx高级功能详解

一、重写功能

Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求。

官方文档:nginx.org/en/docs/htt…

用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为自动访问,另外还可以在一定程度上提高网站的安全性。

  1. if 指令

if指令可以配置在server或location块中
if指令用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置
Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断
使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false

用法:

点击查看代码
if (条件匹配) {   
 action
}

示例:
点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf  //编辑子配置文件

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

location /test {
     index index.html;            //定义网站的默认首页
     default_type text/html;      //指定解析当前内容的类型
   if  ( $scheme = https ) {      //添加if指令
   echo "if -----> $scheme";
   }
 }
}

[root@node1 ~]#  nginx -s reload  //重新加载

浏览器验证 https://192.168.204.10/test

  1. return 指令
  • return用于完成对请求的处理,并直接向客户端返回响应状态码
  • return可以在server、location块和 if 进行配置
  • 处于此指令后的所有配置都将不被执行

语法格式:

点击查看代码
Syntax:	return code [text];  //返回给客户端的状态码及响应报文的实体内容
           return code URL;    //返回给客户端的URL地址
           return URL; 
Default:	—
Context:	server, location, if

示例:
点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

  location /test {
     index index.html;
     default_type text/html;
     return 666 "hello";     //如果访问192.168.204.10/test 就返回666错误代码,页面显示hello
  }
}

[root@node1 ~]#  nginx -s reload     //重新加载        

[root@node2 ~]#  curl 192.168.204.10/test
hello
[root@node2 ~]#  curl 192.168.204.10/test -I
HTTP/1.1 666    //错误代码666
Server: nginx
..........

示例2:301

点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

  location /test {
     index index.html;
     default_type text/html;
     return 301 http://www.baidu.com/;     //如果访问192.168.204.10/test 就返回301并跳转到百度
  }
}

[root@node1 ~]#  nginx -s reload     //重新加载        

示例3:302

点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

  location /test {
     index index.html;
     default_type text/html;
     return 302 index.html;     //如果访问192.168.204.10/test 就返回302并显示/data/html/index.html内容
  }
}

[root@node1 ~]#  nginx -s reload     //重新加载        

状态码 301 和 302

状态码 含义 区别
301 永久重定向 服务器不需要每次向客户提供新的url,客户访问过后会记录在自己的缓存中,即使nginx服务器死机,客户在一定时间内也可以继续跳转
302 临时重定向 没有缓存,服务器断开无法重定向
  1. set 指令
  • set定义格式为set $key value
  • 指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key
  • value可以是text, variables和两者的组合
    语法格式:
点击查看代码
Syntax:	set $variable value;
Default:Context:	server, location, if

示例:
点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

  location /test {
     index index.html;
     default_type text/html;
set $name kgc;               //添加set指令
echo $name;
set $my_port $server_port;
echo $my_port;
}
}

[root@node1 ~]#  nginx -s reload   //重新加载

[root@node2 ~]#  curl 192.168.204.10/test   //验证
kgc
80

  1. break 指令

用于中断 当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的 ngx_http_rewrite_module 模块中指令就不再执行。

语法格式:

点击查看代码
Syntax:	break;
Default:Context:	server, location, if

示例:
点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

  location /test {
     index index.html;
     default_type text/html;
set $name kgc;
echo $name;
break;          //添加 break 指令
set $my_port $server_port;
echo $my_port;
echo $host;
}
}

[root@node1 ~]#  nginx -s reload   //重新加载

[root@node2 ~]#  curl 192.168.204.10/test    //验证
kgc
                //break指令下的set指令不执行
192.168.204.10

break下如果出现同属于rewrite模块的指令(if、return、set、rewrite等),那么break后的指令将不再执行。
  1. rewrite 指令

通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理

语法格式:

点击查看代码
Syntax:	rewrite regex replacement [flag];
Default:Context:	server, location, if

//正则匹配原始访问url    替代你想让客户访问的     标志 ()premanent301   redirect302  break  last

flag 说明:

类型 flag 区别
跳转型 redirect 临时重定向302
permanent 永久重定向301
代理型 break 是立即终止匹配  使用该url进行匹配
last 停止本location中的匹配,开启新一轮的location匹配
  • 跳转型指由客户端浏览器重新对新地址进行请求
  • 代理型是在WEB服务器内部实现跳转
  • break 和 last 是为了防止死循环使用的

示例1

实验效果:访问 bj 等于访问 beijing

点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf  

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

 location /bj {      
 root /data/;   //注意!url为bj时访问此目录下的页面,但是重写后url变为beijing,就不再生效
 rewrite ^/bj/(.*)   /beijing/$1   permanent;   //添加rewrite指令,$1代表后项引用
 }
}

[root@node1 ~]#  cd html
[root@node1 html]#  mkdir beijing        //创建目录
[root@node1 html]#  echo /data/beijing/ > beijing/index.html   //生成页面


[root@node1 data]#  nginx -t         //检查格式
[root@node1 data]#  nginx -s reload  //重新加载

浏览器访问192.168.204.10/bj/    //注意bj后面要加/,不然会报错404
等于访问192.168.204.10/beijing/

示例2
实验效果:整个网页跳转 ,www.pc.com 跳转到 www.m.com

点击查看代码
[root@node1 html]#  vim /apps/nginx/conf.d/pc.conf

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

 location / {
 rewrite  /  http://www.m.com  permanent;   //重写
 }
}


server  {
listen 80;
server_name  www.m.com;
root /opt;
}

[root@node1 html]#  cd /opt
[root@node1 opt]#  echo "/opt/wo shi m.com" > index.html   //生成页面
[root@node1 opt]#  nginx -s reload   //重新加载

真机 在C:\Windows\System32\drivers\etc下编辑hosts文件,添加域名

浏览器访问192.168.204.10/  会跳转到www.m.com

示例3
实验效果:http 转 https

点击查看代码
[root@node1 ~]#  vim /apps/nginx/conf.d/pc.conf 

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

   location / {
   if  ( $scheme = http ) {
   rewrite ^/(.*)$ https://www.pc.com/$1 permanent;  //重写
   }
  }
}

[root@node1 ~]#  nginx -s reload   //重新加载

浏览器输入http://www.pc.com
会跳转为  https://www.pc.com

  1. 防盗链
    防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种:
  • none:请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
  • blocked:请求报文有referer首部,但无有效值,比如为空。
  • server_names:referer首部中包含本主机名及即nginx 监听的server_name。
  • arbitrary_string:自定义指定字符串,但可使用 * 作通配符。示例:.kgc.org 或 www.kgc.
  • regular expression:被指定的正则表达式模式匹配到的字符串,要使用 ~ 开头,例如:~.*.kgc.com

实现盗链
7-1:192.168.204.10
7-2:192.168.204.20

7-1上传一张图片;

点击查看代码
[root@node1 ~]#  cd /data/html
[root@node1 html]#  rz   //上传一张图片

[root@node1 html]#  ls
about  beijing  download  index.html  passwd.html  xhr.jpg  //图片上传成功

7-2安装nginx,自定义页面;
点击查看代码
[root@node2 ~]#  yum install epel-release.noarch -y
[root@node2 ~]#  yum install nginx               //yum安装nginx
[root@node2 ~]#  systemctl start nginx           //开启服务
[root@node2 nginx]#  cd /usr/share/nginx/html/   //切换目录
[root@node2 html]#  ls
404.html  50x.html  en-US  icons  img  nginx-logo.png  poweredby.png

[root@node2 html]#  vim index.html    //生成页面

<html>
<body>
<h1>this is Bob  </h1>
<img src="http://192.168.204.10/xhr.jpg"/>     //指明图片的源位置
</body>
</html>

[root@node2 html]#  systemctl restart nginx    //重启服务

浏览器输入7-2的ip地址 192.168.204.20 ;
7-2中实际并不存在xhr.jpg这张图片,但由于进行了盗链设置,可以直接使用7-2的ip地址访问7-1内的图片。

实现防盗链

Nginx防盗链主要是通过对请求的Referer头部字段进行检查,如果请求不是从合法的来源(比如自己的网站)来的,Nginx会返回一个错误码,通常是403 Forbidden。

  1. 编辑7-1的配置文件;
点击查看代码
rerfer你从哪里跳转过来的
设置有效值 来确定是否允许访问我站点上的图片

[root@node1 html]#  vim /apps/nginx/conf.d/pc.conf    //设置防盗链

server  {
listen 80;
listen 443 ssl;
        ssl_certificate /opt/www.pc.com.crt;
        ssl_certificate_key /opt/www.pc.com.key;
server_name  www.pc.com;
root  /data/html/;

    location ~* \.(jpg|gif|swf|jpeg|bmp)$ {       //开启正则,匹配不区分大小写,以.jpg或.gif或.swf结尾的文件
    valid_referers none blocked *.pc.com pc.com;  //设置信任的网站,可以正常使用图片。合法的refer:空、无效值或.pc.com结尾或pc.com
        if ( $invalid_referer ) {                 //如果为非法值,就返回403
       #rewrite ^/ http://www.pc.com/error.png;   //准备一个错误图片返回
        return   403;
        }
    }
}

[root@node1 html]#  nginx -t           //检查格式
[root@node1 html]#  nginx -s reload    //重新加载

2. 验证:浏览器输入7-2的ip192.168.204.20进行验证;未显示图片,防盗链设置成功。
posted @   leikj  阅读(40)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示