美文网首页
4.Nginx的目录与配置语法 之 默认配置语法

4.Nginx的目录与配置语法 之 默认配置语法

作者: 黄刚刚 | 来源:发表于2019-06-24 13:47 被阅读0次

前提:

我们在"2. Nginx的目录与配置语法 之 安装目录"笔记中有解析nginx安装目录,

/etc/nginx/nginx.conf        (类型:配置文件     作用:Nginx主配置文件)

/etc/nginx/conf.d/default.conf        (类型:配置文件     作用:Nginx默认主配置文件,安装时自动生成的)

而且在"3.Nginx的目录与配置语法 之 安装编译参数"笔记中也有记载了安装编译参数

--conf-path=/etc/nginx/nginx.conf (nginx的配置文件)

所以,我们直接进入主题,查看/etc/nginx/nginx.conf (nginx的配置文件),并且直接在配置文件中用注释的方式来说明

nginx默认配置语法

说明:/etc/nginx/nginx.conf (nginx的配置文件)的内容有3大块配置,下面分为(1,2,3)来记录

[root@localhost nginx]# cat nginx.conf

1.全局性或者对服务级别的配置 :

这块是配置文件最前面的一些内容,这些内容主要是配置了nginx的进程,日志文件等对全局性的或者对服务级别的配置

#注释: user 设置nginx服务的系统使用用户  (这是设置了nginx 的worker进程所使用的用户,默认是nginx用户,当然我们也可以改成其他的用户,但是一般没有必要的情况就保持nginx这个普通用户的进程就可以了)

user  nginx;

#注释:worker_processes  工作进程数  (这一块其实是跟我们nginx的一个多进程的方式是有关系的, nginx的优势 I/O复用在worker_processes的设置下会启用多个进程来进行增大nginx的连接数和并发处理,这个的数量一般和CUP的数量保持一致就好了,一个CUP处理一个nginx进程,在优化nginx的时候可以考虑根据服务器的cpu数量配置一下)

worker_processes  1;

#注释: error_log   nginx的错误日志  

error_log  /var/log/nginx/error.log warn;

#注释:  pid    nginx服务启动时候pid (存放nginx.pid的位置,在nginx服务启动的时候,会把nginx服务本身的pid记录到这个文件里面去,方便我们进行查找和操作系统的管理)

pid        /var/run/nginx.pid;

2.事件模块配置

events模块是一个事件的模块,events模块中包含nginx中所有处理连接的设置

events {

    #注释:use  使用epoll的I/O 模型 (值得的注意的是,根据不同的系统内核,Nginx会使用不同的事件驱动机制,所以  Nginx默认配置没有指定use参数,后续优化Nginx时可以考虑直接配置好使用的I/O 模型驱动机制

    #use epoll;

    #注释:worker_connections 每个进程允许的最大连接数量  ( 个人认为是每一个worker进程能并发处理的最大连接数,包含所有连接数。理论上worker_connections  设置越大越好,一般在企业级nginx应用优化的时候worker_connections参数也是必须要去调节的, 当然,一定要按照自己的实际情况而定,也不能设置太大,不能让CPU跑满100% , 按反向代理模式下最大连接数的理论计算公式:   最大连接数 = worker_processes * worker_connections/4 , 生产环境中worker_connections 建议值最好超过9000

    worker_connections  1024;

}

#http是协议界别(个人理解为nginx在处理http的请求会按照http这块的配置去处理)

http {

    #引入子配置文件,设置http协议的Content-type与扩展名对应关系

    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;

}

再来看看/etc/nginx/nginx.conf(nginx的配置文件)的http模块中引入的/etc/nginx/conf.d/*.conf的文件内容

[root@localhost ~]# ls /etc/nginx/conf.d/*.conf;

/etc/nginx/conf.d/default.conf

[root@localhost ~]# cat /etc/nginx/conf.d/default.conf

#server 配置(个人理解 , 不一定程序的运行是这个顺序 , 但是可以这样理解 , server , 顾名思义 , server即服务 , 当nginx处理http请求时 , 要匹配使用哪个server , 肯定要从每个server不同的配置参数去区分了 , 比如listen端口 , 比如server_name)

server {

    #当前server监听的端口号 , 当访问localhost:80时 , 当前server监听的端口号就是80 , 这个server的端口号 , 符合nginx处理localhost:80这个http请求的listenl参数

    listen      80;

    #当前server监听的服务名称 , 当访问localhost:80时 , 当前server监听的服务名称就是localhost , 符合nginx处理localhost:80这个http请求的server_name参数

    server_name  localhost;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    #一个server里面允许配置多个location , location配置则匹配当前server指向的文件路径

    #这里默认配置location的 "/"参数指的是 "匹配全部路径"

    location / {

        # root定义的是文件目录路径

        root  /usr/share/nginx/html;

        #index定义的是默认文件 (如访问location时,上面的root已经定义了文件目录路径了,这里定义的是默认文件 , 所以会在/usr/share/nginx/html目录中找index.html文件,如果没有再找index.htm文件 , 顺序就是从左到右)

        index  index.html index.htm;

    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    #

    #定义错误码  (这里定义了500 502 503 504状态码指向"/50x.html"路径)

    error_page  500 502 503 504  /50x.html;

    #这里默认配置location的 "/50x.html"参数指的是 "匹配如http://192.168.58.100/50x.html这样的http请求,然后在"/usr/share/nginx/html"目录中找到"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;

    #}

}

相关文章

网友评论

      本文标题:4.Nginx的目录与配置语法 之 默认配置语法

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