编辑wp-config.php文件
/* 在"define('WP_DEBUG', false);"这行之前添加 */
define('WP_ALLOW_MULTISITE', true);
刷新WordPress后台
选择网络类型
按照提示完成设置
对于子域名类型的多站点:
server {
listen 80;
server_name ~^(www\.)?(?<sitename>.+)\.example\.com$;
root /var/www/wordpress;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# 其他必要配置...
}
对于子目录类型的多站点:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/wordpress;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
# 处理子目录站点的PHP请求
location ~ ^/[_0-9a-zA-Z-]+/.*\.php$ {
rewrite ^/([_0-9a-zA-Z-]+)/.*$ /$1 last;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# 其他必要配置...
}
server {
server_name example.com *.example.com;
root /var/www/wordpress;
index index.php;
# 通用重写规则
location / {
try_files $uri $uri/ /index.php?$args;
}
# 禁止访问敏感文件
location ~* ^/(wp-config.php|wp-config-sample.php|readme.html|license.txt) {
deny all;
}
# 禁止访问隐藏文件和目录
location ~ /\. {
deny all;
}
# PHP处理
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# 静态文件缓存
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
# 多站点特定规则
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^(/[^/]+)?(/wp-.*) $2 last;
rewrite ^(/[^/]+)?(/.*\.php) $2 last;
}
}
404错误
PHP文件下载而非执行
子站点无法访问
权限问题
完成配置后,不要忘记重新加载Nginx配置:
sudo nginx -t # 测试配置
sudo systemctl reload nginx # 重新加载配置