点赞
评论
收藏
分享
举报
Nginx-1.18.0的安装配置与使用
发表于2020-08-27 11:04

浏览 8.5k

文章标签

正文

一、安装

1.1 yum安装

(1) 配置好yum源与epel源

#本地光盘yum源
[development]
name=dvdbase repo
baseurl=file:///mnt/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-CentOS-7

#在线阿里云yum源
[aliyun]
name=aliyun repo
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
enabled=1
gpgchedk=1
gpgkey=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-$releasever

#在线阿里云EPEL源
[aliyunEpel]
name=aliyun epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever

(2) 安装

[root@localhost ~]# yum install -y nginx

1.2 编译安装

(1) 下载安装包

[root@localhost ~]# wget https://nginx.org/download/nginx-1.18.0.tar.gz

(2) 安装相关依赖包

[root@localhost ~]# yum install -y gcc pcre-devel openssl-devel zlib-devel

(3) 创建nginx用户,解压源码包,开始编译安装

[root@localhost ~]# useradd -r -s /sbin/nologin nginx
[root@localhost ~]# tar -xf nginx-1.18.0.tar.gz 
[root@localhost ~]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@localhost nginx-1.18.0]# make && make install

(4) 启动nginx

#直接启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
#或创建软链接启动
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@localhost ~]# ll /usr/sbin/nginx 
lrwxrwxrwx 1 root root 27 Jun 17 11:42 /usr/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx

二、配置文件详解

#全局配置端
user nginx nginx;                            #启动Ngnix工作进程的用户和组
worker_processes [number | auto];            #启动的工作进程数
worker_cpu_affinity 0001 0010 0100 1000      #将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的

error_log file [debug | info | notice | warn | error | crit | alert | emerg]    #错误日志配置
#error_log logs/error.log;
#error_log logs/error.log notice;

pid    logs/nginx.pid;                       #pid文件保存路径

work_priority 0;                             #工作进程的优先级 -20~19
work_rlimit_nofile 65536;                    #工作进程最大打开文件数

daemon off|on;                                #前台运行nginx用于测试,docker等环境,默认为on
master_process off|on;                        #是否开启Nginx的master-woker工作模式,关闭后 nginx就不会fork出worker子进程来处理请求,而是以master进程自身来处理请求

events {                            #events设置块
    worker_connections 1024;        #设置单个nginx工作进程可以接爱的最大并发连接数据;
                      ##在nginx作为http服务器的时候,最大连接数为worker_processes * worker_connctions;在nginx作为反向代理服务器的时候,最大连接数为worker_processes * worker_connections / 2 use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如select、poll、epoll,只能设置在events模块中设置 accept_mutex on; #优化同一时刻只有一个请求而避免多个睡眠进程被唤醒的设置,on为防止被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进行适当的优化 multi_accept on; #Nginx服务器的每个工作进程可以同时接受多个新的网络连接,但是需要在配置文件中配置,此指令默认为关闭,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个 } http { #http设置块 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 logs/access.log main; #自定义优化参数 sendfile on; #指定是否使用sendfile系统调用来传输文件 #tcp_nopush on; #在开启了sendfile的情况下,合并请求后统一发送给客户端 #tcp_nodelay off; #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认为on,不延迟发送,立即发送用户相应报文 keepalive_timeout 65; #设置会话保持时间,单位是秒 #gzip on; #开启文件压缩 server { listen 80; #设置监听地址和端口 server_name localhost; #设置server name,可以以空格隔开写多个,支持正则表达式,如 *.aaa.com,www.aaa.* ~^www\d+\.aaa\.com$ default_server #charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8 #access_log logs/host.access.log main; #设备访问日志 location / { root html; #指定网站目录 index index.html index.htm; #指定默认网页文件,此指令由ngx_http_index_module模块提供 } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #以http的方式转发php请求到指定web服务器 # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #以fastcgi的方式转发php请求到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; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。 # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { #自定义虚拟server # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { #https服务器配置 # 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; # } #} #include /usr/local/nginx/conf/conf.d/*.conf #导入其他路径的配置文件 }

三、相关配置实例

3.1、站点基本配置

[root@localhost ~]# mkdir /usr/local/nginx/conf/conf.d
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf
server {
    listen 80;
    server_name www.aaa.com;

    location / {
        root html/aaa.com;
        index index.html index.htm
    }
}
[root@localhost ~]# mkdir /usr/local/nginx/html/aaa.com
[root@localhost ~]# echo "www.aaa.com" > /usr/local/nginx/html/aaa.com/index.html
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl www.aaa.com
www.aaa.com

3.2、root与alias

 root:指定web的家目录,在定义location的时候,文件的绝对路径等于root+location,
 alias:定义路径别名,会把访问的路径重新定义到其指定的路径

#root
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf
server {
    listen 80;
    server_name www.aaa.com;

    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location /about {
        root html/aaa.com;    #必须要在html/aaa.com目录下创建一个about目录才可以访问,否则报错
        index index.html index.htm;
    }
}
[root@localhost ~]# mkdir /usr/local/nginx/html/aaa.com/about
[root@localhost ~]# echo "about" > /usr/local/nginx/html/aaa.com/about/index.html
[root@localhost ~]# tree /usr/local/nginx/html/aaa.com/
/usr/local/nginx/html/aaa.com/
├── about
│   └── index.html
└── index.html
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl www.aaa.com/about/
about
#alias
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf 
server {
    listen 80;
    server_name www.aaa.com;

    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location /about {              #使用alias的时候uri后面如果加了斜杠则下面的路径配置必须加斜杠,否则403
        alias html/aaa.com;        #当访问about的时候,会显示alias定义的html/aaa.com里面的内容
        index index.html index.htm;
    }
}
[root@localhost ~]# nginx -s reload
[root@localhost ~]# curl www.aaa.com/about/
www.aaa.com

3.3、location的匹配规则

语法规则: location [=|~|~*|^~] /uri/ { … }
=      #用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止
~      #用于标准uri前,表示包含正则表达式并且区分大小写
~*     #用于标准uri前,表示包含正则表达式并且不区分大写
!~     #用于标准uri前,表示包含正则表达式并且区分大小写不匹配
!~*   #用于标准uri前,表示包含正则表达式并且不区分大小写不匹配
^~    #用于标准uri前,表示包含正则表达式并且匹配以什么开头
$     #用于标准uri前,表示包含正则表达式并且匹配以什么结尾
\     #用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?
*     #用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符

匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

location = /1.jpg {          #只能访问1.jpg
    root html/aaa.com/images;
    index index.html;
}

location ~ /A.?\.jpg {        #只能访问Ax.jpg,x表示任一单个字符
    root html/aaa.com/images;
    index index.html;
}

location ~* /A.?\.jpg {       #可以访问Ax.jpg或Ax.JPG,x表示任一单个字符
    root html/aaa.com/images;
    index index.html;
}

location ^~ /images {         #匹配以images开头
    root html/aaa.com/images;
    index index.html;
}

location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {        #匹配结尾
    root html/aaa.com/images;
    index index.html;
}

3.4、Nginx 四层访问控制

location /about {
    alias /data/nginx/html/pc;
    index index.html;
    deny 192.168.145.1;
    allow 192.168.145.0/24;
    allow 2001:0db8::/32;
    deny all;             #先允许小部分,再拒绝大部分
}        

3.5、Nginx账户认证功能

[root@localhost ~]# yum install -y httpd-tools
[root@localhost ~]# htpasswd -cbm /usr/local/nginx/conf/.htpasswd user1 123456
Adding password for user user1
[root@localhost ~]# htpasswd -bm /usr/local/nginx/conf/.htpasswd user2 123456
Adding password for user user2
[root@localhost ~]# tail /usr/local/nginx/conf/.htpasswd 
user1:$apr1$jsO6E/ci$xvL4zCDCnH28SXoY00MjQ0
user2:$apr1$k00UYYEp$UKi8pQKdfPtQQplgLsxyF/
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf 
server {
    listen 80;
    server_name www.aaa.com;

    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location /about {
        root html/aaa.com;
        index index.html index.htm;
        auth_basic "login password";
        auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
    }
}

3.6、自定义错误页面

[root@localhost ~]# cat  /usr/local/nginx/html/error.html
Sorry,This is a error page.
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf 
server {
    listen 80;
    server_name www.aaa.com;
    error_page 500 502 503 504 404 /error.html

    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location /error.html {
        root html;
    }
}

3.7、自定义访问日志

[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf 
server {
    listen 80;
    server_name www.aaa.com;
    error_page 500 502 503 504 404 /error.html;
    access_log logs/www.aaa.com_access.log;
    error_log logs/www.aaa.com_error.log;
    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location = /error.html {
        root html;
    }
}

3.8、检测文件是否存在

 try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。

location /about {
    root html/aaa.com;
    index index.html index.htm;
    #try_files $uri $uri/index.html $uri.html /about/default.html;
    try_files $uri $uri/index.html $uri.html =489;
}
#当访问到不存在的uri会显示default.html,如果是自定义的状态码则会显示在返回数据的状态码中,如
[root@localhost ~]# curl www.aaa.com/about/xx.html
default page
[root@localhost ~]# curl --head www.aaa.com/about/xx.html 
HTTP/1.1 489 
Server: nginx/1.18.0
Date: Wed, 17 Jun 2020 09:49:00 GMT
Content-Length: 0
Connection: keep-alive

3.9、长连接配置

keepalive_timeout number;  #设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置
keepalive_requests number; #在一次长连接上所允许请求的资源的最大数量,默认为100次
keepalive_requests
3; keepalive_timeout 60 60; #开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。 Keep-Alive:timeout=60   #浏览器收到的服务器返回的报文 #如果设置为0表示关闭会话保持功能,将如下显示: Connection:close      #浏览器收到的服务器返回的报文

3.10、作为下载服务器配置

[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf
server {
    listen 80;
    server_name www.aaa.com;
    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location /download {
        root html/aaa.com;
        autoindex on;
        autoindex_exact_size on;
        autoindex_localtime on;
    }
}
[root@localhost ~]# mkdir /usr/local/nginx/html/aaa.com/download
[root@localhost ~]# cp nginx-1.18.0.tar.gz /usr/local/nginx/html/aaa.com/download/
#浏览器访问 www.aaa.com/download 测试

3.11、作为上传服务器

client_max_body_size 1m;          #设置允许客户端上传单个文件的最大值,默认值为1m
client_body_buffer_size size;      #用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]];        #设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash之后的值从后往前截取1位、2位、2位作为文件名
配置示例: client_max_body_size 10m; client_body_buffer_size 16k; client_body_temp_path
/usr/local/nginx/temp 1 2 2; #reload Nginx会自动创建temp目录

3.12、隐藏Nginx Server版本信息

server_tokens off;        #隐藏Nginx server版本

3.13、其它配置项

keepalive_disable none | browser ...;          #对哪种浏览器禁用长连接
limit_except method ... { ... }                 #仅用于location限制客户端使用除了指定的请求方法之外的其它方法
    method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,ROPPATCH, LOCK, UNLOCK, PATCH
    limit_except GET {
        allow 192.168.0.0/24;
        allow 192.168.7.101;
        deny all;
    }
aio on
| off #是否启用asynchronous file I/O(AIO)功能,需要编译开启 linux 2.6以上内核提供以下几个系统调用来支持aio: 1、SYS_io_setup:建立aio 的context 2、SYS_io_submit: 提交I/O操作请求 3、SYS_io_getevents:获取已完成的I/O事件 4、SYS_io_cancel:取消I/O操作请求 5、SYS_io_destroy:毁销aio的context directio size | off;        #操作完全和aio相反,aio是读取文件而directio是写文件到磁盘,启用直接I/O,默认为关闭,当文件大于等于给定大小时,例如directio 4m,同步(直接)写磁盘,而非写缓存。
open_file_cache off; #是否缓存打开过的文件信息 open_file_cache max
=N [inactive=time]; nginx可以缓存以下三种信息: (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间 (2) 打开的目录结构 (3) 没有找到的或者没有权限访问的文件的相关信息 max=N:可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使用)算法实现管理 inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除 open_file_cache_min_uses number; #open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1 open_file_cache_errors on | off;    #是否缓存查找时发生错误的文件一类的信息,默认值为off open_file_cache_valid time; #缓存项有效性的检查验证频率,默认值为60s #配置实例: open_file_cache max=10000 inactive=60s;   #最大缓存10000个文件,非活动数据超时时长60s open_file_cache_valid 60s; #每间隔60s检查一下缓存数据有效性 open_file_cache_min_uses 5;   #60秒内至少被命中访问5次才被标记为活动数据 open_file_cache_errors on; #缓存错误信息

四、高级应用

4.1、状态页配置

 基于nginx模块ngx_http_auth_basic_module实现,在编译安装nginx的时候需要添加编译参数--with-http_stub_status_module,否则配置完成之后监测会是提示语法错误。

location /nginx_status {
    stub_status;
    allow 192.168.145.0/24;
    allow 127.0.0.1;
    deny all;
}
[root@www ~]# curl 192.168.145.27/nginx_status/
Active connections: 4 
server accepts handled requests
 13 13 28 
Reading: 0 Writing: 1 Waiting: 3

Active connections:     当前处于活动状态的客户端连接数,包括连接等待空闲连接数。
accepts:                统计总值,Nginx自启动后已经接受的客户端请求的总数。
handled:                统计总值,Nginx自启动后已经处理完成的客户端请求的总数,通常等于accepts,除非有因worker_connections限制等被拒绝的连接。
requests:               统计总值,Nginx自启动后客户端发来的总的请求数。
Reading:                当前状态,正在读取客户端请求报文首部的连接的连接数。
Writing:                当前状态,正在向客户端发送响应报文过程中的连接数。
Waiting:                当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于active  (reading+writing)

4.2、变量使用

 Nginx的变量可以在配置文件中引用,作为功能判断或者日志等场景使用,变量可以分为内置变量和自定义变量,内置变量是由nginx模块自带,通过变量可以获取到众多的与客户端访问相关的值。

 (1) 内置变量

$remote_addr;        #存放了客户端的地址,注意是客户端的公网IP,也就是一家人访问一个网站,则会显示为路由器的公网IP
$args;               #变量中存放了URL中的指令,例如http://www.aaa.com/about/index.do?id=202006&partner=search中的id=202006&partner=search
$document_root;     #保存了针对当前资源的请求的系统根目录,如/usr/local/nginx/html
$document_uri;       #保存了当前请求中不包含指令的URI,注意是不包含请求的指令,例如http://www.aaa.com/about/index.do?id=202006&partner=search中的/about/index.do
$host;               #存放了请求的host名称。
$http_user_agent;    #客户端浏览器的详细信息
$http_cookie;        #客户端的cookie信息。

limit_rate 10240;
echo $limit_rate;    #如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0。
$remote_port;      #客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口。
$remote_user;      #已经经过Auth Basic Module验证的用户名。
$request_body_file;    #做反向代理时发给后端服务器的本地资源的名称。
$request_method;       #请求资源的方式,GET/PUT/DELETE等
$request_filename;     #当前请求的资源文件的路径名称,由root或alias指令与URI请求生成的文件绝对路径,如/apps/nginx/html/main/index.html
$request_uri;          #包含请求参数的原始URI,不包含主机名,如:/main/index.do?id=20190221&partner=search 。
$scheme;            #请求的协议,如ftp,https,http等。
$server_protocol;     #保存了客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等。
$server_addr;       #保存了服务器的IP地址。
$server_name;         #请求的服务器的主机名。

 (2) 自定义变量

  假如需要自定义变量名称和值,使用指令set $variable value;,则方法如下:

   Syntax: set $variable value;

  Default: —

  Context: server, location, if

set $name aaa;
echo $name;
set $my_port $server_port;
echo $my_port;
echo "$server_name:$server_port";

4.3、压缩功能

 Nginx支持对指定类型的文件进行压缩然后再传输给客户端,而且压缩还可以设置压缩比例,压缩后的文件大小将比源文件显著变小,这样有助于降低出口带宽的利用率,降低企业的IT支出,不过会占用相应的CPU资源。Nginx对文件的压缩功能是依赖于模块ngx_http_gzip_module。

#相关配置参数:
gzip on | off;                #启用或禁用gzip压缩,默认关闭
gzip_comp_level level;        #压缩比由低到高从1到9,默认为1
gzip_disable "MSIE [1-6]\.";  #禁用IE6 gzip功能
gzip_min_length 1k;           #gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_http_version 1.0 | 1.1;  #启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_buffers number size;     #指定Nginx服务需要向服务器申请的缓存空间的个数*大小,默认32 4k|16 8k;
gzip_types mime-type ...;     #指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
gzip_vary on | off;           #如果启用压缩,是否在响应报文首部插入"Vary: Accept-Encoding"
如:
gzip on;
gzip_comp_level 5;
gzip_min_length 1k;
gzip_types text/plain application/javascript application/x-javascripttext/css application/xml text/javascript application/x-httpd-php image/jpegimage/gif image/png;
gzip_vary on

4.4、https配置

 Nginx 的https功能基于模块ngx_http_ssl_module实现,因此如果是编译安装的nginx要使用参数ngx_http_ssl_module开启ssl功能,但是作为nginx的核心功能,yum安装的nginx默认就是开启的,编译安装的nginx需要指定编译参数--with-http_ssl_module开启。

ssl on | off;                  #为指定的虚拟主机配置是否启用ssl功能,此功能在1.15.0废弃,使用listen [ssl]替代。
ssl_certificate /path/to/file;        #当前虚拟主机使用使用的公钥文件,一般是crt文件
ssl_certificate_key /path/to/file;    #当前虚拟主机使用的私钥文件,一般是key文件
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];    #支持ssl协议版本,早期为ssl现在是TSL,默认为后三个
ssl_session_cache off | none | [builtin[:size]] [shared:name:size];        #配置ssl缓存
    off: 关闭缓存
    none: 通知客户端支持ssl session cache,但实际不支持
    builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
    [shared:name:size]:在各worker之间使用一个共享的缓存,需要定义一个缓存名称和缓存空间大小,一兆可以存储4000个会话信息,多个虚拟主机可以使用相同的缓存名称。
    ssl_session_timeout time;#客户端连接可以复用ssl session cache中缓存的有效时长,默认5m

 #nginx证书配置
 listen 80;
 listen 443 ssl;
 ssl_certificate /usr/local/nginx/certs/www.aaa.com.crt;
 ssl_certificate_key /usr/local/nginx/certs/www.aaa.com.key;
 ssl_session_cache shared:sslcache:20m;
 ssl_session_timeout 10m;

4.5、关于favicon.ico

 favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错。

#解决办法:
#1.服务器不记录访问日志:
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
#2.将图标保存到指定目录访问:
#location ~ ^/favicon\.ico$ {
location = /favicon.ico {
    root /data/nginx/html/pc/images;
}

4.6、rewrite指令

 通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理。

 用法:rewrite regex replacement [flag];

 (1) rewrite flag

  利用nginx的rewrite的指令,可以实现url的重新跳转,rewrtie有四种不同的flag,分别是redirect(临时重定向)、permanent(永久重定向)、break和last。其中前两种是跳转型的flag,后两种是代理型,跳转型是指有客户端浏览器重新对新地址进行请求,代理型是在WEB服务器内部实现跳转的。

redirect;         #临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent;        #重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,状态码:301
last#重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
break#重写完成后停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用

 (2) 临时重定向与永久重定向

location / {
    root /usr/local/nginx/html/pc;
    index index.html;
    rewrite / http://www.aaa.com permanent;
    #rewrite / http://www.aaa.com redirect;
}

 (3) last与break

location /break {
    rewrite ^/break/(.*) /test$1 break;        #break不会跳转到其他的location
    return 666 "break";
}

location /last {
    rewrite ^/last/(.*) /test$1 last;        #last会跳转到其他的location继续匹配新的URI
    return 888 "last";
}

location /test {
    return 999 "test";
}

 (4) 自动跳转https

server {
    listen 80;
    listen 443 ssl;
    ssl_certificate /usr/local/nginx/certs/www.aaa.com.crt;
    ssl_certificate_key /usr/local/nginx/certs/www.aaa.com.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
    server_name www.aaa.com;
    location / {
        root html/aaa.com;
        index index.html index.htm;
        if ($scheme = http){    #不加条件判断,会导致死循环
            rewrite / https://www.aaa.com permanent;
        }
    }
}

#还一种是对监听的80端口进行跳转
server {
    listen 80 default_server;
    server_name www.aaa.com;
    rewrite ^(.*)$ https://$server_name$1 permanent;
}

 (5) 判断文件是否存在

例:当用户访问到公司网站的时输入了一个错误的URL,可以将用户重定向至官网首页
location / {
    root /usr/local/nginx/html/pc;
    index index.html;
    if (!-f $request_filename) {
        #return 404 "No Page";
        rewrite (.*) http://www.aaa.net/index.html;
    }
}

 (6) 防盗链

  防盗链基于客户端携带的referer实现,referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息,如果别人只链接了自己网站图片或某个单独的资源,而不是打开了网站的整个页面,这就是盗链,referer就是之前的那个网站域名,正常的referer信息有以下几种:

none:              #请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。
blocked:           #请求报文有referer首部,但无有效值,比如为空。
server_names:      #referer首部中包含本主机名及即nginx 监听的server_name。
arbitrary_string:   #自定义指定字符串,可使用*作通配符。
regular expression: #被指定的正则表达式模式匹配到的字符串,要使用~开头,例如:~.*\.aaa\.com
location ^~ /images {
    root /usr/local/nginx/pc;
    index index.html;
    valid_referers none blocked server_names *.aaa.com www.aaa.* api.online.test/v1/hostlist ~\.google\. ~\.baidu\.;                 #定义有效的referer
    if ($invalid_referer) {        #假如是使用其他的无效的referer访问:
        return 403;             #返回状态码403
    }
}

4.7、代理功能

 Nginx除了可以在企业提供高性能的web服务之外,另外还可以将本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能:

  ngx_http_proxy_module:   将客户端的请求以http协议转发至指定服务器进行处理。
  ngx_stream_proxy_module: 将客户端的请求以tcp协议转发至指定服务器处理。
  ngx_http_fastcgi_module:  将客户端对php的请求以fastcgi协议转发至指定服务器助理。
  ngx_http_uwsgi_module:  将客户端对Python的请求以uwsgi协议转发至指定服务器处理。

 配置参数:
  proxy_pass; #用来设置将客户端请求转发给的后端服务器的主机,可以是主机名、IP地址:端口的方式,也可以代理到预先设置的主机群组;

location /web {
    index index.html;
    proxy_pass http://192.168.7.103:80;
    #不带斜线将访问的/web,等于访问后端服务器 http://192.168.7.103:80/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问,这是一个追加/web到后端服务器 http://servername:port/WEB/INDEX.HTML的操作
    proxy_pass http://192.168.7.103:80/;    #带斜线,等于访问后端服务器的http://192.168.7.103:80/index.html 内容返回给客户端
}

 (1) 正向代理配置

#代理服务器上配置
http {
    resolver 8.8.8.8;
    server {
        listen 8088;
        location / {
            proxy_pass http://$http_host$request_uri;
        }
    }
}
#客户端配置
export "http_proxy=http://[user]:[pass]@host:port/"
如:export http_proxy=http://192.168.145.27:8088

 (2) 反向代理配置

server {
    listen 80;
    server_name www.magedu.net;
    location / {
        proxy_pass http://192.168.145.7:80/;
    }
}
#也可以对指定location进行代理
location /web {
    proxy_pass http://192.168.7.103:80/;     #注意有后面的/
}

 (3) 代理缓存功能

#代理缓存功能默认关闭,可通过下面参数配置:
proxy_cache zone | off;           #指明调用的缓存,或关闭缓存机制;默认off;Context:http, server, location
proxy_cache_key string;            #缓存中用于"键"的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid [code ...]
time; #定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例: proxy_cache_valid
200 302 10m; proxy_cache_valid 404 1m; proxy_cache_path; #定义可用于proxy功能的缓存;Context:http proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time]
[purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; 例:在http配置定义缓存信息 proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创建 levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=1048576个目录 keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数) inactive=120s #缓存有效时间 max_size=1g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值
#调用缓存功能,需要定义在相应的配置段,如server{...};或者location等 proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid
200 302 301 1h; proxy_cache_valid any 1m; proxy_cache_use_stale;    #在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端 proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ;   #默认是off proxy_cache_methods GET | HEAD | POST ...; #对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存 proxy_set_header field value; #设定发往后端主机的请求报文的请求首部的值 Context: http, server, location proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 请求报文的标准格式如下: X-Forwarded-For: client1, proxy1, proxy2 [root@www ~]# vim /usr/local/nginx/conf/nginx.conf #配置在nginx.conf http配置段 proxy_cache_path /usr/local/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g; [root@www ~]# vim /usr/local/nginx/conf/conf.d/pc.conf #要缓存的URL 或者放在server配置项对所有URL都进行缓存 location /web { proxy_pass http://192.168.27.7:80/; proxy_set_header clientip $remote_addr; proxy_cache proxycache; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m; }

 (4) 添加头部报文信息

  Syntax: add_header name value [always];
  Default: —
  Context: http, server, location, if in location

#添加自定义首部
add_header name value [always];
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;

#添加自定义响应信息的尾部, 1.13.2版后支持
add_trailer name value [always];

#nginx配置:
location /web {
    proxy_pass http://192.168.27.7:80/;
    proxy_set_header clientip $remote_addr;
    proxy_cache proxycache;
    proxy_cache_key $request_uri;
    proxy_cache_valid 200 302 301 1h;
    proxy_cache_valid any 1m;
    add_header X-Via $server_addr;
    add_header X-Cache $upstream_cache_status;
    add_header X-Accel $server_name;
}

4.8、负载均衡

 (1) http的负载均衡-upstream

配置参数:
upstream name {        #自定义一组服务器,配置在http内
    server address [parameters];    #配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
}
#server支持的parameters如下: weight
=number #设置权重,默认为1 max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制 max_fails=number #对后端服务器连续监测失败多少次就标记为不可用 fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒 backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器 down #标记为down状态 resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx hash KEY consistent; #基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。所谓取模运算,就是计算两个数相除之后的余数,比如10%7=3, 7%4=3 hash $request_uri consistent; #基于用户请求的uri做hash ip_hash; #源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持
调度算法:
    rr:         轮询,默认的调度方式,将所有请求都按照时间顺序分配到不同的服务上;
    weight:      权重,指定每个服务的权重比例,weight和访问比率成正比;
    ip_hash:     指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端的请求一直发送到相同的服务器,以保证session会话。这样每个访客都固定访问一个后端服务器,可以解决session不能跨服务器的问题;
    least_conn:  把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果
    fair:      按照服务器端的响应时间来分配请求,响应时间短的优先分配;
    url_hash:   按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费;
            而使用url_hash,可以使得同一个url(也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再此收到请求,就可以从缓存中读取; 例: upstream webserver { #hash $request_uri consistent; #ip_hash; #least_conn; server
192.168.27.7:80 weight=1 fail_timeout=5s max_fails=3; #后端服务器状态监测 server 192.168.27.17:80 weight=1 fail_timeout=5s max_fails=3 backup; } server { listen 80; server_name www.aaa.net; location / { index index.html index.php; root /usr/local/nginx/html/pc; } location /web { index index.html; proxy_pass http://webserver/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #客户端IP透传,添加客户端IP到报文头部 } }

 (2) tcp的负载均衡

  Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端。

配置参数:
stream {         #定义stream
    upstream backend {                        #定义后端服务器
        hash $remote_addr consistent;         #定义调度算法
        server backend1.example.com:12345 weight=5;         #定义具体server
        server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }
    upstream dns {                            #定义后端服务器
        server 192.168.0.1:53535;             #定义具体server
        server dns.example.com:53;
    }
    server {                                  #定义server
        listen 12345;                         #监听IP:PORT
        proxy_connect_timeout 1s;             #连接超时时间
        proxy_timeout 3s;                     #转发超时时间
        proxy_pass backend;                   #转发到具体服务器组
    }
    server {
        listen 127.0.0.1:53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }
    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
}
实例1:Redis负载均衡 stream { upstream redis_server { #hash $remote_addr consistent; server
192.168.145.27:6379 max_fails=3 fail_timeout=30s; } server { listen 192.168.145.7:6379; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass redis_server; } } 实例2:mysql负载均衡 stream { upstream mysql_server { least_conn; server 192.168.145.27:3306 max_fails=3 fail_timeout=30s; } server { listen 192.168.145.7:3306; proxy_connect_timeout 6s; proxy_timeout 15s; proxy_pass mysql_server; } }

4.9、FastCGI配置

 Nginx基于模块ngx_http_fastcgi_module实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理,其配置参数如下:

fastcgi_pass address;        #转发请求到后端服务器,address为后端的fastcgi server的地址,可用位置:location, if in location 
fastcgi_index name;            #fastcgi默认的主页资源,示例:fastcgi_index index.php;
fastcgi_param parameter value [if_not_empty];        #设置传递给FastCGI服务器的参数值,可以是文本,变量或组合,可用于将Nginx的内置变量赋值给自定义key
fastcgi_param REMOTE_ADDR $remote_addr;     #客户端源IP
fastcgi_param REMOTE_PORT $remote_port;     #客户端源端口
fastcgi_param SERVER_ADDR $server_addr;     #请求的服务器IP地址
fastcgi_param SERVER_PORT $server_port;     #请求的服务器端口
fastcgi_param SERVER_NAME $server_name;     #请求的server name
Nginx默认配置示例:
location ~ \.php$ {
    root html;        #$document_root 调用root目录
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;     #默认脚本路径
    #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    #或用$document_root,
    include fastcgi_params;
}
fastcgi缓存定义:
fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] 
[loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; path #缓存位置为磁盘上的文件系统路径 max_size=size #磁盘path路径中用于缓存数据的缓存空间上限 levels=levels: #缓存目录的层级数量,以及每一级的目录数量,levels=ONE:TWO:THREE,示例:leves=1:2:2 keys_zone=name:size #设置缓存名称及k/v映射的内存空间的名称及大小 inactive=time #缓存有效时间,默认10分钟,需要在指定时间满足fastcgi_cache_min_uses 次数被视为活动缓存 fastcgi缓存调用: fastcgi_cache zone | off; #调用指定的缓存空间来缓存数据,可用位置:http, server, location fastcgi_cache_key string; #定义用作缓存项的key的字符串,示例:fastcgi_cache_key $request_uri; fastcgi_cache_methods GET | HEAD | POST ...; #为哪些请求方法使用缓存 fastcgi_cache_min_uses number; #缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项 fastcgi_keep_conn on | off; #收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接 fastcgi_cache_valid [code ...] time; #不同的响应码各自的缓存时长 fastcgi_hide_header field; #隐藏响应头指定信息 fastcgi_pass_header field; #返回响应头指定信息,默认不会将Status、X-Accel-...返回

 Nginx与php-fpm实现:

(1) 安装php-fpm
[root@www ~]# yum install php-fpm php-mysql -y
[root@www ~]# systemctl start php-fpm
(2) 准备php测试页
[root@www ~]# vim /usr/local/nginx/html/aaa.com/index.php
php
    phpinfo();
?>
(3) nginx配置转发
[root@www ~]# vim /usr/local/nginx/conf/conf.d/aaa.conf
server {
    listen 80;
    server_name www.aaa.com;
    location / {
        root html/aaa.com;
        index index.html index.htm;
    }
    location ~ \.php$ {
        root html/aaa.com;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
(3) 重启nginx并使用浏览器测试
[root@www ~]# nginx -s reload

php相关配置优化:
[root@www ~]# grep "^[a-Z]" /etc/php-fpm.conf
include=/etc/php-fpm.d/*.conf
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes        #是否后台启动

[root@www ~]# grep "^[a-Z]" /etc/php-fpm.d/www.conf 
listen = 127.0.0.1:9000                    #监听地址及IP
listen.allowed_clients = 127.0.0.1        #允许客户端从哪个源IP地址访问,要允许所有则在行首加 ; 注释即可
user = nginx                            #php-fpm启动的用户和组,会涉及到后期文件的权限问题
group = nginx
pm = dynamic                            #动态模式进程管理
pm.max_children = 500                    #静态方式下开启的php-fpm进程数量,在动态方式下他限定php-fpm的最大进程数
pm.start_servers = 100                    #动态模式下初始进程数,必须大于等于pm.min_spare_servers和小于等于pm.max_children的值
pm.min_spare_servers = 100                #最小空闲进程数
pm.max_spare_servers = 200                #最大空闲进程数
pm.max_requests = 500000                #进程累计请求回收值,会重启
pm.status_path = /pm_status             #状态访问URL
ping.path = /ping                         #ping访问动地址
ping.response = ping-pong                 #ping返回值
slowlog = /var/log/php-fpm/www-slow.log    #慢日志路径
php_admin_value[error_log] = /var/log/php-fpm/www-error.log        #错误日志
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files                            #phpsession保存方式及路径
php_value[session.save_path] = /var/lib/php/session                #当时使用file保存session的文件路径

五、系统参数优化

 默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者提供压缩服务器的服务器时,内核参数的调整都是不同的,此处针对最通用的、使Nginx支持更多并发请求的TCP网络参数做简单的配置,修改/etc/sysctl.conf来更改内核参数。

fs.file-max = 1000000
#表示单个进程较大可以打开的句柄数

net.ipv4.tcp_tw_reuse = 1
#参数设置为 1 ,表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,这对于服务器来说意义重大,因为总有大量TIME_WAIT状态的链接存在

net.ipv4.tcp_keepalive_time = 600
#当keepalive启动时,TCP发送keepalive消息的频度;默认是2小时,将其设置为10分钟,可更快的清理无效链接

net.ipv4.tcp_fin_timeout = 30
#当服务器主动关闭链接时,socket保持在FIN_WAIT_2状态的较大时间

net.ipv4.tcp_max_tw_buckets = 5000
#表示操作系统允许TIME_WAIT套接字数量的较大值,如超过此值,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为8000,过多的TIME_WAIT套接字会使Web服务器变慢

net.ipv4.ip_local_port_range = 1024 65000
#定义UDP和TCP链接的本地端口的取值范围

net.ipv4.tcp_rmem = 10240 87380 12582912
#定义了TCP接受缓存的最小值、默认值、较大值

net.ipv4.tcp_wmem = 10240 87380 12582912
#定义TCP发送缓存的最小值、默认值、较大值

net.core.netdev_max_backlog = 8096
#当网卡接收数据包的速度大于内核处理速度时,会有一个列队保存这些数据包。这个参数表示该列队的较大值

net.core.rmem_default = 6291456
#表示内核套接字接受缓存区默认大小

net.core.wmem_default = 6291456
#表示内核套接字发送缓存区默认大小

net.core.rmem_max = 12582912
#表示内核套接字接受缓存区较大大小

net.core.wmem_max = 12582912
#表示内核套接字发送缓存区较大大小

注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑

net.ipv4.tcp_syncookies = 1
#与性能无关。用于解决TCP的SYN攻击

net.ipv4.tcp_max_syn_backlog = 8192
#这个参数表示TCP三次握手建立阶段接受SYN请求列队的较大长度,默认1024,将其设置的大一些可使出现Nginx繁忙来不及accept新连接时,Linux不至于丢失客户端发起的链接请求

net.ipv4.tcp_tw_recycle = 1
#这个参数用于设置启用timewait快速回收

net.core.somaxconn=262114
#选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此需要结合高并发请求数来调节此值。

net.ipv4.tcp_max_orphans=262114
#选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立链接将立即被复位并输出警告信息。这个限制指示为了防止简单的DOS攻击,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加这个值
已修改于2023-03-08 22:42
创作不易,留下一份鼓励
守望

暂无个人介绍

关注



写下您的评论
发表评论
全部评论(0)

按点赞数排序

按时间排序

关于作者
守望
这家伙很懒还未留下介绍~
89
文章
0
问答
17
粉丝
相关文章
系统层优化系统socket层优化echo65535>/proc/sys/net/core/somaxconn 准许最大链接数echo1>/proc/sys/net/ipv4/tcp_tw_recycle 快速回收链接echo1>/proc/sys/net/ipv4/tcp_tw_reuse  重用链接echo0>/proc/sys/net/ipv4/tcp_syncookies 关闭洪水抵御系统文件层优化ulimit-n65535nginx配置层优化nginxsocket层优化 worker_connections 10240;nginx层打开文件数优化worker_rlimit_nofile20000;ab-n500000-c20000 http://192.168.1.52/index.htmlServerSoftware:    nginx/1.10.2ServerHostname:    192.168.1.52ServerPort:      80DocumentPath:
点赞 4
浏览 1.1k
NginxVideoThumbExtractorModuleVideoThumbExtractorisamoduletoextractanimagefromavideoframefromaspecificsecondresizing/croppingittoagivensize.Thesmallestgeneratedimageis16x16pixels.ThismoduleisnotdistributedwiththeNginxsource.See theinstallationinstructions.Availableongithubat nginx-video-thumbextractor-moduleStatusThismoduleisconsideredproductionready.RequirementsThismoduledependsfromsomelibraries(headersandsharedobjects)whic
点赞 1
浏览 851
近年来,Kubernetes日益获得业内青睐采用该平台与微服务架构。充分理应用并非易事多的情况下。——当数据在构成应用的微服务之间。在博文Oracing社区创建的开源模块(nginxNGINXOpenSource和NGINXPlus提供种编程语言类使用NGINX和NGINXPlusIngressControllersforKubernetes用例,此处我们讨论跟踪服务器端点既。这两个应用均在NGINXIngress执行负载均衡的Kubernetes集群中运行,并启用了OpenTracingController有关系统任何的信息将span捕获,我们即可穿过的镜像中搭配使用我们的Ingress结的Docker镜像中,并指定所用跟踪器。Controller中,我们为NGINX和NGINXPlus提供了单独的Dockerfile。处理的
点赞 0
浏览 4.9k