计算机网络/计算机科学与应用/系统/运维/开发

4、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;


参考文档:

https://blog.redis.com.cn/doc/core/mainmodule.html


1、核心指令:

daemon on | off

默认:on

生产环境下不要使用,同时该环境下不要使用 master_process指令


env VAR | VAR=VALUE

对环境变量重新定义

env OPENSSL_ALLOW_PROXY_CERTS=1


debug_points [stop | abort ]

默认:none

激化所有设置的调试点


error_log file [ debug | info | notice | warn | error | crit ]

默认: ${prefix}/logs/error.log

使用环境:http、server、location

如果想关闭错误日志功能:

error_log /dev/null crit;


include file | *

默认值:none

用于载入配置文件,注意绝对路径和相对路径问题,支持通配符文件

include vhosts/*.conf;


lock_file file

默认值:编译时指定


master_process  on | off


pid 文件

默认值:编译时指定

kill -HUP 'cat /var/log/nginx.pid'


user user [group]

默认值:nobody nobody

user www users;


worker_cpu_affinity

语法:worker_cpu_affinity cpumask [cpumask...]

默认值:none

实例:

worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;


worker_processes number

默认值:1

worker_processes  1;


二、事件模块

事件模块用于控制Nginx如何处理链接

配置实例:

events {
    worker_connections  1024;
}


以下指令只能在events 区段设置

accept_mutex_delay Nms;

默认值:500ms

如果一个进程没有互斥锁,它将延迟至少多长时间


debug_connection [ip | CIDR ]

默认值:none

用于指定只记录某个ip地址或者某个网段的客户端产生的debug信息,可以指定多个参数

error_log /var/log/nginx/errors;
events {
  debug_connection   192.168.1.1;
}


worker_connections  number

默认值:1024

设置没给worker进程所能处理的链接数

通过 worker_processes 和 worker_connections 计算最大客户端链接数

max_clients = worker_processes * worker_connections

反向代理中,最大连接数计算方式:

max_clients = worker_processes * worker_connections/4


use type

默认值:编译时指定

指定事件驱动模型

参考:https://blog.51cto.com/u_13236892/5821583


使用实例:

events {
    worker_connections 1024;
    use epoll;
    worker_connections 32768
}


三、HTTP内核模块

配置结构

http {
    ...
    server {
        listen 80;
        server_name  www.yy.cn; 
        ...
        
        location / {
                root  html;
                index  index.html index.htm;
        }
    }
    server {
        ...
    }
    
    server {
        listen  443;
        server_name www.xx.cn; 
        
        ssl on;
        ssl_certificate  cert.pem;
        ssl_certificate_key  cert.key;
        
        ssl_session_timeout 5m;
        ...
        
        location / {
             root  html;
             index  index.html index.htm;           
        }
    }
}


aio [on | off | sendfile ]

默认值:off

使用环境:http、server、location

自动禁用sendfile支持


server {...}

默认值:on

使用环境:http

用于配置虚拟主机


server_name  name [...]

默认值:server_name ""

使用环境:server

server {
    server_name example.com  www.example.com;
}


四、完整配置文件

#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
        #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.conf

人生建议:不要因为嘴硬而失去重要的东西,清醒,知趣,明得失,知进退。

评论

^