How to read Nginx config files automatically?

Have anyone familiar with Nginx? I got a difficult task about how to get some key information from hundred of Nginx config files. I'm wondering if any tools can read those configs automatically. Otherwise, I need to manually open those files one by one. It made me crazy.

您可以使用多种工具自动读取 Nginx 配置文件。一种选择是使用nginx -T命令,它可用于检查 Nginx 服务器的语法和配置。此命令将输出服务器的完整配置,包括所有包含的文件及其位置。

您还可以使用该nginx-conf-parser工具,它是一个 Python 库,可用于解析 Nginx 配置文件。使用此工具,您可以从配置文件中提取特定信息,例如服务器名称、侦听端口和位置块。

以下是如何使用nginx-conf-parserNginx 配置文件提取信息的示例:

import nginx_conf_parser

# Parse the config file
config = nginx_conf_parser.parse_nginx_conf('/path/to/nginx.conf')

# Extract the server name
server_name = config['server_name']

# Extract the listen ports
listen_ports = config['listen_ports']

# Extract the location blocks
location_blocks = config['location_blocks']