参考资料

  1. Nginx GitLab(配置归档工具)配置详细说明以及案例
  2. Nginx代理缓存配置详细说明以及案例
  3. Nginx文件下载服务器搭建详细说明以及案例
  4. Nginxlocalhost 路由匹配规则详细说明以及案例
  5. nginx -e详细说明以及案例
  6. Nginxrewrite重定向配置详解
  7. 如何防止勒索病毒攻击?
  8. Nginx端口监听(listen指令)详细说明以及案例

root指令用于指定Nginx服务器上文件的根目录。当Nginx处理请求时,会根据root指令指定的路径来查找请求的文件。

语法

root path;
  • path:指定文件系统的绝对路径或相对路径。

示例

假设Nginx配置文件位于/etc/nginx/nginx.conf,并且网站文件存放在/var/www/html目录下。

1. 基本配置

server {
    listen 80;
    server_name example.com;

    root /var/www/html;

    location / {
        index index.html;
    }
}
  • 当访问http://example.com/index.html时,Nginx会在/var/www/html目录下查找index.html文件。

2. 多个location

server {
    listen 80;
    server_name example.com;

    root /var/www/html;

    location / {
        index index.html;
    }

    location /images/ {
        root /var/www/images;
    }
}
  • 当访问http://example.com/images/logo.png时,Nginx会在/var/www/images/images/logo.png路径下查找文件。

3. 使用相对路径

server {
    listen 80;
    server_name example.com;

    root html;

    location / {
        index index.html;
    }
}
  • 如果Nginx的配置文件位于/etc/nginx/nginx.conf,则html路径会被解析为/etc/nginx/html

注意事项

  • root指令指定的路径是相对于Nginx配置文件的路径,除非使用绝对路径。

  • 如果root指令在location块中定义,则它会覆盖server块中的root指令。

  • 确保Nginx进程对root指定的目录有读取权限。

声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。