Nginx没有www到www和www到no-www

本文翻译自:Nginx no-www to www and www to no-www

I am using nginx on Rackspace cloud following a tutorial and having searched the net and so far can't get this sorted. 在Rackspace云上使用nginx跟随一个教程并搜索网络,到目前为止无法对此进行排序。

I want www.mysite.com to go to mysite.com as normal in .htaccess for SEO and other reasons. 我希望www.mysite.com在.htaccess中正常访问mysite.com以获取SEO和其他原因。

My /etc/nginx/sites-available/www.example.com.vhost config: 我的/etc/nginx/sites-available/www.example.com.vhost配置:

server {
       listen 80;
       server_name www.example.com example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I have also tried 我也试过了

server {
       listen 80;
       server_name example.com;
       root /var/www/www.example.com/web;

       if ($http_host != "www.example.com") {
                 rewrite ^ http://example.com$request_uri permanent;
       }

I also tried. 我也试过了。 Both the second attempts give redirect loop errors. 第二次尝试都会给出重定向循环错误。

if ($host = 'www.example.com' ) {
rewrite ^ http://example.com$uri permanent;
}

My DNS is setup as standard: 我的DNS设置为标准:

site.com 192.192.6.8 A type at 300 seconds
www.site.com 192.192.6.8 A type at 300 seconds

(example IPs and folders have been used for examples and to help people in future). (示例IP和文件夹已用于示例,并在将来帮助人们)。 I use Ubuntu 11. 我使用的是Ubuntu 11。


#1楼

参考:https://stackoom.com/question/XLNu/Nginx没有www到www和www到no-www


#2楼

Actually you don't even need a rewrite. 实际上你甚至不需要重写。

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...
}

As my answer is getting more and more up votes but the above as well. 我的答案是越来越多的投票,但上面也是如此。 You should never use a rewrite in this context. 在这种情况下,你永远不应该使用rewrite Why? 为什么? Because nginx has to process and start a search. 因为nginx必须处理并开始搜索。 If you use return (which should be available in any nginx version) it directly stops execution. 如果使用return (应该在任何nginx版本中可用),它将直接停止执行。 This is preferred in any context. 这在任何情况下都是优选的。

Redirect both, non-SSL and SSL to their non-www counterpart: 将非SSL和SSL重定向到非www对应方:

server {
    listen               80;
    listen               443 ssl;
    server_name          www.example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    return 301 $scheme://example.com$request_uri;
}

server {
    listen               80;
    listen               443 ssl;
    server_name          example.com;
    ssl_certificate      path/to/cert;
    ssl_certificate_key  path/to/key;

    # rest goes here...
}

The $scheme variable will only contain http if your server is only listening on port 80 (default) and the listen option does not contain the ssl keyword. 如果您的服务器仅侦听端口80(默认)并且listen选项不包含ssl关键字,则$scheme变量将仅包含http Not using the variable will not gain you any performance. 不使用变量不会获得任何性能。

Note that you need even more server blocks if you use HSTS, because the HSTS headers should not be sent over non-encrypted connections. 请注意,如果使用HSTS,则需要更多服务器块,因为不应通过非加密连接发送HSTS标头。 Hence, you need unencrypted server blocks with redirects and encrypted server blocks with redirects and HSTS headers. 因此,您需要具有重定向的未加密服务器块和具有重定向和HSTS标头的加密服务器块。

Redirect everything to SSL (personal config on UNIX with IPv4, IPv6, SPDY, ...): 将所有内容重定向到SSL(UNIX上的个人配置,IPv4,IPv6,SPDY,...):

#
# Redirect all www to non-www
#
server {
    server_name          www.example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80 ipv6only=on;
    listen               [::]:443 ssl spdy ipv6only=on;

    return 301 https://example.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example.com$request_uri;
}

#
# There we go!
#
server {
    server_name          example.com;
    ssl_certificate      ssl/example.com/crt;
    ssl_certificate_key  ssl/example.com/key;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    # rest goes here...
}

I guess you can imagine other compounds with this pattern now by yourself. 我猜你现在可以想象其他具有这种模式的化合物了。

More of my configs? 更多我的配置? Go here and here . 这里这里


#3楼

try this 试试这个

    if ($host !~* ^www\.){
        rewrite ^(.*)$ https://www.yoursite.com$1;
    }

Other way: Nginx no-www to www 其他方式:Nginx没有www到www

server {
  listen       80;
  server_name  yoursite.com;
  root /path/;
  index index.php;
  return       301 https://www.yoursite.com$request_uri;
}

and www to no-www 和www到没有www

server {
  listen       80;
  server_name  www.yoursite.com;
  root /path/;
  index index.php;
  return       301 https://yoursite.com$request_uri;
}

#4楼

If you are having trouble getting this working, you may need to add the IP address of your server. 如果您无法正常工作,则可能需要添加服务器的IP地址。 For example: 例如:

server {
listen XXX.XXX.XXX.XXX:80;
listen XXX.XXX.XXX.XXX:443 ssl;
ssl_certificate /var/www/example.com/web/ssl/example.com.crt;
ssl_certificate_key /var/www/example.com/web/ssl/example.com.key;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}

where XXX.XXX.XXX.XXX is the IP address (obviously). 其中XXX.XXX.XXX.XXX是IP地址(显然)。

Note: ssl crt and key location must be defined to properly redirect https requests 注意:必须定义ssl crt和密钥位置才能正确重定向https请求

Don't forget to restart nginx after making the changes: 不要忘记在进行更改后重新启动nginx:

service nginx restart

#5楼

not sure if anyone notice it may be correct to return a 301 but browsers choke on it to doing 不确定是否有人注意到返回301可能是正确的,但浏览器会阻止它做

rewrite ^(.*)$ https://yoursite.com$1; 

is faster than: 比以下更快:

return 301 $scheme://yoursite.com$request_uri;

#6楼

Ghost blog 鬼博客

in order to make nginx recommended method with return 301 $scheme://example.com$request_uri; 为了使nginx推荐方法return 301 $scheme://example.com$request_uri; work with Ghost you will need to add in your main server block: 使用Ghost,您需要在主服务器块中添加:

proxy_set_header    X-Real-IP           $remote_addr;
proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
proxy_set_header    Host                $http_host;
proxy_set_header    X-Forwarded-Proto   $scheme;
proxy_set_header    X-NginX-Proxy       true;

proxy_pass_header   X-CSRF-TOKEN;
proxy_buffering     off;
proxy_redirect      off;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章