Nginx no-www から www へ、www から no-www へ 質問する

Nginx no-www から www へ、www から no-www へ 質問する

使っていますチュートリアルに従って Rackspace クラウド上の nginx を実行するネットで検索してみましたが、今のところ解決できません。

SEO やその他の理由から、.htaccess で通常どおりにwww.mysite.exampleアクセスしたいと考えています。mysite.example

私の/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;
       }

私も試してみました

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

私も試してみました。2 回目の試行では両方ともリダイレクト ループ エラーが発生します。

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

私の DNS は標準として設定されています:

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

(サンプルの IP とフォルダーは、例として、また将来の人々を助けるために使用されています)。私は Ubuntu 11 を使用しています。

ベストアンサー1

HTTP ソリューション

からドキュメンテーション「正しい方法は、別のサーバーを定義することですexample.org」:

server {
    listen       80;
    server_name  example.com;
    return       301 http://www.example.com$request_uri;
}

server {
    listen       80;
    server_name  www.example.com;
    ...
}

HTTPSソリューション

https://...を含むソリューションをご希望の方へ

server {
        listen 80;
        server_name www.domain.example;
        # $scheme will get the http protocol
        # and 301 is best practice for tablet, phone, desktop and seo
        return 301 $scheme://domain.example$request_uri;
}

server {
        listen 80;
        server_name domain.example;
        # here goes the rest of your config file
        # example
        location / {

            rewrite ^/cp/login?$ /cp/login.php last;
            # etc etc...

        }
}

注: 当社ではロードバランサーを使用しており、https:// サーバーはトラフィック量の多い SSL 支払いサーバーであるため、当初はソリューションに含めませんでしたhttps://。つまり、https:// と http:// を混在させません。


Nginx のバージョンを確認するには、 を使用しますnginx -v

Nginx リダイレクトを使用して URL から www を削除する

server {
    server_name  www.domain.example;
    rewrite ^(.*) http://domain.example$1 permanent;
}

server {
    server_name  domain.example;
    #The rest of your configuration goes here#
}

したがって、2 つのサーバー コードが必要になります。

NginxリダイレクトでURLにwwwを追加する

逆に、 から にリダイレクトする必要がある場合はdomain.examplewww.domain.example次のようにします。

server {
    server_name  domain.example;
    rewrite ^(.*) http://www.domain.example$1 permanent;
}

server {
    server_name  www.domain.example;
    #The rest of your configuration goes here#
}

ご想像のとおり、これはちょうど逆で、最初の例と同じように機能します。この方法では、完全な永続的なリダイレクトと移動であるため、SEO の評価は下がりません。WWW は強制されず、ディレクトリが表示されます。

わかりやすいように、私のコードの一部を以下に示します。

server {
    server_name  www.google.com;
    rewrite ^(.*) http://google.com$1 permanent;
}
server {
       listen 80;
       server_name google.com;
       index index.php index.html;
       ####
       # now pull the site from one directory #
       root /var/www/www.google.com/web;
       # done #
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }
}

おすすめ記事