2

Nginx伪静态排除指定目录伪静态规则设置

 2 years ago
source link: https://www.huhexian.com/23460.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx伪静态排除指定目录伪静态规则设置

2022-01-1709:19:53评论768字

伪静态只是改变了URL的显示形式,实际上还是网站页面还是动态页面。伪静态的页面后缀可以是html、htm或者是目录格式等,伪静态有利于搜索引擎的收录,能够增加网站优化效果。那么Nginx如何做伪静态排除指定目录伪静态规则设置呢。

Nginx伪静态排除指定目录伪静态规则设置

方案一、
location ~* ^/(?![static|h5]) { .. 伪静态规则 }通过 location ~* ^/(?![sys-static|h5])  来设置排除目录,| 分割多个目录。

  1. location ~* ^/(?![static|h5]) {
  2. ... 伪静态规则
  1. location ~* ^/(?![static|h5]) {
  2.     rewrite ^(.*)/([0-9a-zA-Z]+)/$ $1/index.php?url=article/index&pathname=$2 last;
  3.     rewrite ^(.*)/([0-9a-zA-Z]+)-([0-9]+).html$ $1/index.php?url=article/index&pathname=$2&page=$3 last;

这个排除了static和h5目录

注意:这个方法也不知道是不是我姿势不正确。实际测试有问题,location 中的伪静态规则只有第一个栏目规则和第一个栏目的文章规则执行正常,后面的均不正确。建议使用后面两种方案。

方案二、

所以有了第二种让nginx伪静态排除指定目录方案,使用多个 location,不再使用反向排除的思路,改用匹配到该规则,使用 break 语句停止 rewrite 检查。

  1. location ~* /h5/|/sys-static/{
  2.     break;
  1. location ~* {

方案三、

还可以使用请求某个真实存在的文件跳出。示例:

  1. location ~* {
  2. if (-f $request_filename) {
  3. break;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK