【问题标题】:varnish, nginx, & node.js: static nginx html pages with node.js fallbackvarnish、nginx 和 node.js:带有 node.js 后备的静态 nginx html 页面
【发布时间】:2013-02-28 18:16:56
【问题描述】:

我在 nginx 前面的 EC2 上运行 varnish,它路由到 node.js。

我想要的是通过 nginx 从某些路由(例如,/ for index.html)提供特定的静态 HTML 页面,但所有其他路由都由 node.js 处理。

例如,/ 将由 nginx 以静态 HTML 页面的形式发送,而任何不匹配的内容,例如 /dynamic_stuff/dynamic_stuff2,将由 node.js 处理。

在其他在线线程中,其他人将 node.js 完全放在单独的目录中,例如 /node/dynamic_stuff,但我不想为我的路由设置单独的目录。

现在我有 / 像其他所有东西一样由 node.js 提供服务,但如果我只是测试我的 node.js 服务器并将其关闭,我希望 / 回退到 nginx 版本的index.html。在这种情况下,如果我的 node.js 服务器被关闭,那么我会得到一个 502 Bad Gateway。

我不太担心通过 nginx 与 node.js 提供文件的性能,我只是想,如果 node.js 出于某种原因出现故障,我想让 nginx 处理基本页面。

相关脚本:

location = / {
    index index.html
    root /path/to/public
    try_files $uri $uri/ index.html;
}

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass node_js;
}

如果我使用上面的代码,所有请求仍然会发送到 node.js,包括/

【问题讨论】:

    标签: node.js nginx varnish


    【解决方案1】:

    如果只是 index.html,我认为最简单的方法是将 index 设置为

    index index.html
    root /path/to/public
    

    您的公共目录中的所有文件现在都应该由 nginx 提供。

    现在把这个 index.html 放到你的节点应用的公共目录中。其余的将从 nginx 代理到节点实例。

    当然,如果你愿意,你可以简单地将其他静态 html 放在子目录中:

    public/about(index.html
    public/faq/index.html
    ...
    

    【讨论】:

    • 如果我这样做,它仍然会将 / 转发到 node.js。这是我的 conf 中的一个相关部分: location = / { index index.html root /path/to/public try_files $uri $uri/ index.html; } 位置 / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header 主机 $http_host; proxy_set_header X-NginX-Proxy 真; proxy_pass node_js; }
    • 这有点难读。也许您可以将您的配置作为代码 sn-p 传递到您的问题中。
    • 您能否尝试将“根”和“索引”指令移出位置块,我通常没有将它们放在位置中。它也被标记为一个常见的陷阱:wiki.nginx.org/Pitfalls
    • 我试过了——没用。我最终只使用快递来提供页面。不过感谢您的帮助!
    猜你喜欢
    • 2016-04-26
    • 2017-07-27
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2012-11-05
    • 2015-08-19
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多