nginx配置文件结构详解之include

版本详情


root@ubuntu:/home/user# dpkg -s nginx
Package: nginx
Status: install ok installed
Priority: optional
Section: httpd
Installed-Size: 49
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 1.18.0-6ubuntu14.7
Depends: nginx-core (<< 1.18.0-6ubuntu14.7.1~) | nginx-full (<< 1.18.0-6ubuntu14.7.1~) | nginx-light (<< 1.18.0-6ubuntu14.7.1~) | nginx-extras (<< 1.18.0-6ubuntu14.7.1~), nginx-core (>= 1.18.0-6ubuntu14.7) | nginx-full (>= 1.18.0-6ubuntu14.7) | nginx-light (>= 1.18.0-6ubuntu14.7) | nginx-extras (>= 1.18.0-6ubuntu14.7)
Breaks: libnginx-mod-http-lua (<< 1.18.0-6ubuntu5)
Description: small, powerful, scalable web/proxy server
 Nginx ("engine X") is a high-performance web and reverse proxy server
 created by Igor Sysoev. It can be used both as a standalone web server
 and as a proxy to reduce the load on back-end HTTP or mail servers.
 .
 This is a dependency package to install either nginx-core (by default),
 nginx-full, nginx-light or nginx-extras.
Homepage: https://nginx.net
Original-Maintainer: Debian Nginx Maintainers <pkg-nginx-maintainers@alioth-lists.debian.net>

基本结构

nginx配置文件在常见linux系统中通过包管理器安装的默认路径都是/etc/nginx/nginx.conf,这个位置实际上是linux系统的文件规范。

user www-data;   # 表示nginx启动的用户,该用户一般是web服务器通用的,好处是权限低,即使被入侵也无伤大雅
include /etc/nginx/modules-enabled/*.conf;

http {
  include /etc/nginx/mime.types;
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;


   server {
      listen 80;
       location  / {
          }
   }
   server {
      listen 443;
       location  / {
          }
   }

}

文件结构大致就是上述,此外,这里着重说明include,在新手看来nginx.conf中似乎引入了不同的配置文件,他们拥有各种各样的后缀,但从实际作用上,他们并没有区别!。
include 会把目标文件的内容在解析时“原地展开”,真正起作用的只有通配符和配置文件内容是否合规。

但这样做并不是没有道理,如上述配置中nginx默认引入的规则/etc/nginx/sites-enabled/*;j就是默认指不同的站点,也就是server模块,他的作用显示是为了解耦nginx.conf文件

拓展

在做前面的博文时,顺便阅读了nginx官方文档,发觉nginx 有一个主进程和多个工作进程,以及一些nginx命令

主进程和工作进程

官文翻译
nginx有一个master进程和多个worker进程。master进程的主要目的是读取和评估配置,并维护worker进程。worker进程处理实际的请求。nginx采用事件驱动模型和与操作系统相关的机制,在worker进程之间高效地分配请求。worker进程的数量在配置文件中定义,对于给定的配置可能固定,也可以根据可用的CPU核心数量自动调整(参见worker_processes)。

可以通过指令ps aux | grep nginx 查看相应进程

nginx命令

检查配置文件: nginx -t

向主进程发送信号

nginx -s [信号]

其中信号可以是以下之一:

stop — 快速关闭
quit — 优雅关闭
reload — 重新加载配置文件
reopen — 重新打开日志文件

在man nginx中我找到了更相近的信号说明,但他们只对应nginx -s的一部分,另一部分属于进程级别的,需要手动发送

信号
nginx 的主进程可以处理以下信号:

SIGINT、SIGTERM  快速关闭。
SIGHUP           重新加载配置,用新配置启动新的工作进程,并优雅地关闭旧的工作进程。
SIGQUIT          优雅关闭。
SIGUSR1          重新打开日志文件。
SIGUSR2          动态升级 nginx 可执行文件。
SIGWINCH         优雅关闭工作进程。

虽然通常不需要显式控制工作进程,但它们也支持一些信号:

SIGTERM          快速关闭。
SIGQUIT          优雅关闭。
SIGUSR1          重新打开日志文件
posted @ 2025-09-23 21:49  wenzhuo4657  阅读(2)  评论(0)    收藏  举报