nginx는 www에서 www가 아닌 ​​곳으로 다시 작성합니다.

nginx는 www에서 www가 아닌 ​​곳으로 다시 작성합니다.

내 nginx 파일이 실패하고 어떻게 실패하는지 잘 모르겠습니다. http에서 https로 리디렉션하는 데 성공했습니다. 하지만 www가 아닌 ​​버전으로 리디렉션하려면 www를 얻을 수 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/sammy/myproject;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
# added for let's encrypt
location /.well-known/ {
   root /home/sammy/myproject;
   allow all;
}
}
server {
# SSL configuration

listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;

    location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/myproject/myproject;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
}
# added for let's encrypt
location /.well-known/ {
   root /home/sammy/myproject;
   allow all;
}
}

나는 다음과 같은 추가 블록을 시도했습니다.

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

하지만 이것은 작동하지 않는 것 같았습니다. 아마도 443을 수신하도록 이것을 변경해야 한다고 생각했지만 이것이 SSL 서버 블록과 default_server 지시문에 어떤 영향을 미칠지 확신할 수 없었습니까?

답변1

$server_name가상 호스트 블록에 정의한 서버 이름을 나타냅니다 . 따라서 추가 블록으로 인해 리디렉션 주기가 다시 자체적으로 리디렉션됩니다.

변수 대신 리터럴 도메인 이름을 사용해야 합니다.

가 포함된 SSL 도메인의 경우 블록 및 인증서 값을 www추가해야 합니다 .listen 443 ssl;

따라서 이것이 세 번째 블록이 되어야 합니다.

server {
    listen 80;
    listen 443 ssl;
    server_name www.example.com;
    return 301 https://example.com$request_uri;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;
    ...
}

답변2

http에서 https로 전달하는 방법은 다음과 같습니다. 핵심 부분은 하단에 있는 3개의 서버 블록입니다.

server {
  server_name www.example.com;

  listen 443 ssl http2;

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  # This is a cache for SSL connections
  ssl_session_cache shared:SSL:2m;
  ssl_session_timeout 60m;

  root     /var/www/***rootdir;

  # First line is a cached access log, second logs immediately
  access_log  /var/log/nginx/mrwild.access.log main buffer=32k flush=1m if=$log_ua;
  # access_log  /var/log/nginx/mrwild.access.log main;

  # Default location to serve
  location / {
    log_not_found off;

    # This is a static site, so set the cache control headers to allow caching
    # for a day  
    add_header Cache-Control "public";
    expires 1d;

    valid_referers none blocked server_names ~($host) ~(googleusercontent|google|bing|yahoo);
    if ($invalid_referer) {
      rewrite (.*) /stop-stealing-images.png redirect;
    }
  }
  # Let the hotlink detection image be hotlinked
  # *** Find yourself a suitable graphic
  location = /stop-stealing-images.png { 
    add_header Cache-Control "public"; expires 4h;
  }

  # Don't log robots errors but log access
  location = /robots.txt {
    allow all; log_not_found off; 
  }

  location ~ /favicon.ico {
    access_log off; log_not_found off;
  }

  # This is for issuing certificates
  location /.well-known/acme-challenge/ {
    root /var/www/acme-challenge/;
  }

}

# This server simply redirects the requested to the https version of the page
server {
  listen 80;
  server_name example.com;
  return 301 https://www.example.com$request_uri;
}

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

server {
  listen 443 ssl http2;
  server_name example.com;

  ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
  ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

  # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

  access_log  /var/log/nginx/mrwild.access.log main buffer=32k flush=1m if=$log_ua;

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

관련 정보