www가 아닌 ​​www 도메인?

www가 아닌 ​​www 도메인?

사용자가 www 없이 도메인을 입력하면 www가 있는 도메인으로 리디렉션되도록 conf 파일을 변경했습니다.

서버 이름 example.com; 301 $scheme://www.example.com$request_uri를 반환합니다. 나는 또한 /user 아래에 있는 모든 것에 대해 내 https를 원합니다.

너무 많은 리디렉션 오류가 발생합니다. 어디로 잘못 가고 있나요?

그래서 나는 가지고있다:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/example.com/site;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
}
location /user {
        rewrite ^ https://$http_host$request_uri? permanent;
}
}

포트 443의 경우:

server {
listen 443;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;

root /var/www/example.com/site;
index index.html index.htm;

ssl on;
ssl_certificate //path here
ssl_certificate_key //path here

location / {
        rewrite ^ http://$http_host$request_uri? permanent;
}
location /user {
}
}

답변1

이를 수행하는 가장 좋은 방법은 및 server에 대해 별도의 블록을 갖는 것입니다 . 해야 할 항목example.comwww.example.comexample.com오직가지다:

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

에 대한 항목에는 www.example.com다른 모든 것이 포함됩니다(그리고 분명히~ 아니다리디렉션이 있습니다).

마찬가지로 https(포트 443)에 대한 두 개의 별도 서버 항목이 있습니다.

답변2

나는 다음과 같이 할 것입니다: (테스트되지 않음)

server {
listen       80;
server_name  example.com;
return       301 http://www.example.com$request_uri;
}
server {
       listen       80;
       server_name  www.example.com;
       root /var/www/example.com/site;
       index index.html index.htm;

location / {
       rewrite ^/index.php;
          }
}
server {
   listen 443;
   server_name www.example.com;
   root /var/www/example.com/site;
   index index.html index.htm;

   ssl on;
   ssl_certificate //path here
   ssl_certificate_key //path here


  location /user {
                  rewrite ^ https://$http_host$request_uri? permanent;
                 }
 }

관련 정보