Nginx 重写基本 URL 的路径

Cha*_*les 5 php redirect nginx

我正在尝试配置 Nginx,以便将所有请求http://domain.com/path重写为http://domain.com/.

我不想重定向,因为我希望 URL 仍然显示原始路径。

示例重写:

http://domain.com/path/index.php         -> http://domain.com/index.php
http://domain.com/path/category/books    -> http://domain.com/category/books
http://domain.com/path/books.php?q=harry -> http://domain.com/books.php?q=harry
Run Code Online (Sandbox Code Playgroud)

我试过了aliasroot但我无法让它们工作。

location /path
{
    root /var/www/html/;
}

location /path
{
    alias /var/www/html/;
}
Run Code Online (Sandbox Code Playgroud)

Arm*_*ani 2

root旨在alias提供来自特定目录的文件,而不是重写 URL。你应该使用rewrite.

server {
    rewrite ^/path(/.*)$ $1 last;

    # Your location blocks go here.
}
Run Code Online (Sandbox Code Playgroud)

阅读官方文档以获取更多信息。