Nginx程序结构及核心配置

1. 程序结构

$ sudo rpm -ql nginx
/etc/logrotate.d/nginx # 配置日志切割策略
/etc/nginx # nginx 主程序存放路径
/etc/nginx/conf.d # nginx 配置文件目录
/etc/nginx/conf.d/default.conf # nginx 默认配置文件
/etc/nginx/fastcgi_params # nginx 与 PHP 交换的内置变量
/etc/nginx/mime.types # 存放 http 响应报文中的回传的文件类型
/etc/nginx/modules # 存放 nginx 程序模块的目录
/etc/nginx/nginx.conf # nginx 主配置文件
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params # 存放 nginx 与 uwsgi 交换的内置变量
/usr/lib/.build-id
/usr/lib/.build-id/10
/usr/lib/.build-id/10/306025303ca379141ac200de5441297009c52f
/usr/lib/.build-id/4a
/usr/lib/.build-id/4a/99e5d711e64184b60a7c34a54c7b62b4a55ba1
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service # nginx 服务启停
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx # nginx 二进制文件,可执行命令,调用控制 nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.28.0
/usr/share/doc/nginx-1.28.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx # nginx 默认存放网站源码的位置
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx # 日志文件存储目录

2. 配置文件

2.1 nginx.conf 配置解析

2.1.1 nginx.conf 配置文件的结构

# 全局配置
...

# events 模块:nginx 服务本身的配置
events {
  ...
}

# http 模块:处理用户请求中的报文信息
http {
...
  # server 模块:配置一个具体的网站的响应操作
  server {
		...
    # location:匹配 URI
    location {
      ...
    }
	}
	server {
		...
	}
}

2.1.2 全局配置

user  nginx; # 定义 Nginx 运行的用户和用户组
worker_processes  auto; # Nginx进程数,建议设置为等于CPU总核心数

说明:nginx 服务的进程分为 master 进程和 worker 进程

  • master process 不负责处理客户端连接请求,负责对 worker process 的监管,而 worker process 负责处理客户端请求。
  • Nginx 支持热加载和热升级,比如更新了配置文件后执行 reload 命令,master 会开出一个新进程去读取更新过的配置文件,而 worker 进程继续保持从旧请求的连接,直到旧进程死亡,新进程会与新请求连接。master process 由 root 启动,worker process 由设置的启动用户启动,权限较小。
$ ps -ef | grep nginx
root       29533       1  0 11:05 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      29534   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29535   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29536   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29537   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29538   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29539   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29540   29533  0 11:05 ?        00:00:00 nginx: worker process
nginx      29541   29533  0 11:05 ?        00:00:00 nginx: worker process
error_log  /var/log/nginx/error.log notice; # 全局错误日志文件和日志打印级别,[ debug | info | notice | warn | error | crit ]
pid        /run/nginx.pid; # 进程 ID 文件

2.1.3 events 模块下的一些配置及其意义

# 参考事件模型:use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]
# epoll 模型是 Linux 2.6 以上版本内核中的高性能网络 I/O 模型
# 如果跑在 FreeBSD 上面,就用 kqueue 模型
use epoll;

# 单个进程最大连接数(最大连接数 = 连接数 * 进程数)
# 假设 worker_processes 为 8
# 系统可以打开的最大文件数和内存大小成正比
# 查看自己的系统可以打开的最大文件数 cat /proc/sys/fs/file-max :97318
# 并发连接总数要小于系统可以打开的文件总数,这样就在操作系统可以承受的范围之内
# 假设选择最大连接数为 80000
# 在设置了反向代理的情况下,根据经验,最大连接数应该再除以 4,就是 20000
# 所以单个进程最大连接数为 20000/8 = 2500
# 同时应该调整系统的单个进程可以打开的文件数
# 使用 ulimit -a 查看到 open file = 1024
# 应该调整系统的单个进程最大打开文件数(该数值 * 进程数<=97318)
# 调整方法:ulimit -SHn 10000 :把当前 shell 及其子进程的最大文件描述符数 软限制和硬限制都设为 10000
worker_connections  1024;

2.1.4 http 模块下的一些配置及其意义

# 引用文件中的内容,文件扩展名与文件类型映射表
include       /etc/nginx/mime.types;

# 默认文件类型,当 nginx 无法识别当前访问页面内容时,触发下载动作
default_type  application/octet-stream;

# 日志格式
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

# 日志文件位置
access_log  /var/log/nginx/access.log  main;

# 开启高效文件传输模式,sendfile 指令指定 nginx 是否调用s endfile 函数来输出文件,对于普通应用设为 on
# 如果用来进行下载等应用,磁盘 IO 重负载应用,可设置为 off,以平衡磁盘与网络 I/O 处理速度,降低系统的负载。
# 注意:如果图片显示不正常把这个改成off,原理,比如 Nginx 接受用户对某文件的请求,nginx 不能直接读取磁盘的内容,需要经过内核的调用,
# nginx 告诉内核需要读取 x 文件,内核会读取 x 文件到内核的内存中,在把这个文件 copy 到 nginx 的内存堆中,
# nginx 得知数据已经准备好,会再把这个文件发给内核,内核切片之后发送给用户。
# 当并发数过大时太耗费资源,所以这个选项的存在是为了减少文件在两个内存之间的 copy,提高效率。
sendfile        on; 

# 长连接超时时间,单位是秒(与 keeplived 软件无关),建立 TCP 连接后,65 秒没有请求动作,就断开连接
keepalive_timeout  65;

# 开启 gzip 压缩输出
#gzip  on;

# 引用其他配置文件
include /etc/nginx/conf.d/*.conf;

# 设置编码字符集
charset utf-8;

2.2 子配置文件

比如:/etc/nginx/conf.d/default.conf

2.2.1 server 模块下的一些配置及其意义

# 监听的 IP 和端口
listen       80;
# 设置服务的域名
server_name  www.bbb.com; 

# location 后配置访问的 uri
# http://www.bbb.com:80/update/id=1 这是一个 url
# 其中的 /update/id=1 是 uri
# location / 意味着反问 http://www.bbb.com:80 就会读取此处的配置
location / {
  # 网页文件存放的目录
	root   /opt;
  # 默认首页文件,顺序从小到右,如果找不到 index.html,则 index.htm 为首页
  index index.html index.htm;
  # 开启目录列表访问,适合下载服务器,默认关闭。
	autoindex on;
}

3. 编写配置文件的注意事项

  • 注意缩进与对齐
  • 每个配置必须以;结尾
  • 确保配置文件中引用的目录或者文件要存在

4. 检查配置命令

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
posted on 2025-10-26 15:13  老地瓜大数据  阅读(1)  评论(0)    收藏  举报