本地Nginx服务器多WordPress站点的同IP域名配置方法
你当前的Nginx配置存在错误——server_name字段不能包含路径(比如192.168.1.43/site1/wordpress),这个字段是用来定义站点专属域名的。要在本地服务器上实现多站点自定义域名访问,按以下步骤操作:
一、修正Nginx站点配置
将两个站点的server_name替换为自定义本地域名(比如site1.local和site2.local),同时确保根目录路径正确。修正后的配置如下:
站点1配置
server { listen 80; listen [::]:80; root /var/www/site1/wordpress; index index.php; server_name site1.local; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } }
站点2配置
server { listen 80; listen [::]:80; root /var/www/site2/wordpress; index index.php; server_name site2.local; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } }
二、配置本地Hosts文件
让你的本地设备把自定义域名解析到服务器IP192.168.1.43:
- Windows:编辑
C:\Windows\System32\drivers\etc\hosts文件,添加两行:192.168.1.43 site1.local 192.168.1.43 site2.local - Linux/macOS:编辑
/etc/hosts文件,添加同样的两行,保存后执行sudo dscacheutil -flushcache(macOS)或sudo systemctl restart systemd-resolved(Linux)刷新DNS缓存。
三、验证Nginx配置并重启
执行命令检查配置合法性:
sudo nginx -t
如果提示test is successful,重启Nginx服务:
sudo systemctl restart nginx
四、调整WordPress内部设置
分别访问site1.local/wp-admin和site2.local/wp-admin,进入设置→常规,将**WordPress地址(URL)和站点地址(URL)**对应修改为http://site1.local和http://site2.local,保存设置即可。
内容的提问来源于stack exchange,提问作者Sawyer
相关产品推荐
相关产品推荐

