【发布时间】:2021-03-17 19:14:58
【问题描述】:
我很确定这只是一个基本的配置。我已经启动并运行了一个带有基本域的 wordpress 多站点。我想实现简单的域映射,不幸的是 nginx 配置的 Wordpress 文档非常缺乏。
我在 9 个月前尝试使用 WU Domain Mapping plugin 成功地做到了这一点,但这次它无法正常工作,我宁愿让它在没有插件的情况下工作(旨在安装低插件 WP)。
当然,我已经修改了站点部分中的站点 URL 来测试这一点。
我目前拥有的是:
site1.com (working)
site2.site1.com (working)
site2.com (301 redirect loop)
我想要的是:
site1.com
site2.com
有什么建议我需要添加到我的 Nginx conf 文件中吗?我当前的 nginx 配置文件与Digital Ocean Docker 设置页面的配置文件几乎相同:
server {
listen 80;
listen [::]:80;
server_name *.site1.com site1.com www.site1.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
rewrite ^ https://$host$request_uri? permanent;
}
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
server {
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name site1.com www.site1.com;
index index.php index.html index.htm;
root /var/www/html;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/site1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site1.com/privkey.pem;
include /etc/nginx/conf.d/options-ssl-nginx.conf;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
# add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# enable strict transport security only if you understand the implications
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wordpress:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off; access_log off;
}
location = /robots.txt {
log_not_found off; access_log off; allow all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
}
更新:
如果我登录并运行 nginx 命令检查 nginx Docker,我会收到以下错误消息:
编辑 - 这也发生在工作配置上,所以我猜它可能是一个红鲱鱼
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
2021/03/18 10:07:11 [emerg] 29#29: bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
2021/03/18 10:07:11 [emerg] 29#29: bind() to 0.0.0.0:443 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:443 failed (98: Address already in use)
2021/03/18 10:07:11 [emerg] 29#29: bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
2021/03/18 10:07:11 [emerg] 29#29: still could not bind()
在容器内,netstat 为端口 80/443 提供了这个:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1/nginx: master pro
tcp 0 0 172.18.0.2:38422 199.232.138.132:80 TIME_WAIT -
tcp6 0 0 :::80 :::* LISTEN 1/nginx: master pro
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1/nginx: master pro
tcp6 0 0 :::443 :::* LISTEN 1/nginx: master pro
在容器外它似乎只正确运行了 1 个端口:
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1298/docker-proxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1313/docker-proxy
编辑(18/3/21):
刚刚从另一个 IP 地址的工作备份中进行了恢复,但我仍然遇到这个特定域的问题。有趣的是,我用不同的域建立了另一个站点,并且那个站点正在运行!
我会检查配置,看看里面是否有任何不同,如果没有,我想我需要沿着 DNS 路由,检查它是否正确地针对我的主机。
【问题讨论】:
-
好像你没有site2.com的server_name配置,你能检查一下吗?
-
我确实尝试过,就像
server_name *.site1.com site1.com www.site1.com *.site2.com site2.com www.site2.com;中的 80 和 443 端口部分一样,它目前仍然是这样设置的,并且仍在重定向。 -
你可以检查你的主题functions.php并添加
remove_filter('template_redirect', 'redirect_canonical');行吗? -
试过了,在 2021 年或 2019 年的基本主题中都不起作用。嘿,谢谢你的帮助!