自学内容网 自学内容网

nginx主配置文件

Nginx的主配置文件nginx.conf,一般定义了Nginx的基本设置和全局配置。下面是对这个配置文件的详细解释:

文件结构

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    include  /opt/nginx/conf/vhost/*.conf;
}

配置详解

1. 全局配置
#user  nobody;
worker_processes  1;
  • #user nobody;:注释掉的行,用于指定Nginx工作进程的用户。默认情况下,Nginx以启动它的用户身份运行。
  • worker_processes 1;:指定Nginx的工作进程数。通常设置为CPU核心数,以充分利用多核处理器。
2. 错误日志
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
  • #error_log logs/error.log;:注释掉的行,用于指定错误日志的文件路径。
  • #error_log logs/error.log notice;:注释掉的行,用于指定错误日志的级别为notice
  • #error_log logs/error.log info;:注释掉的行,用于指定错误日志的级别为info
3. 进程ID文件
#pid        logs/nginx.pid;
  • #pid logs/nginx.pid;:注释掉的行,用于指定Nginx主进程的PID文件路径。
4. 事件模块
events {
    worker_connections  1024;
}
  • events块:配置Nginx的事件处理模型。
  • worker_connections 1024;:每个工作进程的最大并发连接数。总并发连接数为worker_connections * worker_processes
5. HTTP模块
http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    include  /opt/nginx/conf/vhost/*.conf;
}
  • include mime.types;:包含MIME类型配置文件,用于识别不同类型的文件。
  • default_type application/octet-stream;:设置默认的MIME类型。
  • #log_format main ...:注释掉的行,用于定义日志格式。
  • #access_log logs/access.log main;:注释掉的行,用于指定访问日志的文件路径和格式
  • sendfile on;:启用高效文件传输模式,用于提高文件传输效率。
  • #tcp_nopush on;:注释掉的行,用于控制TCP的Nagle算法,减少小包的发送。
  • #keepalive_timeout 0;:注释掉的行,用于关闭持久连接。
  • keepalive_timeout 65;:设置持久连接的超时时间。
  • #gzip on;:注释掉的行,用于启用Gzip压缩。
  • include /opt/nginx/conf/vhost/*.conf;包含虚拟主机配置文件,路径为/opt/nginx/conf/vhost/下的所有.conf文件。

总结

这个配置文件定义了Nginx的基本设置,包括工作进程数、错误日志、事件处理模型、HTTP模块的基本配置以及虚拟主机配置文件的包含路径。通过这些配置,Nginx可以有效地处理各种HTTP请求,并支持多个虚拟主机。


原文地址:https://blog.csdn.net/qq_41081716/article/details/142795242

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!