nginx详解

nginx
多路处理模型
MPM:将结构模块化;把核心任务处理作为一个可插拔的模块,即MPM,使其能针对不同的坏境进行优化
处理模式分三种:prefork Worker event
prefork:实现了一个非线程型的,预生派的web服务器
Worker:使用的多进程,每个进程又有多个线程
event:worker模式的变种,它把服务进程从连接中分离出来,在开启keepalive场合下相对于worker模式能够承受的了更高的并发负载
I/O网络模型:
阻塞blocking IO
非阻塞noblocking IO
多路IO multiplexing
异步 asyncronous IO
时间驱动 signal drive IO
nginx特点:
采用C进行编写
高性能的web和反向代理服务器
高连接并发,支持高达50000个并发连接数的响应
作为负载均衡服务器,既可以在内部直接支持和PHP程序对外进行服务,也可以支持作为HTTP代理服务器对外进行服务
优点:
高并发连接:能够支撑50000并发连接
内存消耗小:30000并发连接下,开启的10个nginx进程才消耗15M*10=150M
配置文件简单
成本低
支持rewrite重写规则:更够根据域名,URL的不同,将http请求分到不同的后端服务器组
内置的健康检查功能
节省带宽
稳定性高
模块化设计:模块可以动态编译
外围支持好:文档全,二次开发和模块较多
支持热部署
支持事件驱动,AIO mmap
naginx主要应用:
使用nginx结合fastcgi运行PHP,JSP,Perl等程序
使用nginx作反向代理,负载均衡,规则过滤(七层)
使用nginx运行静态html,图片
结合其他新技术的结合应用
nginx的模块和工作原理
nginx有内核和模块组成
从结构上分:核心模块,基础模块,第三方模块
核心模块:HTTP模块,EVENT模块,MAIL模块
基础模块:HTTP Access 模块,HTTP FastCGI模块,HTTP Proxy模块,HTTP Rewrite 模块
第三方模块:HTTP Upstream Request Hash模块,Notice模块,HTTP Access Key模块
从功能上分:
Handlers:处理器模块,直接进行请求,并进行输出和修改headers信息等操作(该模块一般只有一个)
Filters:过滤器模块,对其他处理器模块输出的内容进行修改操作,最后由nginx 输出
Proxies:代理类模块,nginx的HTTP
Upstream 类的模块,主要与后端一些服务如fastcgi等操作交互,实现服务代理和负载均衡功能
nginx的模块直接被编译进Nginx,因此属于静态编译方式。
启动nginx后,nginx模块被自动加载,不像apache一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但同一个处理请求只能由一个模块来完成。
nginx处理请求的流程:
http发出请求---->nginx内核--(选择一个Handlers处理模块)-->Handers(处理器模块)--->
--(生产内容)-->Filters(过滤模块1)--(处理内容)-->Filters(过滤模块N)---->http响应请求
nginx arch(nginx 架构):
一个主进程,多个子进程
nginx+php/tomcat可以部署在一台服务器上(nginx本身消耗的资源较少)
基本的web服务器请求的步骤:
用户空间
HTTP服务器软件进程
(3)处理请求
|------------------------------->
| |
| <---(5)构建响应<---------|
(2)接收请求 | | (4)访问资源
| | | |
| | | |
| | | |
TCP/IP网络协议栈 | |
| | | |
| | | |
(1)建立连接------------ | 对象储存
客户端 <---------(网络接口)--
(6)发送响应
--------------------------------------------------------------------------
源码安装:
需要gcc,pcre,zlib,zlib-devel(支持ssl需openssl,openssl-devel)
软件:www.nginx.org
tar xf nginx-1.8.0.tar.gz -C /usr/src/lnmp/
cd /usr/src/lnmp/nginx-1.8.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module
make
make install
安装第三方模块:echo模块
echo模块:接收“echo”指令,指令可指定一个字符串参数,模块会输出这个字符串作为HTTP响应
软件:https://github.com/openresty/echo-nginx-module/releases/tag/v0.60
tar xf echo-nginx-module-0.60.tar.gz -C /usr/src/lnmp/nginx-1.8.0/
编译(在原来编译参数的基础上加上:--add-module=echo-nginx-module-0.60)
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --add-module=echo-nginx-module-0.60
make
make install
location / {
# root html;
echo "this is echo module "
index index.html index.htm;
}
测试:http://192.168.1.113/index.html
ls /usr/local/nginx/conf
nginx.conf
fastcgi.conf
koi-utf ----字体
uwsgi_params ----跟python交互
koi-win
fastcgi_params ----跟php交互
mime.types ----定义了nginx支持的文件类型
scgi_params
win-utf ----windows字体
/usr/local/tengine/sbin/nginx -t ---检测配置文件的语法问题
-----------------------------------------------------------------------------
nginx默认配置:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.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 {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# 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;
# }
#}
}
---------------------------------------------------------------------
状态页面:
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.1.112;
allow 192.168.1.0/24;
deny all;
}
http://192.168.1.113:80/nginx_status
显示结果:
Active connections: 1
server accepts handled requests
14 14 66
Reading: 0 Writing: 1 Waiting: 0
说明:
Active connections:当前所有处于打开状态的连接数
server accepts handled requests:总共处理了*连接,成功创建*握手,总共处理了*请求
Reading:nginx读取到客户端的Header信息数;
Writing:nginx返回给客户端的Header信息数;
Waiting:开启keep-alive的情况下,该值等于active-(reading+writing);
表示nginx已经处理网正在等候下一次请求指令的驻留连接
---------------------------------------------------------------------------------
#################################################################################
----------------------------------------------------------------------------------
配置说明:
user www www; ---#定义Nginx运行的用户和用户组
worker_processes 8; ---#nginx进程数,建议设置为等于CPU总核心数-1。
#worker_cpu_affinity 0001 0010 0100 1000---给进程分配单独的CPU来运行(避免上下文的切换),CPU的亲缘性
error_log /usr/local/nginx/logs/error.log info;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
pid /usr/local/nginx/logs/nginx.pid;
#进程pid文件
worker_rlimit_nofile 65535;
#指定进程可以打开的最大描述符:数目
#工作模式与连接数上限
#这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。
#现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
#这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。
events
{
use epoll;
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
#是Linux 2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果跑在FreeBSD上面,就用kqueue模型。
#补充说明:
#与apache相类,nginx针对不同的操作系统,有不同的事件模型
#A)标准事件模型
#Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
#B)高效事件模型
#Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
#Epoll:使用于Linux内核2.6版本及以后的系统。
#/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
#Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
worker_connections 65535;
#单个进程最大连接数(最大连接数=连接数*进程数)
#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。
#keepalive超时时间。
keepalive_timeout 60;
client_header_buffer_size 4k;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#分页大小可以用命令getconf PAGESIZE 取得。
# #getconf PAGESIZE
#4096
#但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。
open_file_cache max=65535 inactive=60s;
#这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 80s;
#这个是指多长时间检查一次缓存的有效信息。
#语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.
open_file_cache_min_uses 1;
#open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
#语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.
open_file_cache_errors on;
#语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.
}
http ---协议级别
#设定http服务器,利用它的反向代理功能提供负载均衡支持
{
include mime.types;
#文件扩展名与文件类型映射表
default_type application/octet-stream;
#默认文件类型
#charset utf-8;
#默认编码
server_names_hash_bucket_size 128;
#服务器名字的hash表大小
#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。
参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。
在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。
如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。
第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。
因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.
client_header_buffer_size 32k;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
large_client_header_buffers 4 64k;
#客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取。
client_max_body_size 8m;
#设定通过nginx上传文件的大小
sendfile on;
#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
autoindex on;
#开启目录列表访问,合适下载服务器,默认关闭。
tcp_nopush on;
#此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
tcp_nodelay on;
keepalive_timeout 120;
#长连接超时时间,单位是秒
#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_cache_min_uses 1;为指定的应答代码指定缓存时间,如上例中将200,302应答缓存一小时,301应答缓存1天,其他为1分钟。
#gzip模块设置
gzip on; #开启gzip压缩输出
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml; #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#开启限制IP连接数的时候需要使用
#负载均衡配置
upstream piao.jd.com {
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
#nginx的upstream目前支持4种方式的分配
#1、轮询(默认)
#每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
#2、weight
#指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
#例如:
#upstream bakend {
# server 192.168.0.14 weight=10;
# server 192.168.0.15 weight=10;
#}
#2、ip_hash
#每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
#例如:
#upstream bakend {
# ip_hash;
# server 192.168.0.14:88;
# server 192.168.0.15:80;
#}
#3、fair(第三方)
#按后端服务器的响应时间来分配请求,响应时间短的优先分配。
#upstream backend {
# server server1;
# server server2;
# fair;
#}
#4、url_hash(第三方)
#按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
#例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
#upstream backend {
# server squid1:3128;
# server squid2:3128;
# hash $request_uri;
# hash_method crc32;
#}
#tips:
#upstream bakend{#定义负载均衡设备的Ip及设备状态}{
# ip_hash;
# 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;
#}
#在需要使用负载均衡的server中增加 proxy_pass http://bakend/;
#每个设备的状态设置为:
#1.down表示单前的server暂时不参与负载
#2.weight为weight越大,负载的权重就越大。
#3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
#4.fail_timeout:max_fails次失败后,暂停的时间。
#5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
#client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
#client_body_temp_path设置记录文件的目录 可以设置最多3层目录
#location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
}
#虚拟主机的配置
server
{
listen 80;
#监听端口
server_name www.jd.com jd.com;
#域名可以有多个,用空格隔开
index index.html index.htm index.php;
root /data/www/jd;
#对******进行负载均衡
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#图片缓存时间设置
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS缓存时间设置
location ~ .*.(js|css)?$
{
expires 1h;
}
#日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
#$remote_user:用来记录客户端用户名称;
#$time_local: 用来记录访问时间与时区;
#$request: 用来记录请求的url与http协议;
#$status: 用来记录请求状态;成功是200,
#$body_bytes_sent :记录发送给客户端文件主体内容大小;
#$http_referer:用来记录从那个页面链接访问过来的;
#$http_user_agent:记录客户浏览器的相关信息;
#通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。
#反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
#定义本虚拟主机的访问日志
access_log /usr/local/nginx/logs/host.access.log main;
access_log /usr/local/nginx/logs/host.access.404.log log404;
#对 "/" 启用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
#以下是一些反向代理的配置,可选。
proxy_set_header Host $host;
client_max_body_size 10m;
#允许客户端请求的最大单文件字节数
client_body_buffer_size 128k;
#缓冲区代理缓冲用户端请求的最大字节数,
#如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。
#如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
#无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误
proxy_intercept_errors on;
#表示使nginx阻止HTTP应答代码为400或者更高的应答。
proxy_connect_timeout 90;
#后端服务器连接的超时时间_发起握手等候响应超时时间
#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_send_timeout 90;
#后端服务器数据回传时间(代理发送超时)
#后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout 90;
#连接成功后,后端服务器响应时间(代理接收超时)
#连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
proxy_buffer_size 4k;
#设置代理服务器(nginx)保存用户头信息的缓冲区大小
#设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
proxy_buffers 4 32k;
#proxy_buffers缓冲区,网页平均在32k以下的设置
#设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
proxy_busy_buffers_size 64k;
#高负荷下缓冲大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
#设定缓存文件夹大小,大于这个值,将从upstream服务器传
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;
#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
}
#本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#所有静态文件由nginx直接读取不经过tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
expires 15d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
}
}
------------------------------------------------------------------------------------
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
将/scripts改为$document_root ---在访问.php文件,实际读取的是$document_root(网站根目录)下的.php文件
匹配的优先级:= > ^~ > ~ > ~*
访问控制:
location / {
....
allow 192.168.1.0;
allow 192.168.1.0/24;
deny all;
}
基于用户名访问:
location / {
...
auth_basic "welcome to ";
auth_basic_user_file "/usr/local/nginx/conf/user";
}
创建该文件:
vim /usr/local/nginx/conf/user
zhangsan:$apr1$bJ0ev6Kr$Qw9GmWcI1Oh6omxUmzp9E/
(用户名和密码可用htpasswd生成器生成)
----------------------------------------------------------------------------------------
nginx的优化:
worker_processes 8;
work_cpu_affinity 0001 0010 0100 1000; ---进程与CPU绑定
work_rlimit_nofile 65535; ---nginx进程能够打开的最多文件描述符数目
work_connections 20480/65535; ---最大连接数
keepalive_timeout 60;
server_tokens off;
open_file_cache max=65535 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 65;
send_timeout 10;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
client_body_buffer_size 1K;
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on;
autoindex on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_cache_min_uses 1;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#反向代理部分:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_intercept_errors on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#内核优化设置
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
#预防DOS攻击:限制用户连接数来
limit_zone slimits $binary_remote_addr 5m;
limit_conn slimits 5;
timeout设低
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 65;
send_timeout 10;
posted @ 2018-06-19 22:28  Sky-wings  阅读(305)  评论(0编辑  收藏  举报