Nginx: ベースドメインからサブディレクトリへの書き換え

Nginx: ベースドメインからサブディレクトリへの書き換え

私はブログを運営していますhttp://site.com/blog

site.comnginxにからのリクエストを書き換えるように指示するにはどうすればよいですかsite.com/blog?

これは永続的なものではありません。

答え1

location = / {
    rewrite ^ http://site.com/blog/ redirect;
}

これはルート専用のリクエストのみを実行します。すべてをキャッチする必要がある場合 (http://site.com/somearticle/something.htmlにリダイレクトする場合http://site.com/blog/somearticle/something.html)、さらに複雑なものが必要になります。

location /blog/ {
    # Empty; this is just here to avoid redirecting for this location,
    # though you might already have some config in a block like this.
}
location / {
    rewrite ^/(.*)$ http://site.com/blog/$1 redirect;
}

答え2

代わりにこれを試してください:

location = / {
      return 301 /blog/;
 }

ここで重要なのは'='シンボル。

答え3

これは私にはうまくいきませんでした。うまくいったのは次の方法です:

  1. サイトの NGINX 構成ファイルを開きます。サーバー ブロック内に、ルート ディレクトリへのパスを追加し、ファイルの優先順位を設定します。

    root /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    
  2. 空の場所ブロックを作成する前にその他のすべてのロケーションブロック:

    location /latest {
    # Nothing in here; this is to avoid redirecting for this location
    }
    
  3. location / {} ブロック内のルート ディレクトリ ディレクティブをコメント アウトし、次のようにリダイレクトを追加します。

    location / {
    # root   /mnt/www/www.domainname.com;
    index  index.php index.html index.htm;
    rewrite ^/(.*)$ http://www.domainname.com/latest/$1 redirect;
    }
    
  4. location ~ .php$ブロックのルートが以下を指していることを確認してください。

    root /mnt/www/www.domainname.com;
    

これで問題は解決しました。

答え4

ホームページのみで他に何もない場合は、次を使用します。

location / {           
    rewrite ^/$ /blog/ redirect;
}

/foo/ など、それ以外のものは /blog/ にリダイレクトされません。

関連情報