简体   繁体   English

清漆,nginx和node.js:具有node.js后备功能的静态nginx html页面

[英]varnish, nginx, & node.js: static nginx html pages with node.js fallback

am running varnish on EC2 in front of nginx which routes to node.js. 我在Nginx前面的EC2上运行清漆,该清漆路由到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从某些路由(例如/ for index.html )提供特定的静态HTML页面,但所有其他路由都由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. 例如, /将由nginx以静态HTML页面的形式发送,而任何不匹配的内容(例如/dynamic_stuff/dynamic_stuff2 )将由node.js处理。

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/dynamic_stuff但我不想为路由选择一个单独的目录。

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 . 现在我有/担任了由node.js的其他事物一样,但如果我只是在测试我的node.js服务器,我把它记下来,我想/退却到的nginx的版本index.html In this case, if my node.js server is taken down, then I get a 502 Bad Gateway. 在这种情况下,如果关闭了我的node.js服务器,那么我会得到502 Bad Gateway。

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. 我不太担心通过nginx vs.node.js来处理文件的性能,我只是想,如果node.js出于某种原因出现故障,我想让nginx处理基本页面。

Relevant script: 相关脚本:

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;
}

If I use this above code, all requests still get sent to node.js, including / . 如果我使用上面的代码,所有请求仍将发送到node.js,包括/

I think the simplest thing to do if it's just the index.html is to set index to 我认为如果只是index.html,最简单的方法是将index设置为

index index.html
root /path/to/public

All files in your public directory should now be served from nginx. 现在,应该从nginx提供公共目录中的所有文件。

Now put this index.html in the public directory of your node app. 现在,将此index.html放在节点应用程序的公共目录中。 The rest will be proxied from nginx to the node instance. 其余的将从nginx代理到该节点实例。

Of course you can simply put other static html in subdirectories if you want: 当然,您可以根据需要将其他静态html放在子目录中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM