解决方法
我还在Nginx wiki和其他博客上看过这个,并且性能明智的最佳方式是执行以下操作:
使用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语句用于表现.
它使用$request_uri而不是必须创建一个$1匹配,对重写征税(参见Nginx Common Trafalls页面).
资料来源:
> https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#server-name-if
> http://wiki.nginx.org/NginxHttpCoreModule#.24scheme