在 Debian 下通过 Nginx 设置伪静态(URL Rewrite),通常用于 隐藏 index.php、美化 URL,常见于 WordPress、Typecho、Laravel、ThinkPHP 等程序。
下面按通用做法 + 常见程序示例说明。
Nginx 没有 Apache 的 .htaccess,伪静态通过:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
或 rewrite 规则实现。
一般路径:
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
网站配置通常在:
/etc/nginx/sites-available/your-site
sudo nano /etc/nginx/sites-available/default
server {
listen 80;
server_name yourdomain.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
}
✅ 适用于大多数 PHP 程序
location / {
try_files $uri $uri/ /index.php?$args;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
}
}
sudo nginx -t
sudo systemctl reload nginx
root 路径是否正确index.php 存在last 或 breakDebian + Nginx 伪静态 ≠ .htaccess
用
try_files或rewrite写在server {}配置里,然后nginx -s reload
如果你使用的是 某个具体程序(如 WordPress / Typecho / Laravel / 自建 PHP),可以告诉我,我可以给你精确配置。