Nginx

Nginx 多域 www 到非 www 重定向到錯誤的域

  • April 16, 2018

我在 nginx 伺服器上設置了一個多語言 Wordpress 多站點(目前的英文頁面尚未翻譯,但即將推出):

  • 英文版:appscaptain.com
  • 丹麥語版本:appscaptain.dk

www兩者都應該分別從to重定向non-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)。我無法將其拆分為兩個單獨的規則文件。

試試這個,我讓這個效率低下只是為了讓一切都清楚,你還需要在 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

}

引用自:https://serverfault.com/questions/907740