【问题标题】:What does root directive in nginx refer to when it is assigned a static folder?nginx 中的 root 指令在分配静态文件夹时指的是什么?
【发布时间】:2015-04-07 18:29:24
【问题描述】:

我有一个 nodejs 应用程序与 nginx 一起提供静态文件。在阅读 nginx 的 sites_available 时,我看到 root 被分配了一个静态文件服务文件夹(确切地说是公共文件夹)。这是什么意思? 我的配置看起来像

server {
  server_name mysite.mydomain.com;
  root /var/www/mysite/public/;
  index index.html index.htm;

  access_log /var/log/nginx/mysite.access.log;
  error_log  /var/log/nginx/mysite.error.log;
  underscores_in_headers on;

  recursive_error_pages on;
  error_page 503 @maintenance;

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  location @maintenance {
    error_page 405 = /system/maintenance.html;
    rewrite  ^(.*)$  /system/maintenance.html break;
  }

  location ~ ^/(js|css|images|media|system)/ {
    autoindex off;
    add_header Cache-Control public;
    expires 4w;
  }

  location / {
    try_files $uri $uri/index.html @proxy;
  }

  location @proxy {
    proxy_set_header Host $http_host;
    proxy_pass   http://127.0.0.1:7485;
  }
}

谁能给我解释一下?我搜索了 nginx 文档,但没有找到任何问题的答案。

【问题讨论】:

    标签: node.js ubuntu nginx


    【解决方案1】:

    root 指令设置请求的基本路径。也就是说,它用于将http请求路径转换为文件系统路径。

    例如,在server 指令下将root 设置为/var/www/,如果请求文件http://www.example.com/image.jpg,则其路径将转换为/var/www/image.jpg

    更多详情可以在这里找到:http://nginx.org/en/docs/http/ngx_http_core_module.html#root

    【讨论】:

    • 是的,那是我的问题。如果 root 设置为公用文件夹,nodejs 服务器要处理的其他路由不会被错误路由吗?我也没有找到任何关于 @proxy 的信息,你能帮我解决这个问题吗?
    • @suman 你到底想达到什么目的,请解释一下。
    • 我正在使用 nginx 来提供我所有的静态文件,并使用我的节点服务器来提供剩余的文件。我只是想了解配置文件中实际发生的情况
    • @suman 一般来说,NodeJS 自己处理静态文件,而 nginx 只是用于反向代理,不需要单独设置。有很多 static 中间件可以做到这一点。我认为您正在尝试做的事情也是可能的。
    猜你喜欢
    • 2021-12-04
    • 2022-01-14
    • 1970-01-01
    • 2014-06-15
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2015-09-04
    相关资源
    最近更新 更多