美文网首页
NGINX配置文件

NGINX配置文件

作者: 夏胖运维 | 来源:发表于2020-11-18 21:21 被阅读0次

    1. 配置文件结构

    我们从nginx安装后默认的配置文件,去了解nginx配置文件的结构

    [root@web-01 ~]# cat /etc/nginx/nginx.conf 
    
    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;
    }
    
    [root@web-01 ~]# cat /etc/nginx/conf.d/default.conf 
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/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   /usr/share/nginx/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;
        #}
    }
    

    我们剔除文件里面的基础指令及注释信息,应该是如下形式:

    user  nginx;
    worker_processes  1;
    
    ...
    ...
    
    events {
        ...
    }
    
    
    http {
        ...
        server {
            ...
            location / {
               ...
            }
        }
    }
    
    

    我们会发现NGINX配置文件的结构包含events、http、server、location这四大块,另外我们还发现一些指令不包含在这几大块中,我们称他们为 mian 指令。每块具体意义如下:

    模块 意义
    main块 主要控制nginx子进程的所属用户/用户组、派生子进程数量、错误日志位置/级别、pid文件位置、进程对应CPU、进程能够打开的文件描述符数量等。
    events块 控制nginx处理连接的方式
    http块 是nginx处理http请求的主要配置模块,有关http的相关配置都在这个模块中进行。
    server块 nginx中虚拟主机的配置块,可以配置多个。
    location块 包含在server块中,是server中对应的目录级别的控制块,可以配置多个。

    2. 基础指令介绍

    2.1. 配置运行用户和组

    user   user [group];
    

    user:指定运行nginx子进程的用户

    group:指定运行nginx子进程的用户组

    2.2. 配置worker线程数量

    worker_process number|auto;
    

    number:指定nginx进程最多生产worker process数量,默认情况number = 1

    auto:设置该值,nginx将自动检测CPU核心数,并将worker process数量设置成等同CPU核心数量

    2.3. 配置最大打开文件数

    worker_rlimit_nofile number;
    

    number:设置一个worker进程打开最大文件数量

    2.4. 配置最大连接数

    worker_connections number;
    

    number:设置一个worker进程的最大网络连接数

    2.5. 配置PID文件位置

    pid /path/file;
    

    注意:设置的nginx进程运行的用户需要对 /path/file 文件有读写权限

    2.6. 配置错误日志

    error_log file|stderr [debug|info|notice|warn|error|crit|alert|emerg]
    

    从语法结构上看,nginx 服务器的日志文件支持蔬菜到摸个固定的文件file或者输出到标准错误输出stderr;日志的级别是可选项,有低到高为debug(需要编译的时候使用--with-debug开启debug开关)到emerg。需要注意的是,设置某个级别后,比这个级别高的日志也会被记录。比如设置warn级别后,基本为warn及error、crit、alert和emerg的日志都会被记录下来。

    例如:

    error_log logs/error.log error;
    

    2.7. 配置文件引入指令

    include  file;
    

    file,号引入的配置文件,他支持相对路径。

    在一些情况下,我们可能将其他的nginx配置或第三方模块的配置引入到当前的主配置文件中,此时可以使用此指令。

    此指令可以放在配置文件文件的任意位置。

    相关文章

      网友评论

          本文标题:NGINX配置文件

          本文链接:https://www.haomeiwen.com/subject/inapbktx.html