Nginx伪静态
其实所谓的伪静态,就是去除地址里的动态参数,比如 ?、= 等,以便更适应搜索引擎优化搜索,当然,也可以美化我们的 url。
下面以文章 id 为 5 的文章页为例。原始地址为:
xxx = http://blog.******.com/?p=5
现在,我们可以将上述链接的伪静态地址定为:
xxx = http://blog.******.com/p/5.html
也就是说,当我们在地址栏中输入 xxx 时,需要服务器匹配到 xxx 匹配的路由。这个对于 nginx 而言,一个正则匹配就够了。
location / {
rewrite ^/p/(\d+).html$ /?p=$1; #将 /p/140.html 重写成 /?p=140
}如果有较多的匹配规则,可以将伪静态的路由重写抽离成一个单独的文件,在对应域名下引入重写文件即可。
# rewrite.conf
location / {
rewrite ^/p/(\d+).html$ /?p=$1; 将 /p/140.html 重写成 /?p=140
}# nginx.conf
location / {
include rewrite.conf
}





豫公网安备41042502000209号