Nginx 多域名 www 到非 www 重定向到错误域名

Nginx 多域名 www 到非 www 重定向到错误域名

我已经在 nginx 服务器上设置了一个多语言 Wordpress 多站点(当前英文页面尚未翻译,但即将推出):

  • 英文版:appscaptain.com
  • 丹麦语版本:appscaptain.dk

两者都应该分别从 重定向wwwnon-www

www.appscaptain.com目前正确重定向到appscaptain.com,但由于某种原因:

  • appscaptain.dk有时会重定向appscaptain.com
  • www.appscaptain.dk有时不会重定向到appscaptain.dk,而是appscaptain.com

奇怪的是,这种情况并不持续发生。

有人能发现 Nginx 规则中的问题吗?

server {
    listen 80;
    server_name appscaptain.com www.appscaptain.com appscaptain.dk www.appscaptain.dk;
    rewrite ^ (.*) https: //appscaptain.com$1 permanent;
}
server {
    listen 443 ssl http2;
    server_name www.appscaptain.com;
    return 301 https: //appscaptain.com$request_uri;
    ssl_certificate / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.crt;
    ssl_certificate_key / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.key;
    ssl_session_cache shared: SSL: 10m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    include / etc / nginx / conf / ssl - protocol - cipher.conf;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid = 300s;
    resolver_timeout 30s;
    ssl_trusted_certificate / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.ca;
    ssl_buffer_size 1400;
    ssl_session_tickets on;
    add_header Strict - Transport - Security max - age = 31536000;
    access_log off;
    access_log / home / appscaptain.com / logs / access_log;
    error_log off;
    error_log / home / appscaptain.com / logs / error.log;
    add_header X - Frame - Options SAMEORIGIN;
    add_header X - Content - Type - Options nosniff;
    add_header X - XSS - Protection "1; mode=block";
}
server {
    listen 443 ssl http2;
    ssl_certificate / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.crt;
    ssl_certificate_key / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.key;
    ssl_session_cache shared: SSL: 10m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    include / etc / nginx / conf / ssl - protocol - cipher.conf;
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid = 300s;
    resolver_timeout 30s;
    ssl_trusted_certificate / etc / nginx / auth - acme / appscaptain.com / appscaptain.com.ca;
    ssl_buffer_size 1400;
    ssl_session_tickets on;
    add_header Strict - Transport - Security max - age = 31536000;
    access_log off;
    access_log / home / appscaptain.com / logs / access_log;
    error_log off;
    error_log / home / appscaptain.com / logs / error.log;
    add_header X - Frame - Options SAMEORIGIN;
    add_header X - Content - Type - Options nosniff;
    add_header X - XSS - Protection "1; mode=block";
    root / home / appscaptain.com / public_html;
    include / etc / nginx / conf / ddos2.conf;
    index index.php index.html index.htm;
    server_name appscaptain.com appscaptain.dk www.appscaptain.dk;

PS 它设置在 1 个 nginx 规则和一个 wordpress 目录下,以使本地化域映射工作(Polylang)。我无法将其拆分到两个单独的规则文件下。

答案1

试试这个,我这样做只是为了让一切都清楚,而且你还需要在 wordpress 上使用另一个配置,这样它才能接受两个域名。看起来你的配置复制粘贴有问题,

   server {
    listen 80;
    server_name appscaptain.com www.appscaptain.com ;
    rewrite ^ (.*) https://appscaptain.com$1 permanent;
}

server {
    listen 80;
    server_name appscaptain.dk www.appscaptain.dk;
    rewrite ^ (.*) https://appscaptain.dk$1 permanent;
}

server {
    listen 443 ssl http2;
    server_name www.appscaptain.com;
    return 301 https://appscaptain.com$request_uri;
    ..... >> SSL config to your ssl cert
}

server {
    listen 443 ssl http2;
    server_name www.appscaptain.dk;
    return 301 https://appscaptain.dk$request_uri;
    ..... >> SSL config to your ssl cert
}

server {
    listen 443 ssl http2;
    server_name appscaptain.com appscaptain.dk;
    .................. >>> Your config here including your index root

}

相关内容