www가 아닌 ​​도메인으로 nginx를 리디렉션하는 방법은 무엇입니까? 작업을 수행하려고한다고 가정 해 보겠습니다. 나는

www.example.com에서 example.com으로 리디렉션하고 nginx를 사용 하여이 작업을 수행하려고한다고 가정 해 보겠습니다. 나는 주변을 둘러 보았고 이것에 대한 좋은 문서를 보지 못했기 때문에 내 자신의 질문에 대답하고 답할 것이라고 생각했습니다.



답변

나는 또한 Nginx 위키와 다른 블로그에서 이것을 보았으며 성능을 현명하게하는 가장 좋은 방법은 다음을 수행하는 것입니다.

nginx (작성시 버전 1.0.12)를 사용하여 www.example.com에서 example.com으로 리디렉션합니다.

server {
  server_name www.example.com;
  rewrite ^ $scheme://example.com$request_uri permanent;
  # permanent sends a 301 redirect whereas redirect sends a 302 temporary redirect
  # $scheme uses http or https accordingly
}

server {
  server_name example.com;
  # the rest of your config goes here
}

요청이 example.com으로 오면 성능에 if 문이 사용되지 않습니다. 그리고 재 작성에 세금을 부과하는 $ 1 일치를 만드는 대신 $ request_uri를 사용합니다 (Nginx Common Pitfalls 페이지 참조).

출처 :


답변

주변을 파고 실수를 한 후에 해결책이 있습니다. 내가 겪은 문제는 ” http://example.com $ uri”를 사용하는 것입니다. $ uri 앞에 /를 삽입하면 http://example.com// 으로 리디렉션됩니다 .

  server {
    listen 80;
    server_name www.example.com;
    rewrite ^ http://example.com$uri permanent;
  }

  # the server directive is nginx's virtual host directive.
  server {
    # port to listen on. Can also be set to an IP:PORT
    listen 80;

    # Set the charset
    charset utf-8;

    # Set the max size for file uploads to 10Mb
    client_max_body_size 10M;

    # sets the domain[s] that this vhost server requests for
    server_name example.com;

    # doc root
    root /var/www/example.com;

    # vhost specific access log
    access_log  /var/log/nginx_access.log  main;


    # set vary to off to avoid duplicate headers
    gzip off;
    gzip_vary off;


    # Set image format types to expire in a very long time
    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        access_log off;
        expires max;
    }

    # Set css and js to expire in a very long time
    location ~* ^.+\.(css|js)$ {
        access_log off;
        expires max;
    }

    # Catchall for everything else
    location / {
      root /var/www/example.com;
      access_log off;

      index index.html;
      expires 1d;

      if (-f $request_filename) {
        break;
      }
    }
  }

답변

SO 에서이 질문을 방문하십시오 : https : //.com/a/11733363/846634

더 나은 답변에서 :

실제로 다시 작성하지 않아도됩니다.

server {
    #listen 80 is default
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    #listen 80 is default
    server_name example.com;
    ## here goes the rest of your conf...
}

내 대답으로 점점 더 많은 표를 얻고 있지만 위도 마찬가지입니다. rewrite이 문맥에서 절대로 사용해서는 안됩니다 . 왜? nginx는 검색을 처리하고 시작해야하기 때문입니다. 당신이 사용하는 경우 return는 직접 실행을 중지 (모든의 nginx 버전에서 사용할 수 있어야합니다). 이것은 어떤 상황에서도 선호됩니다.


답변

www가 아닌 ​​사이트로 리디렉션하려면 vhost 파일을 수정하십시오.

server {
  listen 80;
  server_name www.example.com;
  rewrite ^/(.*) http://example.com/$1 permanent;
}

‘영구적’은 리디렉션을 301 리디렉션으로 바꿉니다. 이 코드 차단 후 www없이 도메인을 구성 할 수 있습니다.

www가 아닌 ​​사이트를 www로 리디렉션하는 경우 :

server {
  listen 80;
  server_name example.com;
  rewrite ^/(.*) http://www.example.com/$1 permanent;
}

Thassit.

BTW, Nginx를 사용한 전체 VPS 설정에 대해서는 내 사이트 guvnr.com에서 VPS Bible을 확인하십시오.


답변

이것이 내가 사용하는 것입니다 :

# ---------------------------------------
# vHost www.example.com
# ---------------------------------------

server {

##
# Redirect www.domain.tld
##

    server_name  www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;

}

# ---------------------------------------
# vHost example.com
# ---------------------------------------

server {

   # Something Something
}