nginx 리디렉션 구성에서 중복을 피하시겠습니까?

nginx 리디렉션 구성에서 중복을 피하시겠습니까?

리디렉션 설정wwwwww가 아닌그리고HTTPHTTPS동시에 나는 극복하지 못한 중복 문제에 직면했습니다.

내 도메인에 example.com기본 이름이 인 웹사이트가 있습니다 another.example.com. example.com, www.example.com및 에 대한 요청이 로 www.another.example.com리디렉션되고 another.example.com모든 HTTP 요청이 동시에 HTTPS로 리디렉션되기를 원합니다. 또한 HTTP/2와 IPv6도 지원하고 싶습니다.

이 작업을 수행하는 데에는 문제가 없지만 구성 파일의 상당 부분(즉, HTTPS 인증서 설정) 복제를 제거하는 데 실패했습니다. 중복을 줄이려는 모든 시도로 인해 하나 이상의 리디렉션 또는 모든 리디렉션이 작동을 중지합니다(때로는 HTTP/2와 함께).

구성을 살펴보고 정리 방법을 제안하십시오.

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

server {
    listen 443;
    listen [::]:443;
    server_name www.another.example.com www.example.com example.com;
    return 301 https://another.example.com$request_uri;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    server_name another.example.com;
    root /usr/share/nginx/another.example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl http2;
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

답변1

server {
    server_name another.example.com;
    root /usr/share/nginx/another.example.com;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    listen [::]:443 ssl http2;
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.another.example.com www.example.com example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    rewrite ^/(.*)$ https://another.example.com/$1 permanent;
}
server {
    listen 80;
    listen [::]:80;
    server_name www.another.example.com www.example.com another.example.com example.com;
    location / {
        if ($host !~* ^(www)) {
          rewrite ^/(.*)$ https://another.example.com/$1 permanent;
        }
    }
}

관련 정보