2017-10-19 15 views
0

私はNginxが新しく、サブドメインを適切に処理する方法を理解しようとしています。達成したいのは、メインドメインexample.comは常にhttps://www.example.comにリダイレクトされますが、sub.example.comというサブドメインは常にhttps://sub.example.comにリダイレクトされる必要があります。私の現在の設定では、最初の要件が満たされていますが、sub.example.comは常にhttps://www.sub.example.comにリダイレクトされます。私の設定の問題は何ですか?どうすれば修正できますか?nginx - サブドメインではなくメインドメインのみをリダイレクト

おかげで、Fabian。


私の2台のサーバーの設定ファイル:

server { 
    listen 80; 
    listen [::]:80; 
    server_name example.com www.example.com; 
    return 301 https://www.$host$request_uri; 
} 

server { 
    listen 443 ssl http2; 
    server_name example.com; 
    ssl_certificate /path/on/my/server/to/certificate.pem; 
    ssl_certificate_key /path/on/my/server/to/privatekey.pem; 
    return 301 https://www.$host$request_uri; 
} 


server { 
    listen 443 default_server ssl http2; 
    listen [::]:443 default_server ssl http2; 
    server_name www.example.com; 
    ssl_certificate /path/on/my/server/to/certificate.pem; 
    ssl_certificate_key /path/on/my/server/to/privatekey.pem; 

    root /var/www/html; 
    index index.html index.htm index.nginx-debian.html; 

    location/{ 
     try_files $uri $uri/ =404; 
     index index.php index.html index.htm; 
    } 
} 


サブ

デフォルト

server { 
    listen 80; 
    listen [::]:80; 
    server_name sub.example.com; 
    return 301 https://$host$request_uri; 
} 

server { 

    listen 443 ssl; 
    listen [::]:443 ssl; 
    server_name sub.example.com; 
    ssl_certificate /path/on/my/server/to/subcertificate.pem; 
    ssl_certificate_key /path/on/my/server/to/subprivatekey.pem; 

    root /var/www/sub; 

    location/{ 
     index index.php index.html index.htm; 
     try_files $uri = 404; 
    } 

    location ~ \.php$ { 
     try_files $uri = 404; 
     include fastcgi_params; 
     fastcgi_pass unix:/var/run/php7-fpm-web1.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
    } 
} 
+0

現在の設定では、「www.example.com」から「www.www.example.com」にリダイレクトされます。 'sub'設定ファイルが確実に読み込まれていますか?試してみてください: 'nginx -t'と' nginx -T' –

+0

はい、設定ファイルが読み込まれ、不思議なことに、ブラウザのキャッシュをクリアした後で動作します。 – FTFT1234

答えて

0

他の人にこの問題が発生した場合:ブラウザのキャッシュをきれいにしてください。

関連する問題