温馨提示×

lnmp如何设置伪静态

小樊
67
2026-08-31 19:05:00
栏目: 编程语言

在 LNMP(Linux + Nginx + MySQL + PHP) 环境下,伪静态通常是通过 Nginx 的 rewrite 规则 实现的。下面按常见场景给你说明。


一、LNMP 中伪静态的基本原理

Nginx 本身不支持 .htaccess,伪静态需要在 站点配置文件 或 伪静态规则文件 中设置 rewrite。

常见路径:

  • 站点配置:/usr/local/nginx/conf/vhost/域名.conf
  • 伪静态规则:/usr/local/nginx/conf/rewrite/域名.conf

二、LNMP 一键包设置伪静态(最简单)

如果你用的是 LNMP 一键安装包:

lnmp vhost add

添加站点时会问:

Add rewrite rule? (y/n)

选 y,然后会让你选择:

  • wordpress
  • typecho
  • discuz
  • thinkphp
  • 其他

选对应程序即可自动生成伪静态。

✅ 已存在站点可重新设置:

lnmp vhost rewrite 域名

三、手动配置伪静态(推荐了解)

1️⃣ 编辑站点配置

vi /usr/local/nginx/conf/vhost/yourdomain.conf

示例(WordPress):

server {
    listen 80;
    server_name yourdomain.com;
    root /home/wwwroot/yourdomain;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
}

2️⃣ 使用独立伪静态文件(LNMP 推荐)

在 yourdomain.conf 中引入:

include rewrite/wordpress.conf;

创建规则文件:

vi /usr/local/nginx/conf/rewrite/wordpress.conf

内容:

rewrite ^/(.*)$ /index.php/$1 last;

或 WordPress 常用:

try_files $uri $uri/ /index.php?$args;

四、常见程序伪静态规则

✅ WordPress

try_files $uri $uri/ /index.php?$args;

✅ Typecho

if (!-f $request_filename){
    rewrite ^/(.*)$ /index.php/$1 last;
}

✅ ThinkPHP

location / {
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php?s=$1 last;
    }
}

✅ Discuz!

rewrite ^/topic-(.+)\.html$ /portal.php?mod=topic&topic=$1 last;
rewrite ^/forum-(\d+)-(\d+)\.html$ /forum.php?mod=forumdisplay&fid=$1&page=$2 last;

五、修改后必须重载 Nginx

nginx -t
lnmp nginx reload

或:

systemctl reload nginx

六、常见问题

❌ 伪静态不生效

  • 没 reload nginx
  • rewrite 写错 last / break
  • 程序后台未开启伪静态

❌ 出现 404

  • try_files 路径不对
  • root 目录配置错误

如果你告诉我:

  • 用的 什么程序(WP / TP / 自写)
  • 是否 LNMP 一键包
  • 报错信息

我可以直接帮你写一份可用的伪静态配置。

0 踩