Linux学习107 nginx实现web服务配置

一、nginx续

  1、main相关的配置

    f、http协议相关的配置

      (4)、定义客户端请求的相关配置

        1)、keepalive_timeout timeout [header_timeout];

          设定保持连接的超时时长,0表示禁止长连接;默认为75s。这个75秒有点长了

        2)、keepalive_requests number

          在一次长连接上所允许请求的资源的最大数量,默认为100,这个值比较合理

        3)、keepalive_disable none | browser ...;

          对哪种浏览器禁用长连接。这种用的比较少一点。

        4)、send_timeout time;

          向客户端发送响应报文的超时时长,此处,是指两次写操作之间的间隔时长

        5)、client_body_buffer_size size;

          用于接收客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由client_body_temp_path指令所定义的位置。客户端使用上传,即post或put才会使用到client_body_buffer_size。

        6)、client_body_temp_path path[level1  [level2 [level3]]]

          设定用于存储客户端请求报文的body部分的临时存储路径及子目录结构和数量。因为加入我们nginx有上万并发的话我们的文件量就大的多了,为了能够便于文件查找我们都将其放在一个平面目录上,进行分级存放,比如我们在一个目录下创建16个一级子目录,因此我们文件应该会分散在这16个一级子目录下,因此我们只需要能路由到一级子目录中去就行,然后整个查找范围就缩小了16倍,如果我们在一级子目录下再创建256个子目录,意味着又缩小了256倍,即总体查找速度又缩小到1/16 * 1/256了,如果三级子目录又来16个,那就又缩小了16倍,因此假如有500万个文件我们查找速度就非常快了。那么我们怎么能够把其放在子目录下下次还知道去哪儿找呢?即我们怎么做路径路由呢?很简单,即把一个文件对应的url,大家知道我们用户请求时无论是上传下载都要请求路径的url,即http://host:port/path做一次hash计算

          16进制的数字

          client_body_temp_path path /var/tmp/client_body 2 1 1

            1:表示用一位16进制数字表示一级子目录:0-f

            2:表示用2位16进制数字表示二级子目录:00-ff

            2:表示用2位16进制数字表示三级子目录:00-ff

      (5)、对客户端进行限制的相关配置

        1)、limit_rate rate;

          限制响应给客户端的传输速率,单位是bytes/second,0表示无限制

        2)、limit_except method ... {...}

          限制对指定的请求方法之外的其它方法的使用客户端

          limit_except GET { #get方法以外的方法只允许192.168.10.0/24这个网段的主机中使用,其它网段的都不允许

                allow 192.168.10.0/24;

                deny all;

                  }

      (6)、文件操作优化的配置

        1)、alo on | off | threads[=pool];

          是否启用aio功能

        2)、directio size | off

          在Linux主机启用O_DIRECT标记,此处意味文件大于等于给定的大小时使用,例如directio 4m

        3)、open_file_cache off;

          open_file_cache max=N [inactive=time];

          nginx可以缓存以下三种信息:

            i、文件的描述符,文件大小和最近一次的修改时间

            ii、打开的目录结构

            iii、没有找到的或者没有权限访问的文件的相关信息

          max=N:可缓存的缓存项上限;达到上限后会使用LRU(最近最少使用)算法实现缓存管理;

          inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_user指令所指定的次数的缓存项即为非活动项

        4)、open_file_cache_valid time

          缓存项有效性的检查频率;默认为60s

        5)、open_file_cache_min_users number

          在 open_file_cache指令的inactive参数指定的时长内,至少应该被命中多少次方可被归类为活动项

        6)、open_file_cache_errors on | off

          是否缓存查找发生错误的文件一类的信息

  2、ngx_http_access_module模块,即基于IP地址的访问控制功能

    a、allow address | CIDR | unix: | all;

    b、deny address | CIDR | unix: | all;

      http,server,location,limit_except

  3、ngx_http_auth_basic_module模块

    a、实现基于用户的访问控制,使用basic机制进行用户认证

    b、auth_basic string | off;

    c、auth_basic_user_file file;

      location /admin/ {

        alias /webapps/app1/data/;

        auth_basic "Admin Area"; #auth_basic表示做什么用

        auth_basic_user_file /etc/nginx/.ngxpasswd;

              }

    d、注意:htpasswd命令由httpd-tools所提供

    e、我们来配置认证

      (1)、首先安装httpd-tools

        yum install -y httpd-tools

      (2)、创建相应的用户以及密码。(第一次添加用户的时候需要使用-c)

[root@www conf.d]# htpasswd -c -m /etc/nginx/.ngxpasswd tom
New password: 
Re-type new password: 
Adding password for user tom
[root@www conf.d]# htpasswd -m /etc/nginx/.ngxpasswd jerry
New password: 
Re-type new password: 
Adding password for user jerry

      (3)、编辑配置文件

[root@www /]# cat /etc/nginx/conf.d/vhost1.conf
server {
    listen 80;
    server_name www.wohaoshuai1.com;
    root /data/nginx/vhost1;
    location / {
        #root /data/nginx/vhost2; 
        allow all;
}
    location ^* ^/(admin|login) {
        auth_basic "admin area or login url";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
} 

    location ~*.(jpg|png)$ {
        deny 192.168.10.14;
        allow all;
}
    location ^~ /images/ {
        alias /data/pictures/;
}
    error_page 404 =200 /notfound.html;
    location = /notfound.html {
        root /data/nginx/error_pages;
}
}

[root@www /]# mkdir -p /data/nginx/vhost1/admin
[root@www /]# vim /data/nginx/vhost1/admin/index.html
[root@www /]# cat /data/nginx/vhost1/admin/index.html
</h1>
    Admin Area
</h1>
[root@www /]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www /]# nginx -s reload

        然后我们访问链接http://www.wohaoshuai1.com/admin/即可弹出相应的对话框。

  4、ngx_http_stub_status_module模块

    a、用于输出nginx的基本状态信息

      Active connections:291

      server accepts handled requests

        16630948 16630948 31070465

      Reading:6 Writing: 179 Waiting:106

      Active connections:活动状态的连接数

      accepts:已经接受的客户端请求的总数

      handled:已经处理完成的客户端请求的总数

      requests:客户端发来的总的请求数

      Reading:处于读取客户端请求报文首部的连接的连接数

      Writing:处于向客户端发送响应报文过程中的连接数

      Waiting:处于等待客户端发出请求的空间连接数

    b、stub_status

    c、配置示例:

      location /basic_status{

        stub_status;

      }

    d、示例

[root@www /]# cat /etc/nginx/conf.d/vhost1.conf
server {
    listen 80;
    server_name www.wohaoshuai1.com;
    root /data/nginx/vhost1;
    location / {
        #root /data/nginx/vhost2; 
        allow all;
}
    location ~* ^/(admin|login) {
        auth_basic "admin area or login url";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
} 

    location ~*.(jpg|png)$ {
        deny 192.168.10.14;
        allow all;
}
    location ^~ /images/ {
        alias /data/pictures/;
}
    error_page 404 =200 /notfound.html;
    location = /notfound.html {
        root /data/nginx/error_pages;
}
    location /ngxstatus { #最好把auth_basic也写上,因为不能随意让其他人访问
        stub_status;
}
}
[root@www /]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www /]# nginx -s reload

      然后访问http://www.wohaoshuai1.com/ngxstatus连接即可访问到状态页

      我们使用ab -n 100000 -c 100 http://192.168.10.13/命令进行压测可以发现相应的变化。 

  5、ngx_http_log_module模块

    a、he ngx_http_log_module module writes request logs in the specifled format

    b、log_format name string ...;

      string可以使用nginx核心模块及其它模块内嵌的变量

      课外作业:为ngnx定义使用类似于httpd的combined格式的访问日志

    c、access_log path [format [buffer=size] [gzip[level]]] [flush=time] [if=condition]

      access_log off;关闭访问日志 

      访问日志文件路径,格式及相关的缓冲的配置

        buffer=size :定义缓冲区大小,

        flush=time:刷新时间

      (1)、我们配置自定义日志文件

[root@www /]# cat /etc/nginx/conf.d/vhost1.conf
server {
    listen 80;
    server_name www.wohaoshuai1.com;
    root /data/nginx/vhost1;
    access_log /var/log/nginx/vhost1_access.log main; #这个main是主配置文件/etc/nginx/nginx.conf中定义的main类日志格式
    location / {
        #root /data/nginx/vhost2; 
        allow all;
}
    location ~* ^/(admin|login) {
        auth_basic "admin area or login url";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
} 

    location ~*.(jpg|png)$ {
        deny 192.168.10.14;
        allow all;
}
    location ^~ /images/ {
        alias /data/pictures/;
}
    error_page 404 =200 /notfound.html;
    location = /notfound.html {
        root /data/nginx/error_pages;
}
    location /ngxstatus { #最好把auth_basic也写上,因为不能随意让其他人访问
        stub_status;
}
}
[root@www /]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@www /]# nginx -s reload
[root@www /]# curl www.wohaoshuai1.com
index
[root@www /]# cat /var/log/nginx/vhost1_access.log 
192.168.10.13 - - [17/Jun/2020:17:03:27 +0800] "GET / HTTP/1.1" 200 6 "-" "curl/7.29.0" "-"
[root@www /]# 

      (2)、我们配置状态页不记录日志

[root@www /]# cat /etc/nginx/conf.d/vhost1.conf
server {
    listen 80;
    server_name www.wohaoshuai1.com;
    root /data/nginx/vhost1;
    access_log /var/log/nginx/vhost1_access.log main; #这个main是主配置文件/etc/nginx/nginx.conf中定义的main类日志格式
    location / {
        #root /data/nginx/vhost2; 
        allow all;
}
    location ~* ^/(admin|login) {
        auth_basic "admin area or login url";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
} 

    location ~*.(jpg|png)$ {
        deny 192.168.10.14;
        allow all;
}
    location ^~ /images/ {
        alias /data/pictures/;
}
    error_page 404 =200 /notfound.html;
    location = /notfound.html {
        root /data/nginx/error_pages;
}
    location /ngxstatus { #最好把auth_basic也写上,因为不能随意让其他人访问
        stub_status;
        access_log off;
}
}
[root@www /]# nginx -s reload

      (3)、我们可以在相应的location下自己定义对应的日志记录文件

[root@www /]# cat /etc/nginx/conf.d/vhost1.conf
server {
    listen 80;
    server_name www.wohaoshuai1.com;
    root /data/nginx/vhost1;
    access_log /var/log/nginx/vhost1_access.log main; #这个main是主配置文件/etc/nginx/nginx.conf中定义的main类日志格式
    location / {
        #root /data/nginx/vhost2; 
        allow all;
}
    location ~* ^/(admin|login) {
        auth_basic "admin area or login url";
        auth_basic_user_file /etc/nginx/.ngxpasswd;
        access_log /var/log/nginx/vhost1_admin_access.log main;
} 

    location ~*.(jpg|png)$ {
        deny 192.168.10.14;
        allow all;
}
    location ^~ /images/ {
        alias /data/pictures/;
}
    error_page 404 =200 /notfound.html;
    location = /notfound.html {
        root /data/nginx/error_pages;
}
    location /ngxstatus { #最好把auth_basic也写上,因为不能随意让其他人访问
        stub_status;
        access_log off;
}
}
[root@www /]# nginx -s reload

    d、open_log_file_caceh max=N [inactive=time] [min_uses=N] [valid=time];

      open_log_file_cache off;

      缓存各日志文件相关的元数据信息

      max:缓存的最大文件描述符数量

      min_uses:在inactive指定的时长内访问大于等于此值方可被当做活动项

      inactive:非活动时长

      valid:验证缓存中各缓存项是否为活动项的时间间隔

      即在inactive这么长的时间内访问次数小于min_uses的就标记为非活动项而且每隔valid这么长时间就起来检查一次,最多缓存max这么多个,一旦超出了还要再缓存的话就使用LRU算法。

      注意这只是缓存日志文件元数据的不是缓存日志文件数据内容的。

  6、ngx_http_gzip_module

    The ngx_http_gzip_module module is a filter that compresses using the "gzip" method,This often helps to reduce the size of transmltted data by half or even more

    a、gzip on | off

      Enables or disables gzipping of responses

    b、gzip_comp_level level;压缩级别,默认为1

      Sets a gzip compression level of a response,Acceptable values are in the range from 1 to 9

    c、gzip_disable regex ... :对我们正则匹配到的浏览器禁止gzip压缩

      Disables gzipping of responses for requests with "User-Agent" header fields matching any of the specified regurlar expressions

    d、gzip_min_length length;

      启用压缩功能的响应报文大小阈值。默认为20个字节。即小于20个字节就不压缩,大于20个字节才压缩。

    e、gzip_buffers number size;

      支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小。即我们启动多少个内存段每一段有多大来完成压缩功能

      

      图中表示启动32段每一段4k或者启动16段,每段8k。

    f、gzip_proxled off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth |any;

      nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的

      off:对代理的请求不启用

      no-cache,no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能

    g、gzip_types mime-type ...

      压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能

    h、示例:

      在http中定义

[root@www /]# cat /etc/nginx/nginx.conf |grep gzip
    gzip on;
    gzip_comp_level 6; 
    gzip_types text/css text/xml application/javascript; #可以在文件/etc/nginx/mime.type中查看

        然后我们访问一个大一点的配置文件我们就能访问到了。

原文地址:https://www.cnblogs.com/Presley-lpc/p/13151424.html