0

我有我在 NGINX 中使用的 python 烧瓶应用程序,并且我已经对这个图像进行了 dockerised。默认情况下,flask 应用程序运行在 5000 端口,NGINX 运行在 80 端口。如果我在容器中运行图像,所有服务都可以正常工作。我能够从内部映射到烧瓶 5000 端口的 NGINX 端口 80 访问服务。现在我想为这张图片添加健康检查。所以我在这样的烧瓶应用程序中使用py-healthcheck模块。

health = HealthCheck()
def redis_available():
    return True, "UP"

health.add_check(redis_available)

app.add_url_rule("/health", "healthcheck", view_func=lambda: health.run())

现在如果我使用 URL 只运行烧瓶应用程序(在我的本地系统中没有 NGINX)

http://localhost:5000/health

我得到了正确的回应,说应用程序已启动。为了添加图像的健康检查,我在 Dockerfile 中添加了这个命令

HEALTHCHECK --interval=30s --timeout=120s --retries=3  CMD wget --no-check-certificate --quiet --tries=1 --spider https://localhost:80/health || exit 1

在这里,我假设我正在尝试从 NGINX 访问健康检查端点,这就是我使用localhost:80的原因。但是,如果我运行容器,容器总是不健康,但所有端点都工作正常。我是否必须在NGINX conf 文件中进行一些配置才能从NGINX访问烧瓶的健康检查端点?

这是 nginx 配置:

# based on default config of nginx 1.12.1
# Define the user that will own and run the Nginx server
user nginx;
# Define the number of worker processes; recommended value is the number of
# cores that are being used by your server
# auto will default to number of vcpus/cores
worker_processes auto;

# altering default pid file location
pid /tmp/nginx.pid;

# turn off daemon mode to be watched by supervisord
daemon off;

# Enables the use of JIT for regular expressions to speed-up their processing.
pcre_jit on;

# events block defines the parameters that affect connection processing.
events {
    # Define the maximum number of simultaneous connections that can be opened by a worker process
    worker_connections  1024;
}


# http block defines the parameters for how NGINX should handle HTTP web traffic
http {
    # Include the file defining the list of file types that are supported by NGINX
    include /opt/conda/envs/analytics_service/etc/nginx/mime.types;
    # Define the default file type that is returned to the user
    default_type text/html;

    # Don't tell nginx version to clients.
    server_tokens off;

    # Specifies the maximum accepted body size of a client request, as
    # indicated by the request header Content-Length. If the stated content
    # length is greater than this size, then the client receives the HTTP
    # error code 413. Set to 0 to disable.
    client_max_body_size 0;

    # Define the format of log messages.
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';

    # Define the location of the log of access attempts to NGINX
    access_log /opt/conda/envs/analytics_service/etc/nginx/access.log  main;

    # Define the location on the file system of the error log, plus the minimum
    # severity to log messages for
    error_log /opt/conda/envs/analytics_service/etc/nginx/error.log warn;

    # Define the parameters to optimize the delivery of static content
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;

    # Define the timeout value for keep-alive connections with the client
    keepalive_timeout  65;

    # Define the usage of the gzip compression algorithm to reduce the amount of data to transmit
    #gzip  on;

    # Include additional parameters for virtual host(s)/server(s)
    include /opt/conda/envs/analytics_service/etc/nginx/conf.d/*.conf;
}
4

0 回答 0