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;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章