我想从www.mydomain.com重定向到Nginx中的domain.com.我搜索互联网并发现两种方式:
第一种方式
@H_403_8@server { listen 80; server_name www.domain.com; rewrite ^/(.*) http://domain.com/$1 permanent; }
第二种方式
@H_403_8@server { listen 80; server_name www.domain.com; return 301 $scheme://domain.com$request_uri; }
两种方式都有效.但是我应该使用哪一个?为什么?
最佳答案
第二种方式更好……
@H_403_8@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.
我自己的想法……
默认情况下,正则表达式代价高昂,会降低性能.