当前位置: 首页 > news >正文

web网页设计模板代码/seo发外链工具

web网页设计模板代码,seo发外链工具,域名服务器搭建,网上商城前端模板Nginx Rewrite相关功能 Nginx服务器利用ngx_http_rewrite_module模块处理rewrite请求,此功能依靠RCRE(perl compatible regularexpression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写&…

Nginx Rewrite相关功能

Nginx服务器利用ngx_http_rewrite_module模块处理rewrite请求,此功能依靠RCRE(perl compatible regularexpression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需 其他网站修改我们的链接,就可以设置为访问,另外还可以在一定程度上提高网站的安全性。

Nginx_http_rewrite_module模块指令

rewrite模块相关的指令有break、if、return、rewrite、rewrite_log、set

if指令

用于条件匹配的判断,并根据条件判断结果选择不同的nginx配置,可以配置在server或Location块中进行配置,Nginx的if语法仅能使用if做单次判断。不支持使用if...else或者if...elif这种多重判断

#语法格式
if (条件匹配) {action
}

if指令需要使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为ture,否则认为false。变量与表达式之间用符号进行连接

=: #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false。 
!=: #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。 
~: #表示在匹配过程中区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足为假。 
~*: #表示在匹配过程中不区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足问假。 
!~:#区分大小写不匹配,不满足为真,满足为假,不满足为真。 
!~*:#为不区分大小写不匹配,满足为假,不满足为真。-f 和 ! -f: #判断请求的文件是否存在和是否不存在 
-d 和 ! -d: #判断请求的目录是否存在和是否不存在。 
-x 和 ! -x: #判断文件是否可执行和是否不可执行。 
-e 和 ! -e: #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)

示例:
1.= 判断用户请求的值是否等于所设定的值,如果匹配成功则执行后面的命令
判断用户请求的scheme是否为http

server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $scheme = http ){echo scheme is $scheme;}}
}

测试访问

[root@www ~]# curl http://www.mylinuxops.com
scheme is http

2.!= 判断用户的是否不等于所设定的值,如果不等于则执行后面的命令

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $scheme != http ){echo scheme is $scheme;}}
}

测试访问

[root@www ~]# curl http://www.mylinuxops.com
mylinuxops.com      #由于$scheme和http相等所以执行默认操作,输出默认页面

3.判断文件是否存在,如果存在输出文件存在

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( -f $request_filename ) {echo "file is exist";}}
}

测试访问

[root@www ~]# curl http://www.mylinuxops.com
file is exist       #主页文件存在所以输出一个文件存在的信息

4.判断文件是否不存在,如果不存在输出文件不存在

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( !-f $request_filename ) {echo "file is not exist";}}
}

测试访问一个不存在的文件

[root@www ~]# curl www.mylinuxops.com/123
file is not exist       #输出文件不存在

在生产环境中,如果文件不存在应该将其重定向到一个可以访问的页面

break指令

用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就不再生效了,Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server块和location块以及if块中使用。
用到此指令的场景不多,一般在判断中使用,就是跳出后不再执行location后续的操作
示例:
1.没有使用break时

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;set $name mylinuxops;echo $name;#break;set $my_port $server_port;echo $my_port;}}

测试

[root@www ~]# curl www.mylinuxops.com
mylinuxops
80

使用break后

[root@www ~]# curl www.mylinuxops.com
mylinuxops          #后面端口号部分被打断没有输出

return指令

从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,比如其可以指定重 定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令 后的所有配置都将不被执行,return可以在server、if和location块进行配置

#语法格式
return code [text]; #返回一个状态相应码+一段字符串
return code URL;    #返回一个状态相应码+跳转的URL
return URL;         #返回一个URL重定向到新的URL

示例:
1.return code [test];

server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $uri = /123 ) {return 404 "not exist";}}
}

测试

[root@www ~]# curl -I www.mylinuxops.com/123
HTTP/1.1 404 Not Found                  #返回一个404值
Server:
Date: Fri, 31 May 2019 13:50:58 GMT
Content-Type: application/octet-stream
Content-Length: 9
Connection: keep-alive
Keep-Alive: timeout=65[root@www ~]# curl www.mylinuxops.com/123
not exist                               #输出一个not exist字符串

2.return code URL;

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $uri = /123 ) {return 327 http://www.mylinuxops.com;}}
}

测试

[root@www ~]# curl -I www.mylinuxops.com/123
HTTP/1.1 327         #返回327值
Server:
Date: Fri, 31 May 2019 13:55:48 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Keep-Alive: timeout=65
Location: http://www.mylinuxops.com     #跳转的url

3.return URL;

[root@www ~]# vim /apps/nginx/conf/servers/vs.conf
server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $uri = /123 ) {return http://www.mylinuxops.com;}}
}

测试

[root@www ~]# curl -I www.mylinuxops.com/123
HTTP/1.1 301 Moved Permanently
Server:
Date: Fri, 31 May 2019 13:58:43 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Keep-Alive: timeout=65
Location: http://www.mylinuxops.com

rewrite指令

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

#语法格式
rewrite regex replacement [flag];

flag可以是last,break,redirect,permanent中的任意一个

last:重写完成后,对新的uri再次进行匹配
break:当重写完成后,就不再进行匹配直接输出改写后的uri
redirect:重写完成后以临时重定向的方式直接返回重写生后生成的新URL给客户端,由客户端发起新的请求;状态码302
permanent:重写完成后以永久重定向的方式生成新的URL发送给客户端,客户端重新发起连接,状态码301

临时重顶向和永久重定向的区别:
永久重定向会在浏览器中做缓存,当用户下次访问时会直接查缓存然后去访问重定向后的站点,这种重定向一般用在站点域名不再使用的情况下。
临时重定向不会做缓存,下次访问时浏览器依旧会先去站点,站点返回一个重定向,然后用户端再次发起请求访问重定向的域名,一般用在临时不让用户访问的情况下。
示例:
1.redirect

server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $scheme = http ) {rewrite / https://www.mylinuxops.com$uri redirect;}}
}

测试:

[root@localhost www]# curl -I www.mylinuxops.com/image
HTTP/1.1 302 Moved Temporarily               #302临时重定向
Server: 
Date: Fri, 31 May 2019 16:05:11 GMT
Content-Type: text/html
Content-Length: 154
Connection: keep-alive
Keep-Alive: timeout=65
Location: https://www.mylinuxops.com/image    #所有的http请求都被送往了https

2.permanent

[root@localhost www]# vim /apps/nginx/conf/servers/vs.conf server {server_name www.mylinuxops.com;access_log /var/log/nginx/access.log access_json;location / {root /data/www;index index.html;if ( $scheme = http ) {rewrite / https://www.mylinuxops.com permanent;}}
}

测试

[root@localhost www]# curl -I www.mylinuxops.com
HTTP/1.1 301 Moved Permanently          #301永久重定向
Server: 
Date: Fri, 31 May 2019 16:17:44 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Keep-Alive: timeout=65
Location: https://www.mylinuxops.com

3.break和last
修改配置文件

    location /break {root /data/www;index index.html;rewrite ^/break/(.*) /test/$1 break;
}location /last {rewrite ^/last/(.*) /test/$1 last;
}location /test {index index.html;root /data/www/hello;
}

在/data/www下建一个test目录创建一个index.html

[root@localhost test]# cat /data/www/test/index.html 
123 

在/data/www/hello下建一个test目录创建一个index.html

[root@localhost test]# cat /data/www/hello/test/index.html 
test2

测试查看break和last的区别

[root@localhost ~]# curl www.mylinuxops.com/break/index.html
123       #访问break时输出的是/data/www/test/index.html的内容
[root@localhost ~]# curl www.mylinuxops.com/last/index.html
test2     #访问last时输出的是/data/www/hello/test/index.html的内容

结论:
break在重写后会在root所定义的目录下寻找rewrite的uri。break常用于多版本的资源并存时的调用,访问方式不变改变访问的资源。
last在重写后会再次使用新的uri去匹配各location。

转载于:https://blog.51cto.com/11886307/2403944

http://www.jmfq.cn/news/5275027.html

相关文章:

  • 昌平网站建设推广优化seo/外贸网站推广费用
  • 南昌 网站/自己创建网站
  • 太原网站建设优化/搜一搜
  • 马云不会代码怎么做的网站/seo是什么车
  • 如何给网站做优化代码/广州优化疫情防控举措
  • 网站建设开发计划模板/友情下载网站
  • 成都互联网网站建设/小红书关键词排名
  • 教育培训网站建设方案模板/外贸营销平台
  • scratch编程免费下载/seo流量
  • 临沂哪家做网站最好/凡科建站下载
  • 甘孜建设网站首页/免费网站seo排名优化
  • 如何做一个网站的功能吗/搜索引擎推广一般包括哪些
  • 千峰培训多少钱/搜索引擎环境优化
  • wordpress jd哪个好/万能优化大师下载
  • 合肥做企业网站/广东今日最新疫情通报
  • 服务器做网站教程/优化设计方案
  • 自适应网站建设服务哪家好/app软件开发
  • 基于cms系统网站的建设/东莞优化排名公司
  • 移动网站建设动态/网站维护是什么意思
  • 网站维护 推广/免费推广引流app
  • 西安做网站公司哪家好 应该怎么选择/无锡网站建设优化公司
  • 站长工具seo综合查询下载安装/关键词整站优化
  • 做网站公司排行/登录百度账号注册
  • 广州建设银行投诉网站/关键词完整版免费听
  • 城阳建网站/网页设计制作网站模板图片
  • b站推广网站2024不用下载/网络推广代运营公司
  • 根据网站日志做seo/教你免费申请个人网站
  • 网站建设案例教程/360网站推广官网
  • wordpress主题更新无法创建目录/网站seo外链平台
  • 网站最下面版权模板/泉州seo报价