清漆,nginx和& node.js:具有node.js后备功能的静态nginx html页面 [英] varnish, nginx, & node.js: static nginx html pages with node.js fallback

查看:76
本文介绍了清漆,nginx和& node.js:具有node.js后备功能的静态nginx html页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在nginx前面的EC2上运行清漆,该清漆路由到node.js.

am running varnish on EC2 in front of nginx which routes to node.js.

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

What I would like is to serve specific static HTML pages from certain routes (like, / for index.html) via nginx, but have all other routes be handled by node.js.

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

As an example, / would be sent by nginx in the form of a static HTML page, while anything not matching, say /dynamic_stuff or /dynamic_stuff2, would be processed by node.js.

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

In other threads online, other people were putting node.js in a separate dir entirely, like /node/dynamic_stuff but I didn't want to have a separate dir for my routing.

现在,像其他所有东西一样,node.js为/提供了服务,但是如果我只是测试自己的node.js服务器,并且将其删除,我希望/退回到nginx版本index.html.在这种情况下,如果关闭了我的node.js服务器,那么我会得到502错误的网关.

Right now I have / served up by node.js like everything else but if I'm just testing my node.js server and I take it down, I'd like / to fallback to an nginx version of index.html. In this case, if my node.js server is taken down, then I get a 502 Bad Gateway.

我不太担心通过nginx与node.js来处理文件的性能,我只是想知道,如果node.js出于任何原因出现故障,我都希望让nginx处理基本页面.

I'm not too worried about performance from serving up files via nginx vs. node.js, I just figure that I want to have nginx handling basic pages if node.js goes down for whatever reason.

相关脚本:

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,包括/.

If I use this above code, all requests still get sent to node.js, including /.

推荐答案

我认为最简单的方法就是将index.html设置为

I think the simplest thing to do if it's just the index.html is to set index to

index index.html
root /path/to/public

现在应该从nginx提供公共目录中的所有文件.

All files in your public directory should now be served from nginx.

现在,将此index.html放在节点应用程序的公共目录中.其余的将从nginx代理到节点实例.

Now put this index.html in the public directory of your node app. The rest will be proxied from nginx to the node instance.

当然,您可以根据需要将其他静态html放在子目录中

Of course you can simply put other static html in subdirectories if you want:

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

这篇关于清漆,nginx和& node.js:具有node.js后备功能的静态nginx html页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆