[英]Nginx/Node.JS - start Nginx only after node server is fully up
How can I configure nginx
systemd service to start only after a node.js
server is fully started. 我如何配置
nginx
systemd服务使其仅在完全启动node.js
服务器之后才能启动。
I have an ubuntu server with 1 CPU and 5 systemd services: 我有一个具有1个CPU和5个systemd服务的ubuntu服务器:
nginx_dashboard.service
dashboard_customer@.service
dashboard_customer@.service
all services run at startup and all are running docker
containers. 所有服务都在启动时运行,并且都在运行
docker
容器。 Only the nginx
container is exposed outside and it is the one that links to the 4 node
applications. 只有
nginx
容器暴露在外部,并且它是链接到4个node
应用程序的容器。
The problem is that because of the 1 CPU and all services are starting together, it takes a few seconds for the node
applications to start (10-13 seconds) while the much lighter nginx
starts immediately. 问题在于,由于1个CPU和所有服务都一起启动,因此启动
node
应用程序需要几秒钟的时间(10-13秒),而轻得多的nginx
立即启动。 I tried adding Requires
and After
to the nginx service, but it doesn't help since the node services are running, but the app itself inside the containers takes time to load. 我尝试将
Requires
和After
添加到nginx服务中,但由于节点服务正在运行,因此没有帮助,但是容器内的应用程序本身需要花费一些时间来加载。
In case the nginx
is down, my content delivery system (in this case - CloudFlare), knows to present a temp page. 万一
nginx
故障,我的内容交付系统(在本例中为CloudFlare)知道呈现临时页面。 But because of this delay, the nginx is up and I have a few seconds of downtime to my dashboard. 但是由于这种延迟,nginx已启动,并且我的仪表板有几秒钟的停机时间。
The question is if I can prevent this delay and start nginx only after the node.js application is fully up, not just the docker container. 问题是我是否可以防止这种延迟并仅在node.js应用程序完全启动后才启动nginx,而不仅仅是docker容器。
You can create a /healthz
route for checking if the app is ready. 您可以创建
/healthz
路由来检查应用程序是否准备就绪。 IN your nginx service, you can recursively check if the app is ready ( /healthz
returns http status code 200) and only start once it's ready. 在您的nginx服务中,您可以递归检查应用程序是否准备就绪(
/healthz
返回http状态代码200),只有在准备就绪后才能启动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.