简体   繁体   中英

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.

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.

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.

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.

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.

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.

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 / .

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

All files in your public directory should now be served from 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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