- 您的帖子内容不能为空。
- 帖子

将网站的主域名 (abc.com) 和子目录 (abc.com/sub) 下的网站分别托管到不同的服务器。
这是很常见的架构设计,以下是几种实现方案:
方案一:Nginx 反向代理(推荐)
配置示例
# 主服务器配置 – 托管主域名内容
server {
listen 80;
server_name wpforo.cn http://www.abc.com;# 主站点根目录
root /var/www/main-site;
index index.php index.html;# 处理主站点请求
location / {
try_files $uri $uri/ /index.php?$args;
}# 将子目录代理到另一台服务器
location ^~ /sub/ {
# 反向代理到子目录服务器
proxy_pass http://子目录服务器 IP:端口/;# 代理设置
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;# 缓存设置
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
}# PHP处理
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}子目录服务器配置
# 子目录服务器配置
server {
listen 8080; # 可以监听内部端口
server_name _;# 注意:根目录要包含子目录路径
root /var/www/sub-site;
index index.php index.html;# WordPress重写规则
location / {
try_files $uri $uri/ /index.php?$args;
}location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;# 确保WordPress知道正确的URL
fastcgi_param SCRIPT_NAME /forum$fastcgi_script_name;
}
}方案二:DNS 子域名+独立服务器
更简单的替代方案
# 主服务器 – 主域名
server {
server_name abc.com http://www.abc.com;
# … 主站点配置
}# 子域名指向独立服务器
server {
server_name sub.abc.com;# 301重定向到子域名方式(推荐)
location / {
return 301 https://sub.abc.com$request_uri;
}
}
- 哎呀,回复帖子必需登录账户。



