阅读nginx的配置文件

尝试在CentOS 8上使用nginx。

$ cat /etc/redhat-release 
CentOS Linux release 8.2.2004 (Core) 

安装

已安装了1.14.1版本。
据说当nginx的次要版本为偶数时,该版本为稳定版本,为奇数时,该版本为开发版本。

$ dnf install nginx -y 
...インストール

$ nginx -v
nginx version: nginx/1.14.1

设置文件

在/etc/nginx目录下有配置文件。
nginx.conf是原始的配置文件吗?

公式
Nginx官方文档中有关nginx核心模块的指令的详细介绍。

[root@f82608275426 nginx]# tree
.
|-- conf.d
|-- default.d
|-- fastcgi.conf
|-- fastcgi.conf.default
|-- fastcgi_params
|-- fastcgi_params.default
|-- koi-utf
|-- koi-win
|-- mime.types
|-- mime.types.default
|-- nginx.conf
|-- nginx.conf.default
|-- scgi_params
|-- scgi_params.default
|-- uwsgi_params
|-- uwsgi_params.default
`-- win-utf

nginx.conf 的释义是 Nginx 的主配置文件。


user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

使用者名称为nginx;

指定运行nginx的工作进程的用户。
nginx用户会在安装nginx时自动创建吗?
(主进程的运行用户会是root之类的吗?)

工作进程自动。

将Worker进程数设置为与CPU数相同是较好的选择,auto选项会自动查找并设置与CPU数相同的值。

错误日志记录在/var/log/nginx/error.log文件中。

错误日志文件的文件路径

运行nginx的进程ID保存在/run/nginx.pid中。

定义一个用来存储主进程的进程ID的文件
→ 刚安装完之后,/run/nginx.pid文件是不存在的。

$ cat /run/nginx.pid
cat: /run/nginx.pid: 没有那个文件或目录

包括 /usr/share/nginx/modules/*.conf;

读取每个模块的配置。默认情况下只有这个conf文件。

ll /usr/share/nginx/modules/    
total 28
drwxr-xr-x 2 root root 4096 Dec  9 14:39 .
drwxr-xr-x 4 root root 4096 Dec  9 14:39 ..
-rw-r--r-- 1 root root   72 Oct  7  2019 mod-http-image-filter.conf
-rw-r--r-- 1 root root   64 Oct  7  2019 mod-http-perl.conf
-rw-r--r-- 1 root root   71 Oct  7  2019 mod-http-xslt-filter.conf
-rw-r--r-- 1 root root   59 Oct  7  2019 mod-mail.conf
-rw-r--r-- 1 root root   61 Oct  7  2019 mod-stream.conf

事件{…}

上下文。编写了指定影响连接处理的指令的设置。

worker_connections设定为1024。

设置工作进程能够打开的并发连接的最大数。
请注意,该数量包括与客户端的连接以及所有连接(特别是代理服务器的连接)在内。

HTTP { … }

如下所述的上下文应该编写有关HTTP和HTTPS连接设置的内容。

日志格式 main …

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

在这里我们指定了日志格式。我们定义了一个名为”main”的日志格式。

remote_addr: 远程来源的IP地址
remote_user: 使用基本认证的用户名
time_local: 请求时间
request: 完整的原始请求行???
status: 响应状态
body_bytes_sent: 发送给客户端的字节数(不包括响应头)。
http_referer: 引荐网址
http_user_agent: 用户代理
http_x_forwarded_for: XFF(代理服务器转发的)

访问日志 /var/log/nginx/access.log main;

进行访问日志的配置。在这里指定了路径和日志格式。

发送文件开启。

嗯,这个呢,不知道呢…进行一下sendfile函数的设置是吗?

开启tcp_nopush。

听说这是在将sendfile设置为on时使用的参数。

在FreeBSD中使用TCP_NOPUSH套接字选项,在Linux中使用TCP_CORK套接字选项,可以启用或禁用这些选项。只有在使用sendfile时,这些选项才有效。启用此选项可以将响应头和文件的开头合并到一个数据包中进行发送。同时会使用完整的数据包来发送文件。

启用tcp_nodelay;

启用或禁用TCP_NODELAY选项。该选项在连接转入保持活动状态时启用。此外,它还适用于SSL连接,无缓冲代理和WebSocket代理。

保持连接超时时间为65秒。

等待来自客户的请求时间

types_hash_max_size 2048;
哈希表最大尺寸为2048;

定义MIME类型哈希表条目的最大大小。

包括/etc/nginx/mime.types;

读取由nginx提供的MIME类型。扩展名和MIME类型的对应表。

默认类型为 application/octet-stream;

指定默认的 MIME 类型来响应。
application/octet-stream 是用于通用的二进制数据(或真实类型未知的二进制数据)的 MIME 类型。

服务器 { … }

服务器设置

请参考。

以下是一些关于Nginx配置的链接:

1. CentOS下Nginx配置详解:https://www.bnote.net/centos/nginx_conf01.html
2. Nginx核心模块文档:http://nginx.org/en/docs/ngx_core_module.html
3. 理解Nginx配置文件结构和配置上下文:https://www.codeflow.site/ja/article/understanding-the-nginx-configuration-file-structure-and-configuration-contexts
4. Nginx日志模块文档:http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format
5. Nginx变量详解:http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
6. Nginx配置示例:https://qiita.com/teco_naka/items/f42671850122ed69d0c5
7. 使用Nginx的一些建议:https://qiita.com/yuse/items/fe05cec1a331306eac19

bannerAds