• Nginx的核心配置文件详解


    核心配置文件

    配置文件详解

    该文件位于Nginx的安装目录/usr/local/nginx/conf目录下,名字为nginx.conf

    [root@administrator conf]# pwd
    /usr/local/nginx/conf
    [root@administrator conf]# ll
    total 68
    -rw-r--r-- 1 root root 1077 Mar  2 13:53 fastcgi.conf
    -rw-r--r-- 1 root root 1077 Mar  2 13:53 fastcgi.conf.default
    -rw-r--r-- 1 root root 1007 Mar  2 13:53 fastcgi_params
    -rw-r--r-- 1 root root 1007 Mar  2 13:53 fastcgi_params.default
    -rw-r--r-- 1 root root 2837 Mar  2 13:53 koi-utf
    -rw-r--r-- 1 root root 2223 Mar  2 13:53 koi-win
    -rw-r--r-- 1 root root 5231 Mar  2 13:53 mime.types
    -rw-r--r-- 1 root root 5231 Mar  2 13:53 mime.types.default
    -rw-r--r-- 1 root root 2656 Mar  2 13:53 nginx.conf
    -rw-r--r-- 1 root root 2656 Mar  2 13:53 nginx.conf.default
    -rw-r--r-- 1 root root  636 Mar  2 13:53 scgi_params
    -rw-r--r-- 1 root root  636 Mar  2 13:53 scgi_params.default
    -rw-r--r-- 1 root root  664 Mar  2 13:53 uwsgi_params
    -rw-r--r-- 1 root root  664 Mar  2 13:53 uwsgi_params.default
    -rw-r--r-- 1 root root 3610 Mar  2 13:53 win-utf
    [root@administrator conf]# 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    规则

    1、用“#”表示注释
    
    2、每行配置的结尾需要加上分号
    
    3、如果配置项值中包括语法符号,如空格符,那么需要使用单引号或双引号括住配置项值,否则Nginx会报语法错误
    
    4、单位简写。当指定空间大小时,可以使用的单位包括:K或k千字节(KiloByte,KB),M或者m兆字节(MegaByte,MB)
       当指定时间时,可以使用的单位包括:ms(毫秒),s(秒),m(分钟),h(小时),d(天),w(周,包含7天),M(月,包含30天),y(年,包含
    含365天)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Nginx的核心配置文件主要由三个部分构成

    #====================基本配置、全局配置===============================
    
    #配置worker进程运行用户 nobody也是一个linux用户,一般用于启动程序,没有密码
    #user  nobody;  
    
    #配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量
    worker_processes  1;  
    
    #配置全局错误日志及类型,[debug | info | notice | warn | error | crit],默认是error
    #error_log  logs/error.log;  
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;  #配置进程pid文件 
    
    #====================events配置===============================
    
    #配置工作模式和连接数
    events {
        #配置每个worker进程连接数上限,nginx支持的总连接数等于worker_processes * worker_connections		
        worker_connections  1024;  
    }
    
    #====================http配置===============================
    
    #配置http服务器,利用它的反向代理功能提供负载均衡支持
    http {
        #配置nginx支持哪些多媒体类型,可以在conf/mime.types查看支持哪些多媒体类型
        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日志及存放路径,并使用上面定义的main日志格式
        #access_log  logs/access.log  main;
    
        sendfile        on;  #开启高效文件传输模式
        #tcp_nopush     on;  # 与sendfile配合使用,当一个数据包累积到一定大小后发送,提升效率,防止网络阻塞
    
        #keepalive_timeout  0;
        keepalive_timeout  65;  #长连接超时时间,单位是秒
    
        #gzip  on;  #开启gzip压缩输出
    	
    
        #配置虚拟主机
        server {
            listen       80;  #配置监听端口
            server_name  localhost;  #配置IP 域名 localhost
    
            #charset koi8-r;  #配置字符集
    
            #access_log  logs/host.access.log  main;  #配置本虚拟主机的访问日志
    
    	#默认匹配斜杠/的请求,当访问路径中有斜杠/,会被该location匹配到并进行处理
            location / {
    	    #root是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
                root   html;  
    	    #配置首页文件的名称
                index  index.html index.htm;  
            }		
    
            #error_page  404              /404.html;  #配置404页面
            # redirect server error pages to the static page /50x.html
            #error_page   500 502 503 504  /50x.html;  #配置50x错误页面
            
    	#精确匹配
    	location = /50x.html {
                root   html;
            }
    
    	#PHP脚本请求全部转发到Apache处理
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
    	#PHP脚本请求全部转发到FastCGI处理
            # 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;
            #}
    
    	#禁止访问 .htaccess文件
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
    
    	
        #配置另一个虚拟主机 server配置,可以有多个
        # 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服务,安全的网络传输协议,加密传输,端口443,运维来配置
        # 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;
        #    }
        #}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139

    gzip压缩

    # 开启压缩功能,提供传输效率,解压带宽
    gzip on;
    #限制最小压缩,小于1字节文件不压缩
    gzip_min_length 1;
    # 定义压缩级别,压缩比例,取值1-9,压缩越多,CPU使用率越高
    gzip_comp_level 3;
    # 定义压缩文件类型
    gzip_types text/plain application/javascript image/png image/jepeg .........
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    location匹配规则

    空格 :默认匹配,普通匹配

    location / {
    root /root;
    }
    
    • 1
    • 2
    • 3

    = :精确匹配

    location = /www/test.png {
    root /root;
    }
    
    • 1
    • 2
    • 3

    ~* :匹配正则表达式,不区分大小写

    # 在/root/www目录一层一层的去映射,寻找符合正则的图片
    location ~ \.(GIF|jpg|png|jpeg) {
    root /root/www;
    }
    
    • 1
    • 2
    • 3
    • 4

    ~ :匹配正则表达式,区分大小写

    #GIF必须大写才能匹配到
    location ~ \.(GIF|jpg|png|jpeg) {
    root /root/www;
    }
    
    • 1
    • 2
    • 3
    • 4

    ^~ :以某个字符路径开头

    # ^:,~:正则 即不使用正则,只能从/root/www/img寻找资源
    location ^~ /www/img{
    root /root;
    }
    
    • 1
    • 2
    • 3
    • 4

    upstream指令

    max_conns参数

    限制每台server的连接数,用于保护避免过载,可起到限流作用

    # worker进程设置1个,便于测试观察成功的连接数
    worker_processes 1;
    
    upstream tomcats {
    	server 127.0.0.1:8080 max_conns=2;
    	server 127.0.0.1:8081 max_conns=2;
    	server 127.0.0.1:8082 max_conns=2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    slow_start参数

    缓慢启动加入集群,启动开始请求流量由小慢慢到大,商业版,需要付费

    upstream tomcats {
    	server 127.0.0.1:8080 weight=3 slow_start=60s;
    	server 127.0.0.1:8081 weight=2;
    	server 127.0.0.1:8082 weight=2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    该参数不能使用在 hash 和 random load balancing 中
    
    如果在upstream中只有一台 server,则该参数失效
    
    • 1
    • 2
    • 3

    down参数

    用于标记服务节点不可用

    upstream tomcats {
    	server 127.0.0.1:8080 down;
    	server 127.0.0.1:8081 weight=2;
    	server 127.0.0.1:8082 weight=2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    backup参数

    表示当前服务器节点是备用机,只有在其他的服务器都宕机以后,自己才会加入到集群中,被用户访问到

    upstream tomcats {
    	server 127.0.0.1:8080 backup;
    	server 127.0.0.1:8081 weight=2;
    	server 127.0.0.1:8082 weight=2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    backup 参数不能使用在 hash 和 random load balancing中。
    
    • 1

    max_fails参数

    表示失败几次,则标记server已宕机,剔出上游服务

    fail_timeout参数

    表示失败的重试时间

    # 表示在5秒内请求某一server失败达到2次后,则认为该server已经挂了或者宕机了
    随后再过5秒,期间不会有新的请求到达刚刚挂掉的节点上,而是会请求正常运作的server
    5秒后会再有新请求尝试连接挂掉的server,如果还是失败,重复上一过程,直到恢复
    
    upstream tomcats {
    	server 127.0.0.1:8080 max_fails=2 fail_timeout=5s;
    	server 127.0.0.1:8081 weight=2;
    	server 127.0.0.1:8082 weight=2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Keepalived

    Keepalived用于提高吞吐量

    keepalived : 设置长连接处理的数量
    
    proxy_http_version :设置长连接http版本为1.1
    
    proxy_set_header :清除connection header 信息
    
    • 1
    • 2
    • 3
    • 4
    • 5
    upstream tomcats {
    	server 127.0.0.1:8080 max_fails=2 fail_timeout=1s;
    	server 127.0.0.1:8081 weight=2;
    	server 127.0.0.1:8082 weight=2;
    	keepalive 32;
    }
    
    server {
    	listen 80;
    	server_name www.tomcats.com;
    	location / {
    		proxy_pass http://tomcats;
    		proxy_http_version 1.1;
    		proxy_set_header Connection "";
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    缓存配置

    浏览器端的缓存

    expires可以控制浏览器端的缓存
    
    • 1
    	location /static {
    		alias /root/www;
    		expires 10s;
    	}
    
    • 1
    • 2
    • 3
    • 4
    # 缓存10s后过期
    expires 10s;
    
    # 缓存在晚上22:30过期
    expires @22h30m;
    
    # 缓存提前过期,即缓存失效
    expires -1h;
    
    # 不设置缓存,1970年就过期了
    expires epoch;
    
    # 默认,关闭缓存
    expires off;
    
    # 最长的缓存时间
    expires max;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    反向代理端的缓存

    # proxy_cache_path 设置缓存目录
    # keys_zone 设置共享内存以及占用空间大小
    # max_size 设置缓存大小
    # inactive 超过此时间则被清理
    # use_temp_path 临时目录,使用后会影响nginx性能
    
    proxy_cache_path /usr/local/nginx/upstream_cache keys_zone=mycache:5m max_size=1g inactive=8h use_temp_path=off
    
    
    location / {
    	proxy_pass http://tomcats;
    	# 启用缓存,和keys_zone一致
    	proxy_cache mycache;
    	# 针对200304状态码缓存时间为8小时
    	proxy_cache_valid 200 304 8h;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    location的匹配优先级

    location = /uri:=表示精确匹配,只有完全匹配上才能生效
    
    location ^~/uri:^~开头对URL路径进行前缀匹配,并且在正则之前
    
    location~pattern:开头表示区分大小写的正则匹配
    
    location~*pattern:开头表示不区分大小写的正则匹配
    
    location/uri:不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
    
    location /:通用匹配,任何未匹配到其它location的请求都会匹配到
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    日志

    日志配置

    配置日志格式内容

    log_format  main '$remote_user [$time_local] $http_x_Forwarded_for $remote_addr $request '
                         '$http_x_forwarded_for '
                         '$upstream_addr '
                         'ups_resp_time: $upstream_response_time '
                         'request_time: $request_time \n';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    变量说明
    $bytes_sent发送给客户端的总字节数
    $connection连接的序列号
    $connection_requests当前通过一个连接获得的请求数量
    $msec日志写入时间。单位为秒,精度是毫秒
    $pipe如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”
    $request_length请求的长度,包括请求行,请求头和请求正文
    $request_time请求处理时间,单位为秒,精度毫秒:从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止
    $status记录请求状态
    $time_iso8601ISO8601标准格式下的本地时间
    $time_local通用日志格式下的本地时间
    $remote_addr当客户端使用代理服务器访问时,只能获得代理服务器地址
    $http_x_forwarded_for获得客户端真实IP地址
    $remote_user记录客户端用户名称
    $request记录请求的URL和HTTP协议
    $http_referer记录从哪个页面链接访问过来的
    $body_bytes_sent发送给客户端的字节数,不包括响应头的大小;该变量与Apache模块mod_Iog_config里的“%B”参数兼容
    $http_user_agent记录客户端浏览器相关信息

    配置日志

    access_log /usr/local/nginx/logs/access.log main buffer=1k;
    
    • 1
    参数说明
    path指定日志存放位置
    format指定日志格式,跟log_format的名字对应,如main。默认使用预定义的combined
    buffer指定日志写入时的缓存大小。默认64k
    gzip日志写入前先进行压缩。压缩率可以指定,从1到9数值越大压缩比越高,同时压缩的速度也越慢。默认是1
    flush设置缓存的有效时间。如果超过flush指定的时间,缓存中的内容将被清空
    if条件判断。如果指定的条件计算为0或空字符串,那么该请求不会写入日志

    日志切割

    创建shell文件

    创建logs_ cut.sh可执行文件

    #!/bin/bash
    LOG_PATH="/var/log/nginx/"
    RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
    PID=/var/run/nginx/nginx.pid
    mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
    mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
    #向Nginx主进程发送信号,用于重新打开日志文件
    kill -USR1 `cat $PID`
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    添加可执行的权限

    chmod +x logs_ cut.sh
    
    • 1

    测试日志切割

    ./logs_ cut.sh
    
    • 1

    使用定时任务

    安装定时任务

    yum install crontabs
    
    • 1

    crontab -e 编辑并且添加一行新的任务

    */1 * * * * /usr/local/nginx/sbin/logs_ cut.sh
    
    • 1

    重启定时任务

    service crond restart
    
    • 1

    常用定时任务命令

    service crond start // 启动服务
    
    service crond stop // 关闭服务
    
    service crond restart // 重启服务
    
    service crond reload // 重新载入配置
    
    crontab -e // 编辑任务
    
    crontab -l // 查看任务列表
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    【高度预估】基于matlab卡尔曼滤波和粒子滤波无人机离地高度估计【含Matlab源码 2255期】
    骨传导耳机品牌排行榜前十名,目前最好的几款骨传导耳机推荐
    vue中watch监听事件与计算属性的区别
    EDA工具开发中的调参方法
    jenkins流水线部署springboot应用到k8s集群(k3s+jenkins+gitee+maven+docker)(2)
    Oracle 插入数据
    GitHub上克隆项目
    纯干货无广告,毕业大论文,如何优雅地拼拼凑凑,降重和润色
    Lumerical---FDTD仿真区域设置问题
    山东大学单片机原理与应用实验 3.8 ADC0808/9信号采集实验
  • 原文地址:https://blog.csdn.net/qq_38628046/article/details/116463815