计算机网络/计算机科学与应用/系统/运维/开发

Nginx 配置参数详解

一、配置文件

# 全局指令开始

# 定义Ngixn运行的用户和用户组
#user  nobody;

# nginx进程数,建议设置为等于cpu总核心数
worker_processes  1;

# 全局错误日志定义类型 [ debug | info | notice | warn | error | crit ]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 进程pid文件
#pid        logs/nginx.pid;
# 全局指令结束

# 局部指令开始
# 指定进程可以打开的最大描述符:数目
# 工作模式与连接数上限
events {
# 参考事件模型, use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
# 是linux2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果泡在FreeBSD上面,就用kqueue模型
# 补充说明
# 与apache相关,nginx针对不同的操作系统,有不同的事件模型
# A) 标准事件模型
# select、poll属于标准事件模型,如果当前系统不存在更有效方法nginx会选择select或poll
use epoll;
# 单个进程最大连接数 (最大连接数=连接数* 进程数)
# 根据硬件调节 和前面工作进程配合起来用,尽量大 但是别把cpu跑到100%即可
    worker_connections  1024;
    # keepalive超时时间
    keepalive_timeout   60;
    # gzip模块设置
    gzip on;  # 开启gzip压缩输出
    gzip_min_length 1k;  # 最小压缩文件大小
    gzip_buffers 4 16k;  # 压缩缓冲区
    gzip_http_version 1.0; # 压缩版本(默认1.1,前端如果是squid2.5 请使用1.0)
    gzip_comp_level 2; # 压缩等级
    gzip_types text/plain application/x-javascript text/css application/xml; # 压缩类型 
    gzip_vary on;
}
# 局部指令结束

# nginx 最核心的配置开始
http {
    include     mime.types;
    default_type application/octet-stream;
    # 虚拟主机配置开始
    server {
        # 监听端口
        listen 80;
        # 域名可以有多个,用空格隔开
        server_name localhost;
        location / {
                root html;
                index index.html index.htm;
            }
         }
}
# nginx最核心的配置结束


二、Nginx配置文件语法

1、 由指令和指令块组成

2、 每行语句都得有分号结束,指令和参数之间是空格分隔的

3、 指令块可以由大括号{}组织多条语句

4、 # 表示注释作用

5、 支持用$变量名支持该语法

6、 nginx支持include语句 组合多个配置文件

7、 nginx部分指令支持正则


nginx.conf 重要的指令块

核心功能在http{}指令块,http{}块还包含了以下指令

server{} 指令块 对应一个站点配置,反向代理,静态资源站点

location{} 对应一个url

upstream{} 定义上游服务,负载均衡池


三、Nginx命令行

1、nginx启停指令

nginx  # 初次启动,直接输入nginx 如启动后再执行该命令行,就会提示端口冲突(停止nginx,再启动)
nginx -s  stop  # 停止nginx服务
nginx -s reload # 平滑重启,利用reload可以不重启nginx进程,重新读取配置文件


2、 查看nginx的帮助信息

[root@VM-4-2-centos logs]# nginx -h
nginx version: nginx/1.21.6
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]
Options:
  -?,-h         : this help  # nginx的帮助信息
  -v            : show version and exit   # 列出nginx的版本号
  -V            : show version and configure options then exit  列出nginx版本和编译信息
  -t            : test configuration and exit   # 测试nginx配置文件语法是否正确
  -T            : test configuration, dump it and exit    # 检查配置,输出配置信息
  -q            : suppress non-error messages during configuration testing  # 在检测配置文件期间屏蔽非错误信息
  -s signal     : send signal to a master process: stop, quit, reopen, reload  # 给nginx主进程发送一个主信号 分别stop停止运行 quit 优雅停止 reload 读取配置文件
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -e filename   : set error log file (default: logs/error.log)
  -c filename   : set configuration file (default: conf/nginx.conf)  # 指定配置文件
  -g directives : set global directives out of configuration file # 覆盖设置一些默认参数


无才无以立足,不苦不能成才。

评论

^