linux nginx module模块配置

linux nginx module模块配置

        时间: 20181118

        个人小站: www.winthcloud.top


目录

ngx_http_access_module

allow

deny

ngx_http_auth_basic_module

auth_basic

auth_basic_user_file

ngx_http_stub_status_module

stub_status

ngx_http_log_module

log_format

access_log

open_log_file_cache

ngx_http_gzip_module

gzip

gzip_comp_level

gzip_disable

gzip_min_length

gzip_http_version

gzip_buffers

gzip_types

gzip_vary

gzip_proxied

ngz_http_ssl_module

ssl

ssl_certificate

ssl_certificate_key

ssl_protocols

ssl_session_cache

ssl_session_timeout

ngz_http_rewrite_module

rewrite

return

rewrite_log

set $variable

if (condition) {}

ngx_http_referer_module

valid_referers

if($invalid_referer){return 403 ...}

ngx_http_proxy_module

proxy_pass

proxy_set_header

proxy_cache_path

proxy_cache_key

proxy_cache

proxy_cache_valid

proxy_cache_use_stale

proxy_cache_methods

proxy_hide_header

proxy_connect_timeout

proxy_send_timeout

proxy_read_timeout

ngx_http_headers_module

add_header

add_trailer

ngx_http_fastcgi_module

fastcgi_pass

fastcgi_index

fastcgi_param

fastcgi_cache_path

fastcgi_cache

fastcgi_cache_key

fastcgi_cache_methods

fastcgi_cache_min_uses

fastcgi_keep_conn

fastcgi_cache_valid

ngx_http_upstream_module

upstream name {}

server ADDRESS [parameters]

ip_hash

least_conn

hash $request_url consistent

keepalive

health_check

match name {...}

consistent 一致性hash算法(用来解决缓存雪崩)重点!!!!!!!!!!

实现Nginx高并发Linux内核优化

fs.file-max

net.ipv4.tcp_tw_reuse

net.ipv4.tcp_keepalive_time

net.ipv4.tcp_fin_timeout

net.ipv4.tcp_max_tw_buckets

net.ipv4.ip_local_port_range

net.ipv4.tcp_rmem

net.ipv4.tcp_wmem

net.core.netdev_max_backlog

net.core.rmem_default

net.core.wmem_default

net.core.rmem_max

net.core.wmem_max

net.ipv4.tcp_syncookies

net.ipv4.tcp_max_syn_backlog

net.ipv4.tcp_tw_recycle

net.core.somaxconn

net.ipv4.tcp_max_orphans

nginx 编译安装

总结



ngx_http_access_module

可实现基于ip的访问控制功能

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

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

http, server, location, limit_except

自上而下检查,一旦匹配,将生效,条件严格的置前

示例:

location / {

deny 192.168.1.1;

allow 192.168.1.0/24;

allow 10.1.1.0/16;

allow 2001:0db8::/32;

deny all;

}


ngx_http_auth_basic_module

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

1、auth_basic string | off;

2、auth_basic_user_file file;

location /admin/ {

auth_basic "Admin Area";

auth_basic_user_file /etc/nginx/.ngxpasswd;

}

用户口令文件:

1、明文文本:格式name:password:comment

2、加密文本:由htpasswd命令实现

httpd-tools所提供

3、可以使用openssl passwd命令来生成密码


ngx_http_stub_status_module模块

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

输出信息示例:

Active connections: 291

server accepts handled requests

16630948 16630948 31070465

上面三个数字分别对应accepts,handled,requests三个值

Reading: 6 Writing: 179 Waiting: 106


Active connections:当前状态,活动状态的连接数

accepts:统计总值,已经接受的客户端请求的总数

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

requests:统计总值,客户端发来的总的请求数

Reading:当前状态,正在读取客户端请求报文首部的连接的连接数

Writing:当前状态,正在向客户端发送响应报文过程中的连接数

Waiting:当前状态,正在等待客户端发出请求的空闲连接数

1、stub_status;

示例:location /status {

stub_status;

allow 172.16.0.0/16;

deny all;

}


ngx_http_log_module模块

指定日志格式记录请求

1、log_format name string ...;

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

2、access_log path [format [buffer=size] [gzip[=level]] [flush=time] 

[if=condition]];

access_log off;

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

buffer=size

flush=time

3、open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

open_log_file_cache off;

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

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

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

inactive:非活动时长

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


ngx_http_gzip_module

用gzip方法压缩响应数据,节约带宽


1、gzip on | off;

启用或禁用gzip压缩

2、gzip_comp_level level;

压缩比由低到高:1 到 9

默认:1

3、gzip_disable regex ...;

匹配到客户端浏览器不执行压缩

4、gzip_min_length length;

启用压缩功能的响应报文大小阈值

5、gzip_http_version 1.0 | 1.1;

设定启用压缩功能时,协议的最小版本

默认:1.1

6、gzip_buffers number size;

支持实现压缩功能时缓冲区数量及每个缓存区的大小

默认:32 4k 或 16 8k

7、gzip_types mime-type ...;

指明仅对哪些类型的资源执行压缩操作;即压缩过滤器

默认包含有text/html,不用显示指定,否则出错

8、gzip_vary on | off;

如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”


9、gzip_proxied off | expired | no-cache | no-store | private | 

no_last_modified | no_etag | auth | any ...;

nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压

缩功能

off:不启用压缩

expired,no-cache, no-store,private:对后端服务器的响应报文首部

Cache-Control值任何一个,启用压缩功能

示例:

gzip on;

gzip_comp_level 6;

gzip_min_length 64;

gzip_proxied any;

gzip_types text/xml text/css application/javascript;


ngx_http_ssl_module


1、ssl on | off;

为指定虚拟机启用HTTPS protocol, 建议用listen指令代替,高版本后已经废弃

2、ssl_certificate file;

当前虚拟主机使用PEM格式的证书文件

3、ssl_certificate_key file;

当前虚拟主机上与其证书匹配的私钥文件

4、ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2]; 

支持ssl协议版本,默认为后三个

5、ssl_session_cache off | none | [builtin[:size]] [shared:name:size];

none: 通知客户端支持ssl session cache,但实际不支持

builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有

[shared:name:size]:在各worker之间使用一个共享的缓存


6、ssl_session_timeout time

客户端连接可以复用ssl session cache中缓存的有效时长,默认5m


示例:

server {

listen 443 ssl;

server_name www.winthcloud.com;

root /vhosts/ssl/htdocs;

ssl_certificate /etc/nginx/ssl/nginx.crt;

ssl_certificate_key /etc/nginx/ssl/nginx.key;

ssl_session_cache shared:sslcache:20m;

ssl_session_timeout 10m;

}


apache不支持同一台物理主机多个虚拟主机使用加密,只能支持一个,而nginx

可以多个即多个https 支持TLS SNI support enabled


ngx_http_rewrite_module


将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换

即地址重写


示例:

http://www.winthcloud.com/hn --> http://www.winthcloud.com/henan

http://www.winthcloud.com --> https://www.winthcloud.com/


1. rewrite regex replacement [flag]


将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为

replacement指定的新的URI

注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;

被某条件规则替换完成后,会重新一轮的替换检查

隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]

所表示的标志位用于控制此循环机制

如果replacement是以http://或https://开头,则替换结果会直接以重定向返

回给客户端, 即永久重定向301


[flag]:

last:重写完成后停止对当前URI在当前location中后续的其它重写操作,

而后对新的URI启动新一轮重写检查;提前重启新一轮循环,不建议在

location中使用

break:重写完成后停止对当前URI在当前location中后续的其它重写操作,

而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location

中使用

redirect:临时重定向,重写完成后以临时重定向方式直接返回重写后生成的

新URI给客户端,由客户端重新发起请求;使用相对路径,或者http://或

https://开头,状态码:302

permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客

户端,由客户端重新发起请求,状态码:301


2、return

return code [text];

return code URL;

return URL;

停止处理,并返回给客户端指定的响应码,对 301, 302, 303, 307, 308跳转到URL

3、rewrite_log on | off;

是否开启重写日志, 发送至error_log(notice level)

4、set $variable value;

用户自定义变量

注意:变量定义和调用都要以$开头


5、if (condition) { ... }

条件满足时,执行配置块中的配置指令;server, location

condition:

比较操作符:

= 相同 != 不同

~ 模式匹配,区分字符大小写

~* 模式匹配,不区分字符大小写

!~ 模式不匹配,区分字符大小写

!~* 模式不匹配,不区分字符大小写

文件及目录存在性判断:

-e,!-e 存在与否(包括文件,目录,软链接)

-f,!-f 文件 -d,!-d 目录 -x,!-x 执行


示例 跳转http至https 加条件判断是因为如果不加直接重定向会导致死循环

location / {

if ( $scheme = http){

rewrite / https:www.winthcloud.com redirect;

}

}


ngx_http_referer_module


用来阻止Referer首部无有效值的请求访问,可防止盗链

请求连接是由其它网站上过来的


1、valid_referers none|blocked|server_names|string ...;

定义referer首部的合法可用值,不能匹配的将是非法值

none:请求报文首部没有referer首部

blocked:请求报文有referer首部,但无有效值

server_names:referer首部中包含本主机名

arbitrary_string:任意字符串,但可使用*作通配符

regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,

例如: ~.*\.winthcloud\.com


示例:

valid_referers none block server_names *.winthcloud.com 

winthcloud.* ~\.baidu\. ~\.google\. ~\.winthcloud\.;

if ($invalid_referer) {

return 403 http://www.winthcloud.com;

}



ngx_http_proxy_module


转发请求至另一台主机

1、proxy_pass URL;

Context:location, if in location, limit_except

注意:proxy_pass后面路径不带uri时,会将location的uri传递

(附加)给后端主机

server {

...

server_name HOSTNAME;

location /uri/ {

proxy_pass http://host[:port]; 最后没有/

}

...

}

上面示例:http://HOSTNAME/uri --> http://host/uri

如果上面示例中有 /,即:http://host[:port]/

意味着:http://HOSTNAME/uri --> http://host/ 即置换


proxy_pass后面的路径是一个uri时,其会将location的uri替换为

proxy_pass的uri

server {

...

server_name HOSTNAME;

location /uri/ {

proxy_pass http://host/new_uri/;

}

...

}

http://HOSTNAME/uri/ --> http://host/new_uri/


如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须

不能使用uri; 用户请求时传递的uri将直接附加至后端服务器之后

server {

...

server_name HOSTNAME;

location ~|~* /uri/ {

proxy_pass http://host; 不能加/

}

...

}

http://HOSTNAME/uri/ --> http://host/uri/



2、proxy_set_header field value;

设定发往后端主机的请求报文的请求首部的值

当nginx向后端代理时可以在请求报文首部添加键值对,可以使后端响应服务器

抓取其键值,如将真的客户端ip记录至后端的httpd服务器的日志中


在前端加上首部,,后端加上日志format加上要引用的首部变量


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


后端web-server日志抓取时设置的变量为

nginx server 

log_format main '$http_X_Real_IP' 所有的-都需要替换为_,

http_固定前缀表示要抓取请求报文的首部信息

httpd

LogFormat "%{X-Real-IP}i" 这便表示为要抓取请求首部某字段


3、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];


4、proxy_cache zone | off; 默认off

指明调用的缓存,或关闭缓存机制;

Context:http, server, location

5、proxy_cache_key string;

缓存中用于“键”的内容

默认值:proxy_cache_key $scheme $proxy_host $request_uri;

6、proxy_cache_valid [code ...] time;

定义对特定响应码的响应内容的缓存时长

定义在http{...}中

示例:

proxy_cache_valid 200 302 10m;

proxy_cache_valid 404 1m;


示例:在http配置定义缓存信息(注意要添加目录)

proxy_cache_path /var/cache/nginx/proxy_cache

levels=1:1:1 keys_zone=proxycache:20m

inactive=120s max_size=1g;

说明:proxycache:20m 指内存中缓存的大小,

主要用于存放key和metadata(如:使用次数)

max_size=1g 指磁盘存入文件内容的缓存空间最大值

调用缓存功能,需要定义在相应的配置段,如server{...};

proxy_cache proxycache;

proxy_cache_key $request_uri;

proxy_cache_valid 200 302 301 1h;

proxy_cache_valid any 1m;


7、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 ...

在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端

8、proxy_cache_methods GET | HEAD | POST ...;

对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存


9、proxy_hide_header field;

默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad,

X-Accel-等,用于隐藏后端服务器特定的响应首部

10、proxy_connect_timeout time;

定义与后端服务器建立连接的超时时长,如超时会出现502错误,默认为60s,

一般不建议超出75s

11、proxy_send_timeout time;

将请求发送给后端服务器的超时时长;默认为60s

12、proxy_read_timeout time;

等待后端服务器发送响应报文的超时时长,默认为60s



ngx_http_headers_module

向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值

1、add_header name value [always];

添加自定义首部

add_header X-Via $server_addr;

add_header X-Cache $upstream_cache_status;

add_header X-Accel $server_name;

2、add_trailer name value [always];

添加自定义响应信息的尾部


ngx_http_fastcgi_module

转发请求到FastCGI服务器,不支持php模块方式

1、fastcgi_pass address;

address为后端的fastcgi server的地址

可用位置:location, if in location

2、fastcgi_index name;

fastcgi默认的主页资源

示例:fastcgi_index index.php;

3、fastcgi_param parameter value [if_not_empty];

设置传递给 FastCGI服务器的参数值,可以是文本,变量或组合



示例1:

1)在后端服务器先配置fpm server和mariadb-server

2)在前端nginx服务上做以下配置:

location ~* \.php$ {

fastcgi_pass 后端fpm服务器IP:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /app/php$fastcgi_script_name;

include fastcgi_params;

}



示例2:通过/pm_status和/ping来获取fpm server状态信息

location ~* ^/(pm_status|ping)$ {

include fastcgi_params;

fastcgi_pass 后端fpm服务器IP:9000;

fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

}


4、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];

定义fastcgi的缓存;

path 缓存位置为磁盘上的文件系统

max_size=size 磁盘path路径中用于缓存数据的缓存空间上限

levels=levels:缓存目录的层级数量,以及每一级的目录数量

levels=ONE:TWO:THREE

示例:leves=1:2:2

keys_zone=name:size   k/v映射的内存空间的名称及大小

inactive=time 非活动时长


5、fastcgi_cache zone | off;

调用指定的缓存空间来缓存数据

可用位置:http, server, location

6、fastcgi_cache_key string;

定义用作缓存项的key的字符串

示例:fastcgi_cache_key $request_rui;

7、fastcgi_cache_methods GET | HEAD | POST ...;

为哪些请求方法使用缓存

8、fastcgi_cache_min_uses number;

缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指

定的次数方可被认作活动项

9、fastcgi_keep_conn on | off;

收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接

10、fastcgi_cache_valid [code ...] time;

不同的响应码各自的缓存时长



示例:

http {

fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 

keys_zone=fcgicache:20m inactive=120s;

...

server {

location ~* \.php$ {

...

fastcgi_cache fcgicache;

fastcgi_cache_key $request_uri;

fastcgi_cache_valid 200 302 10m;

fastcgi_cache_valid 301 1h;

fastcgi_cache_valid any 1m;

}

...

}

}



ngx_http_upstream_module

用于将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令进行引用

1、upstream name { ... }

定义后端服务器组,会引入一个新的上下文

默认调度算法是wrr

Context: http

upstream httpdsrvs {

server ...

server...

...

}


调用

server {

location / {

proxy_pass http://upstrave_name;

}

}


2、server address [parameters];

在upstream上下文中server成员,以及相关的参数;Context:upstream

address的表示格式:

unix:/PATH/TO/SOME_SOCK_FILE

IP[:PORT]

HOSTNAME[:PORT]

parameters:

weight=number 权重,默认为1

max_conns 连接后端报务器最大并发活动连接数,1.11.5后支持

max_fails=number 失败尝试最大次数;超出此处指定的次数时,

server将被标记为不可用,默认为1

fail_timeout=time 后端服务器标记为不可用状态的连接超时时长,默认10s

backup 将服务器标记为“备用”,即所有服务器均不可用时才启用

down 标记为“不可用”,实现灰度发布


3、ip_hash 源地址hash调度方法

4、least_conn 最少连接调度算法,当server拥有不同的权重时其为wlc,当所有

后端主机连接数相同时,则使用wrr,适用于长连接

5、hash key [consistent] 基于指定的key的hash表来实现对请求的调度,此处的key

可以直接文本、变量或二者组合

作用:将请求分类,同一类请求将发往同一个upstream server,使用consistent

参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)

时使用

hash $request_uri consistent;

hash $remote_addr;

consistent 一致性hash算法(用来解决缓存雪崩)重点!!!!!!!!!!

1.hash环 0-(2^32-1) 这么多个点是一个闭环,即当到最大值时下一个值就是0

2.对用户请求的url做hash并对2^32次方取模   hash(url)%(2^32) 固定数值<=2^32

3.对后端服务器的特征做hash如IP或主机名   hash(ip)%(2^32) 固定数值<=2^32

4.做对应关系

如后台有3个服务器hash后最终肯定会有3个数值坐落在0到2^32-1次方之间

某个url hash之后是一个固定值然后顺时针走找其相邻最近的3个服务器的hash值

此时便固定了,固定的url hash之后取模值也是固定的,并根据找其最近一个

相邻的服务器hash(ip)的值其也是固定的,至此便可实现缓存不落空

5.此时还有还一个问题就是hash环倾斜,如果有3个服务器最后hash出来值为1,2,3

则此时根据其顺时针找相邻的服务器hash取模值时,大部分的url hash取模后都被定

位找1这个服务器了,因为3到2^32-1之间这url hash值都顺时针指向了1

排序比小 比小不要比大直接指向(这里只为个人理解)


6. 倾斜问题解决方法 +随机数

这里不再讲原理了直接上步骤 为每个服务器加权重

一个服务器hash(ip)%(2^32)此时只会形成一个点

如果权重为100则在ip之后加一个随机数然后再取模,重复100次这样就会

在hash环上形成很多点至此便会解决hash环偏斜的问题

hash(ip+RANDOM)%(2^32) 根据权重定义其次数





6、keepalive 连接数N;

为每个worker进程保留的空闲的长连接数量,可节约nginx端口,并减少连接管

理的消耗


7、health_check [parameters];

健康状态检测机制;只能用于location上下文

常用参数:

interval=time检测的频率,默认为5秒

fails=number:判定服务器不可用的失败检测次数;默认为1次

passes=number:判定服务器可用的失败检测次数;默认为1次

uri=uri:做健康状态检测测试的目标uri;默认为/

match=NAME:健康状态检测的结果评估调用此处指定的match配置块

注意:仅对nginx plus有效即商业版


8 match name { ... }

对backend server做健康状态检测时,定义其结果判断机制;

只能用于http上下文

常用的参数:

status code[ code ...]: 期望的响应状态码

header HEADER[operator value]

期望存在响应首部,也可对期望的响应首部的值基于比较

操作符和值进行比较

body:期望响应报文的主体部分应该有的内容

注意:仅对nginx plus有效


ngx_stream_core_module模块

模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器


1、stream { ... }

定义stream相关的服务;Context:main

stream {

upstream mysqlsrvs {

server 192.168.22.2:3306;

server 192.168.22.3:3306;

least_conn;

}

server {

listen 10.1.0.6:3306;

proxy_pass mysqlsrvs;

}

}


2、listen 开启监听端口

listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] 

[bind] [ipv6only=on|off] [reuseport] 

[so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];



3 proxy_pass address;

指定后端服务器地址

4 proxy_timeout timeout;

无数据传输时,保持连接状态的超时时长

默认为10m

5 proxy_connect_timeout time;

设置nginx与被代理的服务器尝试建立连接的超时时长

默认为60s



stream {

upstream mysqlsrvs {

server 192.168.0.10:3306;

server 192.168.0.11:3306;

hash $remote_addr consistent;

}

server {

listen 172.16.100.100:3306;

proxy_pass mysqlsrvs;

proxy_timeout 60s;

proxy_connect_timeout 10s;

}

}



实现Nginx高并发Linux内核优化


由于默认的Linux内核参数考虑的是最通用场景,这明显不符合用于支持高并发访

问的Web服务器的定义,所以需要修改Linux内核参数,是的Nginx可以拥有更高的

性能,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者

提供压缩服务器的服务器时,期内核参数的调整都是不同的,这里针对最通用的、

使Nginx支持更多并发请求的TCP网络参数做简单的配置,

修改/etc/sysctl.conf来更改内核参数


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

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***,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加

这个值


nginx 编译安装:

yum install pcre-devel openssl-devel zlib-devel

useradd -r –s /sbin/nologin nginx

./configure --prefix=/usr/local/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx.pid \

--lock-path=/var/run/nginx.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_v2_module \

--with-http_dav_module \

--with-http_stub_status_module \

--with-threads \

--with-file-aio

make && make install


编译安装nginx选项:

--prefix=/etc/nginx 安装路径

--sbin-path=/usr/sbin/nginx 指明nginx程序文件安装路径

--conf-path=/etc/nginx/nginx.conf 主配置文件安装位置

--error-log-path=/var/log/nginx/error.log 错误日志文件安装位置

--http-log-path=/var/log/nginx/access.log 访问日志文件安装位置

--pid-path=/var/run/nginx.pid 指明pid文件安装位置

--lock-path=/var/run/nginx.lock 锁文件安装位置

--http-client-body-temp-path=/var/cache/nginx/client_temp 

客户端body部分的临时文件存放路径,服务器允许客户端使用put方法提交大

数据时,临时存放的磁盘路径


--http-proxy-temp-path=/var/cache/nginx/proxy_temp 

作为代理服务器,服务器响应报文的临时文件存放路径

--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 

作为fastcgi代理服务器,服务器响应报文的临时文件存放路径

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 

作为uwsgi代理服务器,服务器响应报文的临时文件存放路径

--http-scgi-temp-path=/var/cache/nginx/scgi_temp 

作为scgi反代服务器,服务器响应报文的临时文件存放路径

--user=nginx 指明以那个身份运行worker进程,主控master进程一般由root运行

--group=nginx

--with-http_ssl_module 表示把指定模块编译进来



总结

动静分离搭建时,将静态文件单独指出并指向一个web服务器,动态的作为根

查看系统单进程可打开的最大文件个数/proc/sys/fs/file-max


猜你喜欢

转载自blog.51cto.com/winthcloud/2318378
今日推荐