基本伪静态规则
server {
listen 80;
server_name example.com;
root /path/to/your/website;
# 启用PHP支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 伪静态规则:将/article/123.html重写为index.php?id=123
location /article/ {
rewrite ^/article/([0-9]+)\.html$ /index.php?id=$1 last;
}
# 其他静态文件直接访问
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
WordPress 伪静态规则
server {
listen 80;
server_name example.com;
root /path/to/wordpress;
index index.php index.html;
# 启用PHP支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# WordPress伪静态规则
location / {
try_files $uri $uri/ /index.php?$args;
}
# 静态文件缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
自定义多参数伪静态
server {
listen 80;
server_name example.com;
root /path/to/your/website;
# 启用PHP支持
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 多参数伪静态规则
location /product/ {
rewrite ^/product/cat([0-9]+)/item([0-9]+)\.html$ /product.php?cat=$1&item=$2 last;
}
# 静态文件处理
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
}
注意事项
sudo nginx -s reload
tail -f /var/log/nginx/error.log