
사용자가 브라우저에 example.com을 입력하면 www.example.com으로 리디렉션하려면 Nginx 설정이 필요합니다. 그 이유는 SEO 컨설턴트가 선호하는 도메인이 하나만 있어야 한다고 말했기 때문입니다. 그렇지 않으면 Google에서는 이를 콘텐츠 중복으로 간주합니다. 그래도 . . .
요점은 Letsencrypt의 SSL도 서버에 설정했지만 example.com에서 www.example.com으로 리디렉션할 수 없다는 것입니다(서버는 두 버전을 모두 허용합니다). 내가 사용하는 구성은 다음과 같습니다.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com www.example.com;
root /home/my_site;
index index.php index.html index.htm;
# for letsencrypt
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
==== 업데이트 ====
이제 다음 답변 중 하나에서 Tim의 조언에 따라(항상 nginx -t
및 ) 구성을 변경했습니다.restart
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /home/ankush/wp_ankushthakur;
index index.php index.html index.htm;
# for letsencrypt
location ~ /.well-known {
allow all;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
다음은 모든 변형에 대한 출력 curl -k
및 액세스 로그입니다(저는 더 간단한 솔루션을 원하고 서버를 엉망으로 만들고 싶지 않기 때문에 소스에서 Nginx를 빌드하지 않았습니다).
curl -k http://example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k http://www.example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k https://example.com
Curl output: 301 - Moved permanently
Access logs: "GET / HTTP/1.1" 301 194 "-" "curl/7.47.0"
curl -k https://www.example.com
Curl output: <Blank>
Access logs: "GET / HTTP/1.1" 301 5 "-" "curl/7.47.0"
CURL 출력이 비어 있고 액세스 로그가 여전히 영구 리디렉션을 제공하는 마지막 섹션을 확인하세요.
재미있게도 두 번째 블록을 주석 처리한 server
다음 Nginx를 다시 시작하면 내가 원했던 것과 반대되는 결과가 발생합니다. 즉, www가 www가 아닌 곳으로 리디렉션됩니다! www.example.com의 HTTPS 버전이 이 (세 번째) 버전의 구성 어디에도 언급되어 있지 않기 때문에 그런 일이 일어나고 있다는 사실에 놀랐습니다.
답변1
아마도 HTTP에서만 리디렉션하고 HTTPS에서는 리디렉션하지 않기 때문일 수 있습니다. 리디렉션 가상 호스트에 HTTPS를 추가하고 example.com
이름만 남겨 두어야 합니다.
더욱이, 당신이 하고 있는 일은 오늘날 사람들이 실제로 하는 일과 정반대입니다. 일반적인 접근 방식은 유산을 묻어 두는 것입니다.www이전 시대의 접두사를 사용하고 간단한 도메인 이름만 사용합니다.www.
답변2
여기서 핵심은 4개의 URL(www 도메인과 www가 아닌 도메인의 http 및 https 버전)을 처리해야 한다는 것입니다. 문제는 www 도메인과 www가 아닌 도메인의 http 버전을https://www도메인이지만 기본 서버 블록이 두 도메인을 모두 수신하고 있습니다.https://example.com그리고https://www.example.com
당신이 해야 할 일은 데이터를 전달할 별도의 서버 블록을 만드는 것뿐입니다.https://example.com~로https://www.example.com섬기는 사람. 여기에 https 설정을 포함해야 합니다.
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com;
# Main server block for main server continues
}
표준 예
나는 가지고있다튜토리얼다운로드 가능한 구성 파일이 있습니다. 표준 예는 다음과 같습니다.
이것이 작동하지 않는 경우 -k 옵션을 사용하여 각 변형(http 및 https, www 및 비www)을 컬링하여 헤더를 표시하고 이를 포함하도록 질문을 편집합니다.
# Main server
server {
server_name www.example.com;
listen 443 ssl http2;
# etc, add all locations, call PHP or servers, etc
}
# Forward http requests to https www server
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
# Forward https non-www requests to the https www server
# Requires https setup for this server
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;
return 301 https://www.example.com$request_uri;
}
문제 해결 문제를 진단하는 가장 좋은 방법은 다음과 같습니다.
- 액세스 로그와 함께 각 도메인 변형에 "curl -k"(헤더 표시)를 사용하세요. 반환된 http 상태 코드는 무슨 일이 일어나고 있는지 알려줍니다. 200은 페이지, 301은 영구 리디렉션, 302는 임시 리디렉션입니다.
- Nginx에 headers_more 모듈이 있는지 확인하세요.소스에서 Nginx 빌드, 아주 쉽습니다. 이를 통해 응답에 https 헤더를 추가할 수 있습니다. 이것은 훌륭한 진단 도구입니다. 이와 같은 명령문을 사용하여 어떤 블록이 실행되고 있는지 알아낼 수 있습니다.
add_header Z_DEBUG "location_name_or_message";
답변3
나는 마침내 SEO 담당자가 www가 아닌 도메인을 기본 도메인으로 간주하도록 설득할 수 있었습니다. www가 아닌 www로 리디렉션하는 데 작동하는 구성은 다음과 같습니다. 그 반대를 달성하려는 나의 시도는 비슷한 구성을 가지고 있었지만 무엇이 그것을 막고 있는지 잘 모르겠습니다.
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
server_name example.com;
root /home/mysite;
index index.php;
location ~ /.well-known {
allow all;
}
location / {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri $uri/ /index.php?$query_string;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}