在Nginx服务器上设置WordPress伪静态需要正确配置Nginx的rewrite规则。以下是详细步骤:
在Nginx的站点配置文件中(通常在/etc/nginx/sites-available/your-site.conf
),添加以下location块:
server {
# ... 其他配置 ...
location / {
try_files $uri $uri/ /index.php?$args;
}
# ... 其他配置 ...
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/wordpress;
index index.php index.html index.htm;
access_log /var/log/nginx/wordpress.access.log;
error_log /var/log/nginx/wordpress.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
}
# 测试Nginx配置是否正确
sudo nginx -t
# 重载Nginx配置
sudo systemctl reload nginx
出现404错误:
root
路径正确指向WordPress安装目录.php
文件的处理配置正确重定向循环:
wp-config.php
中WP_HOME
和WP_SITEURL
设置是否正确特定页面无法访问:
如果你使用WordPress多站点,需要使用不同的rewrite规则:
location / {
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
完成以上设置后,你的WordPress网站应该能够正常使用伪静态URL了。