pursue wind pursue wind
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
首页
Java
Python
数据库
框架
Linux
中间件
前端
计算机基础
DevOps
项目
面试
书
关于
归档
MacOS🤣 (opens new window)
GitHub (opens new window)
  • nginx
    • Nginx 简介
      • 什么是 Nginx
      • Nginx 的应用场景
    • Nginx 虚拟主机
      • 什么是虚拟主机?
      • Nginx 配置文件的结构
      • 基于端口的虚拟主机配置
      • 需求
      • 创建目录及文件
      • 配置虚拟主机
      • 基于域名的虚拟主机配置
      • 需求
      • 配置 Windows Hosts 文件
      • 创建目录及文件
      • 配置虚拟主机
    • Nginx 反向代理
      • 什么是代理服务器?
      • 为什么要使用代理服务器?
      • 提高访问速度
      • 防火墙作用
      • 通过代理服务器访问不能访问的目标站点
      • 什么是正向代理?
      • 什么是反向代理?
      • 反向代理有哪些主要应用?
      • 使用 Nginx 反向代理 Tomcat
      • 需求
      • 启动 Tomcat 容器
      • 配置 Nginx 反向代理
    • Nginx 负载均衡
      • 什么是负载均衡
      • Nginx 实现负载均衡
      • Nginx 配置负载均衡
      • 相关配置说明
    • Nginx 配置说明
      • 配置文件区域
      • 核心功能配置
      • HTTP 核心配置
  • 处理Vim中粘贴文本的格式问题
  • 基础设施
  • Awk文本处理经典案例
  • CentOS 更换 yum 源
  • Linux
pursuewind
2020-11-22
目录

nginx

# Nginx 简介

# 什么是 Nginx

Nginx 是一款高性能的 HTTP 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师 Igor Sysoev 所开发,官方测试 Nginx 能够支支撑 5 万并发链接,并且 CPU、内存等资源消耗却非常低,运行非常稳定。

# Nginx 的应用场景

  • HTTP 服务器:Nginx 是一个 HTTP 服务可以独立提供 HTTP 服务。可以做网页静态服务器。
  • 虚拟主机:可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。
  • 反向代理,负载均衡:当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用 Nginx 做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

# Nginx 虚拟主机

我们使用 Docker 来安装和运行 Nginx,docker-compose.yml 配置如下:

version: '3.1'
services:
  nginx:
    restart: always
    image: nginx
    container_name: nginx
    ports:
      - 81:80
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - ./wwwroot:/usr/share/nginx/wwwroot
1
2
3
4
5
6
7
8
9
10
11

# 什么是虚拟主机?

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供 www 服务,这样就可以实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。

通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置

  • 基于 IP 的虚拟主机
  • 基于域名的虚拟主机
  • 基于端口的虚拟主机

# Nginx 配置文件的结构

# ...
events {
    # ...
}

http {
    # ...
    server{
        # ...
    }

    # ...
    server{
        # ...
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

注: 每个 server 就是一个虚拟主机

# 基于端口的虚拟主机配置

# 需求

  • Nginx 对外提供 80 和 8080 两个端口监听服务
  • 请求 80 端口则请求 html80 目录下的 html
  • 请求 8080 端口则请求 html8080 目录下的 html

# 创建目录及文件

在 /usr/local/docker/nginx/wwwroot 目录下创建 html80 和 html8080 两个目录,并分辨创建两个 index.html 文件

# 配置虚拟主机

修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:

worker_processes  1;	#cpu内核数	老版本有惊群问题

events {
    worker_connections  1024;	#每个子线程的连接数
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    # 配置虚拟主机 192.168.75.145
    server {
    # 监听的ip和端口,配置 192.168.75.145:80
        listen       80;
    # 虚拟主机名称这里配置ip地址
        server_name  192.168.75.145;
    # 所有的请求都以 / 开始,所有的请求都可以匹配此 location
        location / {
        # 使用 root 指令指定虚拟主机目录即网页存放目录
        # 比如访问 http://ip/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/index.html
        # 比如访问 http://ip/item/index.html 将找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html

            root   /usr/share/nginx/wwwroot/html80;
        # 指定欢迎页面,按从左到右顺序查找
            index  index.html index.htm;
        }

    }
    # 配置虚拟主机 192.168.75.245
    server {
        listen       8080;
        server_name  192.168.75.145;

        location / {
            root   /usr/share/nginx/wwwroot/html8080;
            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

# 基于域名的虚拟主机配置

# 需求

  • 两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容
  • 两个域名是 admin.service.itoken.pursue.pub 和 admin.web.itoken.pursue.pub
  • Nginx 服务器使用虚拟机 192.168.75.145

# 配置 Windows Hosts 文件

  • 通过 host 文件指定 admin.service.itoken.pursue.pub 和 admin.web.itoken.pursue.pub 对应 192.168.75.145 虚拟机:
  • 修改 window 的 hosts 文件:(C:\Windows\System32\drivers\etc)

# 创建目录及文件

在 /usr/local/docker/nginx/wwwroot 目录下创建 htmlservice 和 htmlweb 两个目录,并分辨创建两个 index.html 文件

# 配置虚拟主机

user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    server {
        listen       80;
        server_name  admin.service.itoken.pursue.pub;
        location / {
            root   /usr/share/nginx/wwwroot/htmlservice;
            index  index.html index.htm;
        }

    }

    server {
        listen       80;
        server_name  admin.web.itoken.pursue.pub;

        location / {
            root   /usr/share/nginx/wwwroot/htmlweb;
            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

# Nginx 反向代理

# 什么是代理服务器?

代理服务器,客户机在发送请求时,不会直接发送给目的主机,而是先发送给代理服务器,代理服务接受客户机请求之后,再向主机发出,并接收目的主机返回的数据,存放在代理服务器的硬盘中,再发送给客户机。

img

# 为什么要使用代理服务器?

# 提高访问速度

由于目标主机返回的数据会存放在代理服务器的硬盘中,因此下一次客户再访问相同的站点数据时,会直接从代理服务器的硬盘中读取,起到了缓存的作用,尤其对于热门站点能明显提高请求速度。

# 防火墙作用

由于所有的客户机请求都必须通过代理服务器访问远程站点,因此可在代理服务器上设限,过滤某些不安全信息。

# 通过代理服务器访问不能访问的目标站点

互联网上有许多开放的代理服务器,客户机在访问受限时,可通过不受限的代理服务器访问目标站点,通俗说,我们使用的翻墙浏览器就是利用了代理服务器,虽然不能出国,但也可直接访问外网。

# 什么是正向代理?

正向代理,架设在客户机与目标主机之间,只用于代理内部网络对 Internet 的连接请求,客户机必须指定代理服务器,并将本来要直接发送到 Web 服务器上的 Http 请求发送到代理服务器中。

img

# 什么是反向代理?

反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器。

img

# 反向代理有哪些主要应用?

现在许多大型 web 网站都用到反向代理。除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,还可以进行负载均衡,将用户请求分配给多个服务器。

# 使用 Nginx 反向代理 Tomcat

# 需求

  • 两个 tomcat 服务通过 nginx 反向代理
  • nginx 服务器:192.168.75.145:80
  • tomcat1 服务器:192.168.75.145:9090
  • tomcat2 服务器:192.168.75.145:9091

# 启动 Tomcat 容器

启动两个 Tomcat 容器,映射端口为 9090 和 9091,docker-compose.yml 如下:

version: '3'
services:
  tomcat1:
    image: tomcat
    container_name: tomcat1
    ports:
      - 9090:8080

  tomcat2:
    image: tomcat
    container_name: tomcat2
    ports:
      - 9091:8080
1
2
3
4
5
6
7
8
9
10
11
12
13

# 配置 Nginx 反向代理

修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:

user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    # 配置一个代理即 tomcat1 服务器
    upstream tomcat_server1 {
        server 192.168.75.145:9090;
    }

    # 配置一个代理即 tomcat2 服务器
    upstream tomcat_server2 {
        server 192.168.75.145:9091;
    }

    # 配置一个虚拟主机
    server {
        listen 80;
        server_name admin.service.itoken.pursue.pub;
        location / {
                # 域名 admin.service.itoken.pursue.pub 的请求全部转发到 tomcat_server1 即 tomcat1 服务上
                proxy_pass http://tomcat_server1;
                # 欢迎页面,按照从左到右的顺序查找页面
                index index.jsp index.html index.htm;
        }
    }

    server {
        listen 80;
        server_name admin.web.itoken.pursue.pub;

        location / {
            # 域名 admin.web.itoken.pursue.pub 的请求全部转发到 tomcat_server2 即 tomcat2 服务上
            proxy_pass http://tomcat_server2;
            index index.jsp 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

# Nginx 负载均衡

# 什么是负载均衡

负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

负载均衡,英文名称为 Load Balance,其意思就是分摊到多个操作单元上进行执行,例如 Web 服务器、FTP 服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

# Nginx 实现负载均衡

  • nginx 作为负载均衡服务器,用户请求先到达 nginx,再由 nginx 根据负载配置将请求转发至 tomcat 服务器
  • nginx 负载均衡服务器:192.168.75.145:80
  • tomcat1 服务器:192.168.75.145:9090
  • tomcat2 服务器:192.168.75.145:9091

# Nginx 配置负载均衡

修改 /usr/local/docker/nginx/conf 目录下的 nginx.conf 配置文件:

user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream myapp1 {
        server 192.168.75.145:9090 weight=10;
        server 192.168.75.145:9091 weight=10;
    }

    server {
        listen 80;
        server_name nginx.pursue.pub;
        location / {
            proxy_pass http://myapp1;
            index index.jsp 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

# 相关配置说明

# 定义负载均衡设备的 Ip及设备状态 
upstream myServer {
    server 127.0.0.1:9090 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6060;
    server 127.0.0.1:7070 backup;
}
1
2
3
4
5
6
7

在需要使用负载的 Server 节点下添加

proxy_pass http://myServer;
1
  • upstream:每个设备的状态:
  • down:表示当前的 server 暂时不参与负载
  • weight:默认为 1 weight 越大,负载的权重就越大。
  • max_fails:允许请求失败的次数默认为 1 当超过最大次数时,返回 proxy_next_upstream 模块定义的错误
  • fail_timeout:max_fails 次失败后,暂停的时间。
  • backup:其它所有的非 backup 机器 down 或者忙的时候,请求 backup 机器。所以这台机器压力会最轻

# Nginx 配置说明

# 配置文件区域

Nginx 主要配置文件 nginx.conf,里面主要包括以下几个配置区域,如下表:

配置区域 说明
main 配置影响 nginx 全局的指令。一般有运行 nginx 服务器的用户组,nginx 进程 pid 存放路径,日志存放路径,配置文件引入,允许生成 worker process 数等。
events 配置影响 nginx 服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
http 可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type 定义,日志自定义,是否使用 sendfile 传输文件,连接超时时间,单连接请求数等。
upstream 配置 HTTP 负载均衡器分配流量到几个应用程序服务器。
server 配置虚拟主机的相关参数,一个 http 中可以有多个 server。
location 配置请求的路由,以及允许根据用户请求的URI来匹配指定的各 location 以进行访问配置;匹配到时,将被 location 块中的配置所处理

# 核心功能配置

nginx 核心功能配置主要是 main 和 events 的顶层全局配置,都是配置 nginx 核心模块(ngx_core_module),管理服务器级别的行为。下表包含是大部分常用的配置选项,更多配置请参考官方文档:http://nginx.org/en/docs/ngx_core_module.html (opens new window)

配置类别 配置选项 上下文 语法 默认值 功能描述
基本配置 user main user username [groupname] nobody 以那个用户身份运行,以在configure指定的用户为准
pid main pid /path/to/pid_filename nginx.pid 指定nginx的pid文件
worker_rlimit_nofile main 受linux内核文件描述符数量限制 指定一个worker进程所能够打开的句柄数。因为Linux对每个进程所能打开的文件描述数量是有限制的,默认一般是1024个,可通过ulimit -n FILECNT或/etc/securit/limits.conf配置修改linux默认能打开的文件句柄数限制。建议值为:系统最大数量/进程数。但进程间工作量并不是平均分配的,所以可以设置在大一些。推荐值为:655350。
优化性能相关配置 worker_procrsses main worker_processes number | auto; 1 **work进程的个数.**如果负载以CPU密集型应用为主,一般会设置与机器cpu核数一致或少一个(用来处理用户等其他任务),如果负载以IO密集型为主,如响应大量内容给客户端,则worker数应该为CPU个数的1.5或2倍。因为更多的worker数,只会导致进程来竞争cpu资源了,从而带来不必要的上下文切换。而且,nginx为了更好的利用多核特性,具有cpu绑定选项,我们可以将某一个进程绑定在某一个核上,这样就不会因为进程的切换带来cache的失效。
worker_cpu_affinity main worker_cpu_affinity cpumask …; 无,不绑定 **将工作进程绑定到特定的CPU上,减少CPU在进程之间切换的开销。用二进制bit位表示进程绑定在哪个CPU内核。如4工作进程4内核:worker_processes 4;****worker_cpu_affinity 0001 0010 0100 1000;****2工作进程4内核:****worker_processes 2;**worker_cpu_affinity 0101 1010;
worker_priority main worker_priority number; 0 工作进程调度优先级,-20到19之间的值,值越小越优先调用。如果系统同时运行多个任务,你可能需要提高nginx的工作进程的优先级
timer_resolution main timer_resolution interval; 无 每次内核事件调用返回时,都会使用gettimeday()来更新nginx缓存时钟;timer_resolution用于定义每隔多久才会由gettimeday()更新一次缓存时钟;x86-64系统上,gettimeday()代价已经很小,可以忽略此配置
ssl_engine main ssl_engine device; 无 在存在ssl硬件加速器的服务器上,指定所使用的ssl硬件加速设备。由于https链接所消耗的资源比http大得多,可能要多消耗5、6倍,最好有专门处理ssl的硬件设备
事件相关配置 worker_commections events worker_connections number; 512 每个worker能够并发响应的最大请求数。系统每处理一个请求就要消耗一个套接字文件,如果为代理服务器的话,worker_rlimit_nofile=worker_commections*2
use events use method; 无,自动选择 **指定使用哪种模型(select/poll/epoll),建议让nginx自动选择,**linux内核2.6以上一般能使用epoll,提高性能。
accept_mutex events accept_mutex on | off; Off(1.11.3版本前默认on) **是否打开nginx的accept锁;此锁能够让多个worker进行轮流地、序列化地与新的客户端建立连接;而通常当一个worker进程的负载达到其上限的7/8,master就尽可能不将请求调度至worker.**1.11.3版本epoll支持EPOLLEXCLUSIVE 标记,不再有惊群问题 。
accept_mutex_delay events accept_mutex_delay time; 500ms 使用accept锁以后,只有一个worker能取得锁,一个worker进程为取得accept锁的等待时长,即用户建立等待的时间,如果某worker进程在某次试图取得锁时失败了,至少要等待#ms才能在一次请求锁
multi_accept events multi_accept on | off; off 是否允许一次性地响应多个用户请求
调试、定位问题配置 daemon main daemon on | off; on nginx是否以守护进程运行,是否让nignx运行于后台;调试时应该为off,使得所有信息直接输出在控制台
master_process main master_process on | off; on 是否以master/worker模式运行nginx,默认为on,调试时可以设置为off以方便追踪
error_log main, http, mail, stream, server, location error_log file [level]; error_log logs/error.log error; 配置错误日志文件的路径和日志级别。日志级别有debug, info, notice, warn, error, crit, alert和emerg几种。调试时可以使用debug级别,但要求在编译时必须使用–with-debug启用debug功能,默认通常为error级别.

# HTTP 核心配置

http 功能核心配置主要是 http 块、server 块和 location 块的配置,包括 HTTP 核心模块(ngx_http_core_module)和一些扩展模块(如ngx_stream_ssl_module),提供管理 WEB 服务器级别的行为。

必须使用虚拟机来配置站点,每个虚拟主机使用一个 server{} 段来配置,非虚拟主机的配置和公共选项,需要定义在 server 之外,http 之内。

下表包含是大部分常用的配置选项,更多配置请参考官方文档:http://nginx.org/en/docs/ (opens new window)

配置类别 配置选项/模块 上下文 语法 默认值 功能描述
基本配置 http main http { … } 无 提供HTTP服务器配置上下文
server http server { … } 无 HTTP服务器的核心配置,定义一个虚拟主机:nginx支持使用基于主机名或IP的虚拟主机
listen server listen address[:port]listen protlisten unix:socket listen :80 | :8000 **配置虚拟主机监听的IP地址和端口,默认监听本机IP地址和80或8000端口。如果只设置了IP没设端口,默认使用80端口。如果只设置了端口,没设置IP默认使用本机IP。****后面可以指定一些参数:****default_server:定义此server为http中默认的server;如果所有的server中任何一个listen使用此参数,那么第一个server即为默认server;****rcvbuf=SIZE:接收缓存大小;****sndbuf=SIZE: 发送缓存大小;**ssl:https server:必须以ssl连接;
server_name server server_name name …; "" 配置虚拟主机的域名,可以指定多个,用空格分隔。默认为空。名称可以使用通配符和正则表达式(通常以~开头):当nginx收到一个请求时,会取出其首部的server的值,而后跟众server_name进行比较:比较方式(1) 先做精确匹配,如www.tjiyu.com**(2) 左侧通配符匹配,如tjiyu.com****(3) 右侧通配符匹配,如www.****(4) 正则表达式匹配**
server_name_hash_bucket_size server server_names_hash_bucket_size size; 32|64|128 **为了实现快速主机查找,nginx使用hash表来保存主机名。**默认值取决于处理器缓存线的大小。
location server, location **location [ = | ~ | ~ | ^~ ] uri { … }**location @name { … } 无 **允许根据用户请求的URI来匹配指定的各location以进行访问配置;匹配到时,将被location块中的配置所处理。=:精确匹配;~:正则表达式模式匹配,匹配时区分字符大小写;~:正则表达式模式匹配,匹配时忽略字符大小写;^~:只需要前半部分与uri匹配即可,不检查正则表达式;****匹配优先级:**字符字面量最精确匹配、正则表达式检索(由多个时,由第一个匹配到的所处理),按字符字面量。
资源路径定义配置 root http, server, location, if in location root path; root html; **设置web资源路径,用于指定请求的根文档目录,从根开始匹配。**如root /html/image/,请求"/tjiyu.gif"对应的文件为"/html/image/tjiyu.gif"
alias location alias path; 无 **指定路径别名,只能用于location中,从最后一个/开始匹配。****如location /i/ {alias /data/w3/images/;}**请求"/i/top.gif", 实际文件"/data/w3/images/top.gif"
Index http, server, location index file …; index index.html; **ngx_http_index_module.**定义默认页面,可以跟多个值。自左向右匹配。
error_page http, server, location, if in location error_page code … [=[response]] uri; 无 **ngx_http_core_module****当对于某个请求发回错误时,如果匹配上了error_page指令中设定的code,则从定向至新的URI中,错误重定向.****如 error_page 500 502 503 504 /50x.html;****也可以改变返回码。**如error_page 404 =200 /404.html;
try_files server, location **try_files file … uri;**try_files file … =code; 无 **自左向右尝试读取有path所指定路径,在第一找到即停止并返回,如果所有path均不存在,则返回最后一个uri或者code.**如try_files $uri $uri/index.html $uri.html =404;
网络连接相关设置 keepalive_timeout http, server, location keepalive_timeout timeout [header_timeout]; 75s **保持连接的超时时长,默认为75s。**降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值
keepalive_requests http, server, location keepalive_requests number; 100 在一次长连接上允许承载的最大请求数。
keepalive_disable http, server, location keepalive_disable none | browser …; msie6(ie6无法长连接) 对指定的浏览器禁止使用长连接。
tcp_nodelay http, server, location tcp_nodelay on | off; on 这里指ngx_http_core_module模块的选项。对keepalive连接是否使用tcp_nodelay选项启动配置,会在数据包达到一定大小后再发送数据。这样会减少网络通信次数,降低阻塞概率,但也会影响响应及时性。比较适合于文件下载这类的大数据通信场景。
client_header_timeout http, server client_header_timeout time; 60s **读取http请求首部的超时时长。**如果客户端在此时间内未传输整个头,则会向客户端返回408(请求超时)错误。
client_body_timeout http, server, location client_body_timeout time; 60s 读取http请求包体的超时时间。
send_timeout http, server, location send_timeout time; 60s **发送响应的超时时长。**超时后连接将关闭。
对客户端请求的限制配置 limit_except location limit_except method … { … } on **指定范围之外的其他方法的访问控制。****方法有:GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH.****如只允许GET访问:****limit_except GET {**allow 192.168.1.0/32;deny all;}
client_max_body_size http, server, location client_max_body_size size; 1m http请求包体的最大值,常用于限定客户端所能够请求的最大包体,根据请求首部中的Content-Length来检查,以避免无用的传输。
limit_rate http, server, location, if in location limit_rate rate; 0 限制客户端每秒传输的字节数,默认为0,表示没有限制。
limit_rate_after http, server, location, if in location limit_rate_after size; 0 **nginx向客户端发送响应报文时,如果时长超过了此处指定的时长,则后续的发送过程开始限速(下载站点常用)。**配置上面的limit_rate使用。
对客户端请求的特殊处理 ignore_invalid_headers http, server ignore_invalid_headers on | off; on 是否忽略不合法的http首部,默认为on,off意味着请求首部中出现不合规的首部将拒绝响应。
log_not_found http, server, location log_not_found on | off; on 用户访问的文件不存在时,是否将其记录到错误日志中。
resolver http, server, location resolver address … [valid=time] [ipv6=on|off]; 无 **这里指ngx_http_core_module模块选项、****指定nginx使用的dns服务器地址。**valid = 30s,整缓存时间设置。在1.1.9版之前,不可能调整缓存时间,而nginx总是缓存答案5分钟的时间。
resolver_timeout http, server, location resolver_timeout time; 30s 指定DNS解析超时时长。
server_tokens http, server, location server_tokens on | off | string; on **是否在错误页面中显示和"响应头字段中发出nginx的版本号。**从版本1.9.13开始,可以使用带有变量的字符串显式设置。 空字符串禁用。
文件操作的优化 sendfile http, server, location, if in location sendfile on | off; off **是否启用sendfile内核复制模式功能。**作为静态服务器可以提高最大的IO访问速度。传统的文件读写采用read和write方式,流程为:硬盘 >> kernel buffer >> user buffer>> kernel socket buffer >>协议栈,采用sendfile文件读写的流程为:硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈,很明显sendfile这个系统调用减少了内核到用户模式之间的切换和数据拷贝次数,直接从内核缓存的数据拷贝到协议栈,提高了很大的效率。
aio http, server, location aio on | off | threads[=pool]; off **是否启用异步文件IO功能。****Linux从内核版本2.6.22开始支持,有必要启用directio,否则读取将阻塞。****directio只能用于读取在512字节边界(或XFS为4K)上对齐的块。文件结束未对齐将在阻塞模式下读取。****当在Linux上同时启用AIO和sendfile功能时,AIO用于大于或等于directio指令中指定大小的文件,而小于或禁用directio时则用sendfile。****location /video/ {****sendfile on;**aio on;directio 8m;}
open_file_cache http, server, location **open_file_cache off;**open_file_cache max=N [inactive=time]; off **是否打开文件缓存功能。max:用于缓存条目的最大值,允许打开的缓存条目最大数,当满两类以后将根据LRU(最小最少连接数)算法进行置换inactive:某缓存条目在指定时长内没有被访问过时,将自动被删除,即缓存有效期,通常默认为60s。**缓存的信息包括: 文件句柄、文件大小和上次修改时间; **已经打开的目录结构;****没有找到或没有访问权限的信息等。**建议值:max=655350(和worker_rlimit_nofile参数一致) inactive=20s;
open_file_cache_errors http, server, location open_file_cache_errors on | off; off 是否缓存文件找不到或没有权限访问等相关信息。
open_file_cache_valid http, server, location open_file_cache_valid time; 60s **多长时间检查一次缓存中的条目是否超出非活动时长。**建议值:小于等于open_file_cache inactive
open_file_cache_min_use http, server, location open_file_cache_min_uses number; 1 在open_file_cache inactive指定的时长内被访问超过此处指定的次数时,才不会被删除(删除低命中率的缓存)。
Gzip压缩相关配置 gzip http, server, location, if in location gzip on | off; off 开启内容压缩,可以有效降低客户端的访问流量和网络带宽
gzip_min_length http, server, location gzip_min_length length; 20k 内容超过最少长度后才开启压缩,因为太短的内容压缩效果不佳,且压缩过程还会浪费系统资源。这个压缩长度会作为http响应头Content-Length字段返回给客户端。 建议值:64
gzip_comp_level http, server, location gzip_comp_level 1~9; 1 **压缩级别,默认值为1。范围为1~9级,压缩级别越高压缩率越高,但对系统性能要求越高。**建议值:4
gzip_types http, server, location gzip_types mime-type …; text/html 压缩内容类型,默认为text/html;。只压缩html文本,一般我们都会压缩js、css、json之类的,可以把这些常见的文本数据都配上。如:text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Last Updated: 2023/02/14, 18:02:00
处理Vim中粘贴文本的格式问题

处理Vim中粘贴文本的格式问题→

Theme by Vdoing | Copyright © 2019-2023 pursue-wind | 粤ICP备2022093130号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
  • 飙升榜
  • 新歌榜
  • 云音乐民谣榜
  • 美国Billboard榜
  • UK排行榜周榜
  • 网络DJ