回答:
Nginx wikiや他のブログでもこれを確認しました。パフォーマンスを向上させる最善の方法は、次のことです。
nginxを使用してwww.example.comからexample.comにリダイレクトするには(執筆時点ではバージョン1.0.12)。
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の一般的な落とし穴のページを参照)。
出典:
いくつか掘り下げ、いくつかのミスステップの後、これが解決策です。私が遭遇した落とし穴は、必ず「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 : //stackoverflow.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このコンテキストではa を使用しないでください。どうして?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;
}
タシット。
ところで、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
}
              
server {}構成ブロックの後に置く必要があります。