• nginx详细配置


    安全问题,建议用nobody,不要用root.
    user  nobody;
    
    worker数和服务器的cpu数相等是最为适宜
    worker_processes  2;
    
     work绑定cpu(4 work绑定4cpu)
    worker_cpu_affinity 0001 0010 0100 1000
    
     work绑定cpu (4 work绑定8cpu中的4个) 。
    worker_cpu_affinity 0000001 00000010 00000100 00001000  
    
    
    
     error_log path(存放路径) level(日志等级)path表示日志路径,level表示日志等级,
     具体如下:[ debug | info | notice | warn | error | crit ]
     从左至右,日志详细程度逐级递减,即debug最详细,crit最少,默认为crit。 
    
     error_log  logs/error.log;
     error_log  logs/error.log  notice;
     error_log  logs/error.log  info;
    
     pid        logs/nginx.pid;
    
    
    events {
         这个值是表示每个worker进程所能建立连接的最大值,所以,一个nginx能建立的最大连接数,应该是worker_connections * worker_processes。
         当然,这里说的是最大连接数,对于HTTP请求本地资源来说,能够支持的最大并发数量是worker_connections * worker_processes,
         如果是支持http1.1的浏览器每次访问要占两个连接,
         所以普通的静态访问最大并发数是: worker_connections * worker_processes /2,
         而如果是HTTP作为反向代理来说,最大并发数量应该是worker_connections * worker_processes/4。
         因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。
    
        worker_connections  1024;  
    
         这个值是表示nginx要支持哪种多路io复用。
         一般的Linux选择epoll, 如果是(*BSD)系列的Linux使用kquene。
         windows版本的nginx不支持多路IO复用,这个值不用配。
        use epoll;
    
          当一个worker抢占到一个链接时,是否尽可能的让其获得更多的连接,默认是off 。
        multi_accept on;
    
          默认是on ,开启nginx的抢占锁机制。
        accept_mutex  on;
    }
    
    
    http {
         当web服务器收到静态的资源文件请求时,依据请求文件的后缀名在服务器的MIME配置文件中找到对应的MIME Type,再根据MIME Type设置HTTP Response的            Content-Type,然后浏览器根据Content-Type的值处理文件。
    
         include       mime.types;
    
         如果 不能从mime.types找到映射的话,用以下作为默认值
         default_type  application/octet-stream;
    
          日志位置
         access_log  logs/host.access.log  main;
    
          一条典型的accesslog:
          101.226.166.254 - - [21/Oct/2013:20:34:28 +0800] "GET /movie_cat.php?year=2013 HTTP/1.1" 200 5209 "http://www.baidu.com"          "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR          3.0.30729; Media Center PC 6.0; MDDR; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322; Tablet PC 2.0); 360Spider"
     
          1 )101.226.166.254:(用户IP)
          2 )[21/Oct/2013:20:34:28 +0800]:(访问时间) 
          3 )GET:http请求方式,有GET和POST两种
          4 )/movie_cat.php?year=2013:当前访问的网页是动态网页,movie_cat.php即请求的后台接口,year=2013为具体接口的参数
          5 )200:服务状态,200表示正常,常见的还有,301永久重定向、4XX表示请求出错、5XX服务器内部错误
          6 )5209:传送字节数为5209,单位为byte
          7 )"http://www.baidu.com":refer:即当前页面的上一个网页
          8 )"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729;  .NET            CLR 3.0.30729; Media Center PC 6.0; MDDR; .NET4.0C; .NET4.0E; .NET CLR 1.1.4322; Tablet PC 2.0); 360Spider": agent字段:          通常用来记录操作系统、浏览器版本、浏览器内核等信息
    
         log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                           '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
    
    	
         开启从磁盘直接到网络的文件传输,适用于有大文件上传下载的情况,提高IO效率。
         sendfile        on;
     
       
         一个请求完成之后还要保持连接多久, 默认为0,表示完成请求后直接关闭连接。
         keepalive_timeout  0;
         keepalive_timeout  65;
     
     
    	
         开启或者关闭gzip模块
         gzip  on ;
    
         设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
         gzip_min_lenth 1k;
    
          gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢 (传输快但比较消耗cpu )
         gzip_comp_level 4;
    
         匹配MIME类型进行压缩, (无论是否指定 )"text/html"类型总是会被压缩的。
         gzip_types types text/plain text/css application/json  application/x-javascript text/xml  
    
     
    
         动静分离
         服务器端静态资源缓存,最大缓存到内存中的文件,不活跃期限
         open_file_cache max=655350 inactive=20s;   
       
         活跃期限内最少使用的次数,否则视为不活跃。
         open_file_cache_min_uses 2;
    
         验证缓存是否活跃的时间间隔
         open_file_cache_valid 30s;
    
    
        
        upstream myserver{
    
          1、轮询 (默认 )
          每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
          2、指定权重
          指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
          3、IP绑定 ip_hash
          每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
          4、备机方式 backup
          正常情况不访问设定为backup的备机,只有当所有非备机全都宕机的情况下,服务才会进备机。
          5、fair (第三方 )
          按后端服务器的响应时间来分配请求,响应时间短的优先分配。   
          6、url_hash (第三方 )
          按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
    
           ip_hash;
                 server 192.168.161.132:8080 weight=1;
                 server 192.168.161.132:8081 weight=1;
          
           fair
    
           hash $request_uri
           hash_method crc32
          
          }
    
        server {
            # 监听端口号
            listen       80;
            # 服务名 不要加http:// 不要出现/符号
            server_name  192.168.161.130;
            # 允许在标头中使用特殊字符
            ignore_invalid_headers off;
            # 允许任何大小的文件上传。设置为1000m等值;将文件大小限制为特定
            client_max_body_size 1000m;
            # 禁用缓冲
            proxy_buffering off;
             # 字符集
            charset utf-8;
            
    	 location [=|~|~*|^~] /uri/ { … }   
    	  = 精确匹配
    	  ~ 正则匹配,区分大小写
    	  ~* 正则匹配,不区分大小写
    	  ^~  关闭正则匹配
    	
    	 匹配原则:
    	 
    	  1、所有匹配分两个阶段,第一个叫普通匹配,第二个叫正则匹配。
    	  2、普通匹配,首先通过“=”来匹配完全精确的location
                2.1、 如果没有精确匹配到, 那么按照最大前缀匹配的原则,来匹配location
                2.2、 如果匹配到的location有^~,则以此location为匹配最终结果,如果没有那么会把匹配的结果暂存,继续进行正则匹配。
          3、正则匹配,依次从上到下匹配前缀是~或~*的location, 一旦匹配成功一次,则立刻以此location为准,不再向下继续进行正则匹配。
          4、如果正则匹配都不成功,则继续使用之前暂存的普通匹配成功的location.
    
           location /api/ {
            #设置反向代理需要的请求头,比如用户IP、host等信息,这些头信息将被传递给后端服务器    
                # proxy_set_header 语法定义
                # 其中这个X-real-ip是一个自定义的变量名,名字可以随意取,这样做完之后,用户的真实ip就被放在X-real-ip这个变量里了,
                # 然后,在web端可以这样获取:request.getAttribute("X-real-ip"),获取用户真实IP的方式有很多,下面会介绍。    
            proxy_set_header X-Real_IP $remote_addr;
            #其中$host不带端口的,也就是nginx部署的主机ip,而$http_host是带端口的
            proxy_set_header Host $host;
            #设置转发请求的HTTP Header 中的X-Forwarded-For字段
                # $proxy_add_x_forwarded_for是一个内置变量,它表示将当前请求的remote_addr添加到X-Forwarded-For中。
            proxy_set_header X_Forward_For $proxy_add_x_forwarded_for;    
            # 反向代理到http://myserver    
            proxy_pass http://myserver;
    	    # 反向代理的超时时间
            proxy_connect_timeout 10;
            # 重定向 可重复  
            proxy_redirect default;    
            proxy_redirect http://$host/ http://$http_host/; 
            #  ^/api/(.*)$  这个是正则表达式,匹配所有以/api/开头的请求路径,(.*) 表示捕获请求路径中的所有内容到一个变量$1中
            # /app/$1 重写后的路径,使用了$1变量引用正则表达式捕获的内容 break 不再继续应用其他rewrite规则
            rewrite ^/api/(.*)$ /app/$1 break;    
            }
    
    
            location / {     匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
    	     定义服务器的默认网站根目录位置
                root   html;
    	        默认访问首页索引文件的名称
    	        index  index.html index.htm;
             }
    
             location  /images/ {
    	      /static/images/1.jpg
    	        root /static ;
    	     }
    
    	     location ^~ /images/jpg/ {    匹配任何已 /images/jpg/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。 
    	        root images/jpg/ ;
    	     }
             location ~*.(gif|jpg|jpeg)$ { 
    	       所有静态文件直接读取硬盘
                root pic ;
    	       expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力
                  expires 3d;  缓存3天
             }
    
    
             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;
            }
        }
    }
    
    
    • 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
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    nginx内置变量
    
    1. 请求
        - $args  (参数 )
        - $query_string  (请求参数 )
        - $is_args  (参数判断 )
        - $arg_PARAMETER
        - $request  (客户端请求 )
        - $request_body  (客户端请求的报文体 )
        - $request_body_file
        - $request_filename  (请求文件路径 )
        - $request_method  (请求的方法 )
        - $uri  (请求URI )
        - $request_uri  (请求的URI,带参数 )
    
    2. 客户端信息
        - $remote_addr  (客户端IP地址 )
        - $binary_remote_addr  (客户端地址的二进制表示 )
        - $remote_port  (客户端端口号 )
        - $remote_user  (客户端用户名 )
        - $http_cookie  (客户端的cookie信息 )
        - $http_user_agent  (客户端代理信息 )
    
    3. 服务器信息
        - $scheme  (使用协议 )
        - $server_addr  (服务器地址 )
        - $server_port  (服务器端口号 )
        - $server_protocol  (请求的协议版本 )
        - $document_uri  (请求的URI )
        - $document_root  (root路径 )
        - $limit_rate  (速率的限制 )
        - $nginx_version  (nginx版本 )
        - $pid  (主进程的进程ID )
    
    4. Headers
        - $host (请求信息中的Host头域值,如果请求中没有则取访问服务器的IP)
        - $content_type  (请求信息里的Content-Type字段 )
        - $http_x_forwarded_for  (访问真实IP路径 )
        - $content_length  (Content-Length字段 )
        - $http_HEADER  (header中指定字段 )
        - $http_referer  (引用地址 )
        - $http_via (最后一个访问服务器的IP地址)
        - 其他header 的字段
      
    
    • 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

    可以自己启动nginx服务,通过修改配置来测试,测试配置如下

    location = /test {
        add_header  Content-Type 'text/html; charset=utf-8';
        return 200 "
            args = $args
    binary_remote_addr = $binary_remote_addr
    body_bytes_sent = $body_bytes_sent
    content_length = $content_length
    content_type= $content_type
    document_root = $document_root
    document_uri = $document_uri
    host = $host
    hostname = $hostname
    http_user_agent = $http_user_agent
    is_args = $is_args
    limit_rate = $limit_rate
    nginx_version = $nginx_version
    query_string = $query_string
    remote_addr = $remote_addr
    remote_port = $remote_port
    request_filename = $request_filename
    request_body = $request_body
    request_body_file = $request_body_file
    request_completion = $request_completion
    request_method = $request_method
    request_uri = $request_uri
    scheme = $scheme
    server_addr = $server_addr
    server_name = $server_name
    server_port = $server_port
    server_protocol = $server_protocol
    "; }
    • 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

    响应

    args = a=1&b=2&c=3
    binary_remote_addr = s�i�
    body_bytes_sent = 0
    content_length = 
    content_type= 
    document_root = /usr/share/nginx/html
    document_uri = /test
    host = www.chenshuang.work
    hostname = vm_248_128_centos
    http_user_agent = Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.170 Safari/537.36
    is_args = ?
    limit_rate = 0
    nginx_version = 1.12.2
    query_string = a=1&b=2&c=3
    remote_addr = 101.43.59.221
    remote_port = 8888
    request_filename = /usr/share/nginx/html/variable
    request_body = 
    request_body_file = 
    request_completion = 
    request_method = GET
    request_uri = /test?a=1&b=2&c=3
    scheme = http
    server_addr = 101.43.59.221
    server_name = www.chenshuang.work
    server_port = 80
    server_protocol = HTTP/1.1
    
    • 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
  • 相关阅读:
    【机器学习】随机种子Random Seed介绍(在Python、Pytorch、TensorFlow中的设置代码汇总)
    Git常用命令
    觉非科技发布【轻地图高速NOA智驾方案】|地平线,觉非科技,MobileDrive超捷生态协作实现技术落地
    Java面向对象笔记
    【Verilog刷题篇】硬件工程师从0到入门2|组合逻辑
    springboot整合redis
    01-初识HTML和CSS
    Copyleaks:AI抄袭和内容检测工具
    猿创征文 |【算法入门必刷】数据结构-栈(六)
    SpringMVC | 快速上手SpringMVC
  • 原文地址:https://blog.csdn.net/weixin_51216079/article/details/133355152