Nginx下泛目录伪静态的编写方法

当使用懒怂站群的时候,因为使用了伪静态致使任意后缀均可打开模板欲显示的内容,所以一些不存在的js文件,当被调用时,也异常呈现出诡异的HTML代码。

举例:某个网站的内页,一般有访问次数的统计,这是一段js,发出请求后使用js对访问量进行增加。如:常见的<span id="lb_clicks">0<span>,这显而易见是通过js的点击对span标签内的数据进行修正。

实际操作中发现很多时候由于仿站小工具下载模板,当没有采集下载到的js,回传的数据也要也要生成,当使用下文中的伪静态A的时候,则会返回类似前页面一样的HTML代码。

最简单的解决办法是,修改伪静态的,消灭这种错误回传。

Nginx下泛目录伪静态的编写方法-第1张图片

小白伪静态教程

伪静态A:

当内页链接支持泛后缀,如htm、html、shtml、asp、jsp、php时,请将伪静态设置为:

rewrite ^/(.*)\.xml$ /sitemap.php;
rewrite ^/index.html$ /index.php;
if (!-e $request_filename){
  rewrite ^/(.*)/$ /list.php;
  rewrite ^/(.*)$  /show.php;
}
location ~ \.(txt)$ {
deny all;
}

伪静态B:

当内页链接的后缀仅需要支持html,请将伪静态设置为:

rewrite ^/(.*)\.xml$ /sitemap.php;
rewrite ^/index.html$ /index.php;
if (!-e $request_filename){
  rewrite ^/(.*)/$ /list.php;
  rewrite ^/(.*).html$  /show.php;
}
location ~ \.(txt)$ {
deny all;
}

伪静态缩写

当我们既想要想要支持泛后缀,又不想现实错误代码,往往我们用最土的写法,会得到下面的伪静态:

rewrite ^/(.*)\.xml$ /sitemap.php;
rewrite ^/index.html$ /index.php;
if (!-e $request_filename){
  rewrite ^/(.*)/$ /list.php;
  rewrite ^/(.*).htm$  /show.php;
  rewrite ^/(.*).html$  /show.php;
  rewrite ^/(.*).shtml$  /show.php;
  rewrite ^/(.*).asp$  /show.php;
  rewrite ^/(.*).jsp$  /show.php;
  rewrite ^/(.*).php$  /show.php;
}
location ~ \.(txt)$ {
deny all;
}

那么我们还可以将上述伪静态进行缩写:

rewrite ^/(.*)\.xml$ /sitemap.php last;
rewrite ^/(index)\.(html|php)$ /$1.php last;
if (!-e $request_filename) {
  rewrite ^/(.*)/$ /list.php last;
  rewrite ^/(.*)\.(htm|html|shtml|asp|jsp|php)$ /show.php last;
}
location ~* \.txt$ {
  deny all;
}

上述规则相比于原始规则,缩短了重复部分,同时省略了一些可选项和不必要的语法。这些规则可以更好地优化Nginx服务器的性能。

值得注意的是,在应用缩写规则时应该测试它们是否能够正确地处理流量。如果在测试期间发现任何问题,则应该通过向规则中添加必要的语法来纠正它们。

本站所有内容仅限用于学习和研究目的,程序仅供本地断网测试,转载请说明出处!
站群SEO » Nginx下泛目录伪静态的编写方法

发表评论

欢迎 访客 发表评论

聚合全网站群程序及推送工具!

联系站长 联系客服