Nginx学习日记(2)---ngnix配置文件解读

3 篇文章 0 订阅
2 篇文章 0 订阅

今天是学习nginx的第二天,第一天,学习了如何安装,Nginx学习日记(1),可以去看一看。接下来进入正题。

使用过nginx的人应该都知道,nginx的精髓应该就是它的配置文件了,看一个人对ngnix了解不了解,就要看他对配置这个nginx.conf文件熟不熟悉。对于刚入门的同学,一定要多看配置文件。

安装好后的nginx配置文件如下(我添加了一些注释,只是为了让你们对这个更了解)

########### 每个指令必须有分号结束。#################

#user  nobody;   # user 指定运行 nginx 的用户和组(第一个参数为用户第二个为组,这里只有用户)worker_processes  1;   # 指定工作进程数(一般设置为CPU核数)

#日志级别: debug > info > notice > warn > error > crit > alert > emerg

#error_log  logs/error.log;  # 指定错误日志为 logs/ 目录下的 error.log 文件
#error_log  logs/error.log  notice; # 指定错误日志,并指定写入格式为 notice 
#error_log  logs/error.log  info;  # 指定错误日志,并指定写入格式为 info 

#pid        logs/nginx.pid;  #指定 pid 文件(存放主进程 pid 号),启动ngnix的时候帮你生成的,把nginx进程id放到这个里面,方便后面的操作,如重启和停止nginx

# nginx 连接配置模块
events {
    worker_connections  1024;  # 指定每个工作进程最大连接数为 1024
}


http {
    include       mime.types;  # 通过 include 加载 mime.types 文件,里面的 types {} 模块将文件扩展名映射到响应的 MIME 类型

    default_type  application/octet-stream;  # 定义响应的默认 MIME 类型

     #定义日志的格式: log_format  [自定义名称] [一些你自己喜欢的格式],例子main只是一个名称
    #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;  # 指定访问日志和写入格式为 main

    sendfile        on;  # 启用或者禁用 sendfile()
    #tcp_nopush     on;  # 启用或者禁用使用套接字选项(仅在sendfile使用时使用)

    #keepalive_timeout  0;   # 0值禁用保持活动的客户端连接
    keepalive_timeout  65;   # 超时时间为65s

    #gzip  on;   # 启用或者禁用 gzip

    # 虚拟主机配置模块
    server {
        listen       80;  #监听的80端口
        server_name  localhost;  #监听域名或主机ip:localhost

        #charset koi8-r;  # 将指定的 charset 添加到 “Content-Type” 响应头字段。如果此charset与source_charset指令中指定的charset不同,则执行转换。

        #access_log  logs/host.access.log  main; #单独制定访问这个虚拟主机的日志存储地址

        # 将特定的文件或目录重新定位,如 php 文件,image 目录等
        location / {
            root   html;         # 设置请求的根目录
            index  index.html index.htm;  #设置索引,按顺序执行。换句话说就是你匹配到这个路径的时候,你要跳转的页面是这个index.html或者是index.htm
        }

        #error_page  404              /404.html;   # 定义显示 404 错误的 uri

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

        # location 精准匹配 '/50x.html'
        location = /50x.html {
            root   html;
        }

        // 下面的反向代理的配置
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
             # 设置代理服务器的协议和地址,以及应该映射位置的可选URI。作为协议,可以指定“http”或“https”。该地址可以指定为一个域名或IP地址,以及一个可选端口
        #    proxy_pass   http://127.0.0.1;   #反向代理http://127.0.0.1地址
        #}

        #下面的我也不是很清楚,是百度的,好像是针对php的配置,不正确请指正,谢谢理解。
        # 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 服务器的地址。地址可以指定为一个域名或 IP 地址,以及一个端口
        #    fastcgi_index  index.php;  # 设置将在以斜杠结尾的URI之后追加的文件名
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  # 设置一个应该传递给FastCGI服务器的参数。
        #    include        fastcgi_params;  # 加载 conf/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
    #
    # ssl 配置,要启用 ssl 模块需要在编译 nginx 时加上 --with-http_ssl_module 参数
    #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文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

1、全局块:配置影响nginx全局的配置,如用户和用户组配置,pid存放路径,全局的日志配置(日志存放路径,日志的等级,日志的格式)等。

2、event块:配置影响nginx服务器或与用户的网络连接。如每个工作进程的最大连接数work_connections,是全局生效的

3、http块:nginx服务器配置中的重要部分,代理,缓存,日志定义等绝大部分功能和第三方配置都可以放在个模块

4、http全局块:主要配置影响这个http范围里面的server的配置,如日志的定义,gzip启用和禁用等。

5、serve块:与虚拟主机有密切的关系,每个http中可以有多个server,也就可以定义多个虚拟主机服务。在server里面配置监听端口,主机ip和域名。日志也可以在这里面定义,这边定义的日志使用范围仅限于这个server

6、location块:配置请求的路由,以及各种页面的处理情况。

累死我了。

下面是我网上百度的一个配置,看着挺全的,给你们看看。你们也可以在百度多一点日志看看,看多了,就会写了,写多了就成大神了,我也希望早日成为大神。

  ########### 每个指令必须有分号结束。#################

#user administrator administrators;  #配置用户或者组,默认为nobody nobody。

#worker_processes 2;  #允许生成的进程数,默认为1

#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址

error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server
块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg

events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on 
    #惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}

http {
    include       mime.types;   #文件扩展名与文件类型映射表

    default_type  application/octet-stream; #默认文件类型,默认为text/plain

    #access_log off; #取消服务日志    

    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    #1、$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;

    access_log log/access.log myFormat;  #combined为日志格式的默认值

    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    #这个叫啥来着,反正就是一个很厉害的名称 ,对,负载均衡
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备,等上一个服务挂了后就会启用这个服务器
    }

    error_page 404 https://www.baidu.com; #错误页

    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
}

好了,今天的分享就到这里了,希望看后的同学们可以有很多的收获,有问题可以留言,私信也可以的。

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值