リダイレクト サーバーとメイン サーバーがある場合、nginx でサーバーにどのような名前を付ければよいでしょうか?

リダイレクト サーバーとメイン サーバーがある場合、nginx でサーバーにどのような名前を付ければよいでしょうか?

メイン サーバーが を提供しhttps://www.example.com、リダイレクト サーバーがhttp://www.example.comにリダイレクトするとhttps://www.example.comします。これら 2 つのサーバーにどのような名前を付ければよいでしょうか。これに関するベスト プラクティスはありますか。

サーバー名は主に個人の好みによって決まるため、何でも付けることができるのでしょうか?

答え1

両方とも という名前を付ける必要がありますwww.example.com。この名前は実際には非常に重要です。同じ IP アドレスで複数の仮想ホストをホストする場合、 は、nginxリクエストを処理する仮想ホストを http リクエストのヘッダーにある仮想ホストと一致させるために名前を使用するためですHost。仮想ホストが 2 つあるがプロトコル (http/https) が異なる場合は、プロトコル (またはポートListen) を使用して 2 つの仮想ホストをさらに区別します。

答え2

サーバー名は、nginx 構成の重要な部分です。単一のパブリック IP を持つサーバーから複数のドメイン/サブドメインを提供する場合、server_nameディレクティブは、リクエストに応答するサーバーを nginx に指示します。一致するサーバー名がない場合、nginx は次に、存在する場合は何らかの「キャッチオール」サーバーとして機能するデフォルト サーバーを探します。

こちらは設定例http -> https リダイレクトの場合、エラーが発生することなくサーバーに正しく名前を付ける方法も確認できます。

/etc/nginx/sites-available/www.example.com.vhost

# This server will redirect all http -> https
server {
        # Listen for both IPv4 and IPv6 requests.
        listen 80;
        listen [::]:80;
        server_name www.example.com example.com;

        # This section assuming you're using Let's Encrypt / certbot
        location ^~ /.well-known/acme-challenge/ {
            default_type "text/plain";
            root         /path/to/your/document/root
        }
        location = /.well-known/acme-challenge/ {
            return 404;
        }

        # Here's where your 301 permanent redirect to the https:/www version happens.
        return 301 https://www.example.com$request_uri;

}

# This server will redirect all https requests without www to the version with www
server {
        # Listen for both IPv4 and IPv6 requests and activate http2.
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name example.com;

        ssl on;
        # Paths to certificates assuming you're using Let's Encrypt / certbot
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
        # There's much more directives you should fine tune for https, but that's another task.

        # Here's where your 301 permanent redirect to the https://www version happens.
        return 301 https://www.example.com$request_uri;
}

# This server will actually server your https content.
server {
        # Listen for both IPv4 and IPv6 requests and activate http2.
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        server_name www.example.com;

        ssl on;
        # Paths to certificates assuming you're using let's encrypt / certbot
        ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
        # There's much more directives you should fine tune for https, but that's another task.

        ####
        #
        # Your server directives go here.
        #
        ####
}

関連情報