2016-11-02 10 views
2

再びnginxリダイレクトの問題。私は自分のウェブサイトがhttps上でのみアクセス可能で、常にサブドメインwww(またはappまたはapi)でアクセス可能である必要があります。次の設定でhttps://example.com(wwwなし)を受け入れる方法はありますか?これは私が今見ているものなので、理由を説明することができません。ただ、nginxの設定には他のサーバセクションはないと付け加えます。Nginxはwwwにリダイレクト

server { 
    listen 80; 
    server_name ~^(?<subdomain>www|app|api)\.example\.com$; 
    index index.html index.htm; 
    return   301 https://$subdomain.example.com$request_uri; 
} 
server { 
    listen 443 ssl; 
    server_name ~^(?<subdomain>www|app|api)\.example\.com$; 
    root /var/www/html/pathToMyWebsite; 
    index index.php; 
} 

編集:ここで私は@ivanに感謝を使用して終了するものである:

# Redirects http://example.com & https://example.com to https://www.example.com 
server { 
    listen 80; 
    listen 443 ssl; # Here is the trick - listen both 80 & 443. 

    # other ssl related stuff but without "ssl on;" line. 

    server_name example.com; 
    return 301 https://www.example.com$request_uri; 
} 
server { 
    listen 80; 
    server_name ~^(?<subdomain>www|app|api)\.example\.com$; 
    index index.html index.htm; 
    return   301 https://$subdomain.example.com$request_uri; 
} 
server { 
    listen 443 default_server ssl; 
    server_name ~^(?<subdomain>www|app|api)\.example\.com$; 
    root /var/www/html/pathToMyWebsite; 
    index index.php; 
} 

通知 "DEFAULT_SERVERは、" 私はnginxの中に正規表現を使用して、すべてのlisten 443 default_server ssl;

答えて

2

ファーストに追加設定は通常、良い解決策ではありません。 regexpは複雑な複雑さをもたらすので、ほとんどの場合、configブロックをコピー&ペーストしたり、includeステートメントを使用する方が妥当です。もちろん、使用するものとはトレードオフのようなものです。

# Redirects http://example.com & https://example.com to https://www.example.com 
server { 
    listen 80; 
    listen 443 ssl; # Here is the trick - listen both 80 & 443. 

    # other ssl related stuff but without "ssl on;" line. 

    server_name example.com; 
    return 301 https://www.example.com$request_uri; 
} 

# Redirects http://*.example.com to https://*.example.com 
server { 
    listen 80; 
    server_name ~^.+\.example\.com$; 
    return 301 https://$host$request_uri; 
} 

server { 
    listen 443; 
    server_name ~^.+\.example\.com$; 
    ssl on; 
    # ... 
} 
+0

は、私はあなたのソリューションと鉱山のミックスを使用して終了:

しかし、私は私のconfigsではかなり同じ使用パターンを持っています。私は 'listen default_server 443;'を入れなければなりませんでした。なぜなら、SSLハンドシェイク中にSSLポートでリッスンしているサーバでこのエラーが発生していないからです。 "no" ssl_certificate " – Axi

関連する問題