Nginx 多根

Mic*_*lio 13 rewrite http nginx redirect rules

我想将请求转移到特定的子目录,到另一个根位置。如何?我现有的块是:

server {
    listen       80;
    server_name  www.domain.com;

    location / {
        root   /home/me/Documents/site1;
        index  index.html;
    }

    location /petproject {
        root   /home/me/pet-Project/website;
        index  index.html;
        rewrite ^/petproject(.*)$ /$1;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    } }
Run Code Online (Sandbox Code Playgroud)

也就是说,http://www.domain.com应该服务于 /home/me/Documents/site1/index.html 而http://www.domain.com/petproject应该服务于 /home/me/pet-Project/website /index.html - 似乎 nginx 在替换后重新运行所有规则,而http://www.domain.com/petproject只是提供 /home/me/Documents/site1/index.html 。

Ter*_*nen 29

配置有通常发生在 nginx 的常见问题。也就是说,rootlocation块内使用指令。

尝试使用此配置而不是您当前的location块:

root /home/me/Documents/site1;
index index.html;

location /petproject {
    alias /home/me/pet-Project/website;
}
Run Code Online (Sandbox Code Playgroud)

这意味着您网站的默认目录是/home/me/Documents/site1,对于/petprojectURI,内容是从/home/me/pet-Project/website目录提供的。

  • 通常的问题是人们没有意识到“root”和“location”块如何交互。使用“root”指令,nginx 将“location”块后面的路径附加到“root”目录,以获取文件的完整文件系统路径。因此,如果“location”指定的路径在“root”指定的目录中不存在,则会发生用户意想不到的情况。 (4认同)
  • 您提到的常见问题是什么以及为什么别名更好?nginx 文档给出了一个使用两个根的示例 (2认同)