Nginx配置文件详解

Nginx配置文件详解

Nginx 默认使用的配置文件是 nginx.conf,同时,在 nginx.conf 文件里面,我们还可以使用 include 指令包含其他的配置文件。

nginx.conf配置文件详解

路径

/etc/nginx/nginx.conf

说明

我们使用 vim 打开上面的路径,具体命令如下:

vim /etc/nginx/nginx.conf

如下图所示:

01_nginx conf配置文件详解.png

我们执行如上命令,打开配置文件,此时配置文件内容如下所示:

user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; 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 on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }

我们看到,最后一行,即:

include /etc/nginx/conf.d/*.conf;

是说明,我们在 nginx.conf 配置文件里面使用了 include 指令包含了 /etc/nginx/conf.d/ 路径下的所有的 conf 文件。

接着,我们可以看到最上面的 4 个配置,具体解释如下:

配置 说明
user 设置 nginx 服务的系统使用的用户
worker_processes 工作进程数,一般与 CPU 相等
error_log nginx 的错误日志
pid nginx 服务启动时候的 pid

接着,我们可以看到 events 配置,具体解释如下:

配置 说明
worker_connections 每个进程运行最大连接数
use 工作进程数

接着,我们就看到是 http 模块的配置,大体配置样式如下:

02_nginx conf配置文件详解.png

我们可以看到,在 http 下面,我们可以配置多个 server,每个 server 都对应一个站点服务,在每个单独的 server 里面配置每个 server 的信息。

Nginx配置文件总结

Nginx 默认使用的配置文件是 nginx.conf,同时,在 nginx.conf 文件里面,我们还可以使用 include 指令包含其他的配置文件。