리디렉션 서버와 기본 서버가 있는 경우 nginx에서 서버 이름을 어떻게 지정해야 합니까?

리디렉션 서버와 기본 서버가 있는 경우 nginx에서 서버 이름을 어떻게 지정해야 합니까?

내 주 서버가 서비스를 제공 https://www.example.com하고 리디렉션 서버가 http://www.example.com로 리디렉션된다고 가정합니다 https://www.example.com. 이 두 서버의 이름을 어떻게 지정해야 합니까? 이에 대한 일종의 모범 사례가 있습니까?

서버 이름은 주로 개인적으로 선호하는 것이므로 무엇이든 지정할 수 있습니까?

답변1

둘 다 이름이 지정되어야 합니다 www.example.com. 동일한 IP 주소에서 둘 이상의 가상 호스트를 호스트하는 경우 nginx이 이름을 사용하여 요청을 처리해야 하는 가상 호스트를 Hosthttp 요청 헤더의 가상 호스트와 일치시키기 때문에 이 이름은 실제로 매우 중요합니다. 두 개의 가상 호스트가 있지만 서로 다른 프로토콜(http/https)을 사용하는 경우 프로토콜(또는 포트 Listen)을 사용하여 두 가상 호스트를 더욱 구별할 수 있습니다.

답변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.
        #
        ####
}

관련 정보