您的位置 首页 Nginx

nginx – 哪种方式更好地重定向以及为什么

我想从www.mydomain.com重定向到nginx中的domain.com.我搜索互联网并发现两种方式:

第一种方式

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

第二种方式

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

两种方式都有效.但是我应该使用哪一个?为什么?
最佳答案
第二种方式更好……

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

为什么

让我直接引用官方的Nginx维基在Pitfalls and Common Mistakes:

By using the built-in variable $request_uri,
we can effectively avoid doing any capturing or matching at all,
and by using the return directive,
we can completely avoid evaluation of regular expression.

我自己的想法……

默认情况下,正则表达式代价高昂,会降低性能.

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章