Nginx multidomain www to non-www redirect goes to wrong domain

I have setup a multilangual Wordpress multiste site on an nginx server (the current english page is not translated yet but it is coming):

  • English version: appscaptain.com
  • Danish version: appscaptain.dk

Both should redirect from the www to non-www respectively.

www.appscaptain.com currently redirects correctly to appscaptain.com, but for some reason:

  • appscaptain.dk sometimes redirects to appscaptain.com and
  • www.appscaptain.dk sometimes doesn't redirect to appscaptain.dk, but instead to appscaptain.com.

It's weird that it doesn't happen consistently.

Can anyone spot the issue in the Nginx rule?

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;

P.S. It is setup under 1 nginx rule and one wordpress directory to make the localization domain mapping work (Polylang). I can't split it under two separate rule files.

0
задан 15 April 2018 в 23:11
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

}
0
ответ дан 5 December 2019 в 06:10

Теги

Похожие вопросы