【发布时间】:2021-10-28 02:22:39
【问题描述】:
我认为我的 nginx.conf 存在问题,这导致实例一次又一次地重新启动,因为我的托管容器服务的运行状况检查失败。
我在 AWS Lightsail Containers 中运行我的设置,其中运行了三个容器:
- nginx
- django
- nextjs
在我的 AWS Lightsail 实例上发布新版本时,它可以正常运行几分钟,然后遇到 503 错误,导致实例重新启动 - 运行几分钟然后再次重新启动。
查看日志我可以看到健康检查失败,django 抛出错误,说我应该将请求 IP 添加到允许的主机:
[28/Aug/2021:13:56:23] Invalid HTTP_HOST header: 'x.x.x.x'. You may need to add 'x.x.x.x' to ALLOWED_HOSTS.
[28/Aug/2021:13:56:23] Bad Request: /health.txt
问题是我的 lightsail 容器服务没有静态 IP(我也不相信我可以获得静态 IP)。
我当前的 nginx.conf 如下(感谢反馈)。 我的问题是我应该如何处理这个问题?我觉得设置ALLOWED_HOSTS = ['*'] 不是一个好方法。我可以对主机进行硬编码以进行运行状况检查或类似操作吗?
nginx.conf:
upstream backend {
server ${BACKEND_HOST}:${BACKEND_PORT};
}
upstream frontend {
server ${FRONTEND_HOST}:${FRONTEND_PORT};
}
server {
listen 80 default_server;
server_name example.com;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg+xml;
location /robots.txt {
include proxy_params;
proxy_pass http://backend;
}
location /health.txt {
include proxy_params;
proxy_pass http://backend;
}
location /api {
include proxy_params;
proxy_pass http://backend;
}
location /admin {
include proxy_params;
proxy_pass http://backend;
}
location / {
proxy_pass http://frontend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
【问题讨论】:
-
对于你的 nginx 配置,当你对待位置相同时,你可以做
location ~ ^/(first|second|third) -
谢谢@Amin,我会改的!
标签: django nginx nginx-reverse-proxy