一个默认的nginx.conf解析(简单讲解配置)

cat /usr/local/nginx/conf/nginx.conf

#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;

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}


一、整体结构概览

配置文件完整地包含了 Nginx 的五个核心层次

1. 全局块(main)      → 最外层,以 # 注释为主
2. events 块          → 配置事件模型
3. http 块            → HTTP 核心配置
   ├── 4. server 块    → 虚拟主机配置(默认只有一个)
   │    ├── 5. location /        → 根路径处理
   │    └── 5. location = /50x.html → 错误页面
   └── 其他 server 块(全被注释)

二、各配置块详细分析

1. 全局配置块(main)

#user  nobody;          # 默认以nobody运行,建议根据实际需要开启并指定用户
worker_processes  4;    #  在top按1查看cpu核数 这里我是4个 其实也可以写成auto自动识别
worker_cpu_affinity 0001 0010 0100 1000; #因为是四个
worker_rlimit_nofile  655360;  #设置 每个 worker 进程可以打开的最大文件描述符数量。
#error_log  logs/error.log;           # 错误日志,自己选级别
error_log  logs/error.log  notice;  
#error_log  logs/error.log  info;

pid        logs/nginx.pid;            # PID文件位置

2. events 块

events {
    worker_connections  65536;    # 每个worker进程最大连接数65536
}

特点

  • 没有指定 use 指令,Nginx 会自动选择当前系统最优的事件模型(Linux 会自动用 epoll
  • worker_connections 1024 是默认值,可以满足小型应用

3. http 块基础配置

http {
    include       mime.types;                # 引入文件类型映射
    default_type  application/octet-stream;  # 默认文件类型

    log_format  main  ...;                   # 
    access_log  logs/access.log  main;        # 访问日志

    sendfile        on;                       # 高效文件传输
    tcp_nopush     on;                        # 通常与sendfile配合使用

    #keepalive_timeout  0;
    keepalive_timeout  65;                     # 长连接超时65秒

    #gzip  on;                                  # Gzip压缩未启用

特点

  • 只开启了最基本的优化项(sendfilekeepalive_timeout
  • 日志、Gzip 压缩等常用功能都被注释了

4. server 块(默认虚拟主机)

配置如下图


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注