加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:25:02 所属栏目: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

我想从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.

我自己的想法……

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读