Nginx - 进程及配置文件

进程

Nginx 服务启动后,会有一个主进程(master process),一个或多个工作进程(worker processes)。如果开启了 缓存,缓存加载和缓存管理进程也会在 Nginx 服务启动时运行。
主进程用于读取并评估配置文件,维护工作进程的运行。
工作进程用于处理请求。工作进程的数量在 nginx.conf 配置文件中指定,可以设置为固定值或根据可用的 CPU 核心数量来动态调整(参考 worker_processes)。

配置文件

参考 Nginx 官网

配置文件改完后,需要重启 Nginx 进程或发送 reload 信号给 Nginx 进程。

配置文件中,所有注释行用井号开头 #

Nginx 的配置文件名称通常是 nginx.conf,位置是 /etc/nginx/usr/local/nginx/conf

简单指令和块指令

配置文件由指令组成。指令分为简单指令和块指令(block directives)。简单指令由名称和参数组成,以空格分隔,并以分号 ; 结束。块指令不以分号结尾,而是用一系列由大括号 {} 包围的附加指令来结束。如果块指令的大括号内可以有其他指令,这个块指令就被称为一个上下文(例如: eventshttpserverlocation)。

  • 简单指令:
user             nobody;
error_log        logs/error.log notice;
worker_processes 1;
  • 块指令:
location / {
    root /data/www;
}

配置文件中,如果指令在文件的任何上下文之外,则被认为是在主上下文 main context 中。events 和 http 指令在主上下文 main context 中,而 server 指令在 http 中,location 指令在 server 中。

配置文件的可维护性

为了让配置文件容易维护,可以根据功能将配置文件分割为不同的文件,放在 /etc/nginx/conf.d 目录中,然后在主配置文件 nginx.conf 中通过 include 指令引入这些文件。

include conf.d/http;
include conf.d/stream;
include conf.d/exchange-enhanced;

顶级指令(区分流量类型)

配置文件中有 4 个用于区分流量类型的顶级指令,不能写在其他上下文中:

  • events – 处理一般连接
  • http – 处理 HTTP 流量
  • mail – 处理 Mail 流量
  • stream – 处理 TCP/UDP 流量

在这几个区分流量类型的指令中,可以包含一个或多个 server 上下文来定义虚拟服务器。在 server 上下文中可以根据不同的流量类型使用不同的指令。

对于 http 上下文中的 HTTP 流量,其中的每个 server 上下文中的指令可以处理指定域名或 IP 地址的请求。server 上下文中可以定义一个或多个 location 上下文,根据不同的 URI 来做不同的处理。

对于 mailstream 上下文中的 mail 和 TCP 流量,其中的每个 server 上下文中的指令可以处理指定 TCP 端口或 UNIX socket 套接字的请求。

使用上下文的示例:

user nobody; # 主上下文中的指令

events {
    # 关于 connection processing 的配置
}

http {
    # 配置针对 HTTP 流量的一个或多个 server

    listen   80;  # 监听 80 端口
    server_name  xx.com;  # 处理通过 xx.com 这个域名访问的请求
    root   "/home/workspace/xx";  # 处理请求对应的项目的位置

    server {
        # 配置 server
        location /one {
            # 配置 URI 中包含 '/one' 的处理方式
        }
    }

    server {
        # 配置 server
    }
}

stream {
    # 配置针对 TCP/UDP 流量的一个或多个 server
    server {
        # 配置 server
    }
}

对于大多数指令,子上下文会继承父上下文中的指令值。上下文继承可以参考指令 proxy_set_header

Nginx 命令

Nginx 安装成功后,可以使用 nginx 命令执行查看版本、参数信息,或重新加载配置信息等操作。

[root@VM_120_242_centos ~]# nginx -h
nginx version: nginx/1.10.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : 查看版本信息并退出
  -V            : 查看版本信息和配置选项并退出
  -t            : 测试配置文件并退出
  -T            : 测试配置文件,打印并退出
  -q            : 在配置测试期间抑制非错误消息
  -s signal     : 发送信号到主进程,支持 4 个信号:stop、quit、reopen、reload
  -p prefix     : 设置前缀路径,默认是 /usr/share/nginx/
  -c filename   : 设置配置文件,默认是 /etc/nginx/nginx.conf
  -g directives : 设置配置文件外的全局指令

查看版本及配置选项

[root@VM_120_242_centos ~]# nginx -V
nginx version: nginx/1.10.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

测试配置文件

先在文件中随意添加一个字符,执行命令后可以检测出这个错误:

[root@VM_120_242_centos ~]# nginx -t
nginx: [emerg] unknown directive "a" in /etc/nginx/nginx.conf:102
nginx: configuration file /etc/nginx/nginx.conf test failed

测试成功的输出如下:

[root@VM_120_242_centos ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

平滑重启

平滑重启可以不中断 Nginx 服务。如果配置文件有错误,则 Nginx 不会重启:

[root@VM_120_242_centos ~]# nginx -s reload
nginx: [emerg] unknown directive "a" in /etc/nginx/nginx.conf:102
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值