当前位置首页 > Nginx知识

【转载】Nginxnginx状态监控nginx中的超时设置nginx预压缩(gzip)Nginx配置下载资源文件使用nginxa

阅读次数:387 次  来源:admin  发布时间:

通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况。Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过web界面,一种是通过命令,web查看要比命令查看显示的结果精确一些。下面介绍这两种查看方法

No1、通过浏览器查看

通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 –with-http_stub_status_module 然后配置Nginx.conf,在server点里面加入如下内容

location /nginx_status { stub_status on; access_log off; allow 192.168.1.100;  访问IP deny all; }

配置完后重新启动Nginx后我们可以通过浏览器访问http://localhost/status 查看,如下图

解析:

Active connections //当前 Nginx 正处理的活动连接数。

erver accepts handledrequests //总共处理了8 个连接 , 成功创建 8 次握手,总共处理了500个请求。

Reading //nginx 读取到客户端的 Header 信息数。

Writing //nginx 返回给客户端的 Header 信息数。

Waiting //开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接

No2、通过命令查看

#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

TIME_WAIT 17

ESTABLISHED 3254

LAST_ACK 236

FIN_WAIT_1 648

FIN_WAIT_2 581

CLOSING 7

CLOSE_WAIT 4916

解析:

CLOSED //无连接是活动的或正在进行

LISTEN //服务器在等待进入呼叫

SYN_RECV //一个连接请求已经到达,等待确认

SYN_SENT //应用已经开始,打开一个连接

ESTABLISHED //正常数据传输状态/当前并发连接数

FIN_WAIT1 //应用说它已经完成

FIN_WAIT2 //另一边已同意释放

ITMED_WAIT //等待所有分组死掉

CLOSING //两边同时尝试关闭

TIME_WAIT //另一边已初始化一个释放

LAST_ACK //等待所有分组死掉

ginx中的超时设置

ginx使用proxy模块时,默认的读取超时时间是60s。

1. send_timeout

yntax: send_timeout the time

default: send_timeout 60

context: http, server, locatio

Directive assigns response timeout to client. Timeout is established not on entire transfer of answer, but only between two operations of reading, if after this time client will take nothing, then nginx is shutting down the connection.

2. 负载均衡配置时的2个参数:fail_timeout和max_fail

这2个参数一起配合,来控制nginx怎样认为upstream中的某个server是失效的当在fail_timeout的时间内,某个server连 接失败了max_fails次,则nginx会认为该server不工作了。同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。

个人认为,nginx不应该把这2个时间用同一个参数fail_timeout来控制,要是能再增加一个fail_time,来控制接下来的多长时间内,不再使用down掉的server就更好了~

如果不设置这2个参数,fail_timeout默认为10s,max_fails默认为1。就是说,只要某个server失效一次,则在接下来的10s内,就不会分发请求到该server上

3. proxy模块的 proxy_connect_timeout

yntax: proxy_connect_timeout timeout_in_second

context: http, server, locatio

This directive assigns a timeout for the connection to the proxyserver. This is not the time until the server returns the pages, this is the proxy_read_timeout statement. If your proxyserver is up, but hanging (e.g. it does not have enough threads to process your request so it puts you in the pool of connections to deal with later), then this statement will not help as the connection to the server has been made. It is necessary to keep in mind that this time out cannot be more than 75 seconds.

4. proxy模块的proxy_read_timeout

yntax: proxy_read_timeout the_time

default: proxy_read_timeout 60

context: http, server, locatio

This directive sets the read timeout for the response of the proxied server. It determines how long NGINX will wait to get the response to a request. The timeout is established not for entire response, but only between two operations of reading.

In contrast to proxy_connect_timeout, this timeout will catch a server that puts you in it's connection pool but does not respond to you with anything beyond that. Be careful though not to set this too low, as your proxy server might take a longer time to respond to requests on purpose (e.g. when serving you a report page that takes some time to compute). You are able though to have a different setting per location, which enables you to have a higher proxy_read_timeout for the report page's location.

If the proxied server nothing will communicate after this time, then nginx is shut connection.

另一个参考:504 Gateway Time-out问题

常见于使用nginx作为web server的服务器的网站

我遇到这个问题是在升级discuz论坛的时候遇到的

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的, 这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就极有可能导致504 Gateway Time-out

现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K

默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点

在nginx.conf里, 加入:

fastcgi_buffers 8 128k

这表示设置fastcgi缓冲区为8×128k

当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点, 例如设置成60秒:

end_timeout 60

调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验累计才有结果。

roxy_redirect off

roxy_set_header Host $host

roxy_set_header X-Real-IP $remote_addr

roxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

client_max_body_size 10m

client_body_buffer_size 128k

roxy_connect_timeout 90

roxy_send_timeout 90

roxy_read_timeout 90

roxy_buffer_size 4k

roxy_buffers 32 4k

roxy_busy_buffers_size 64k

ginx 预压缩(gzip) nginx 预压缩(gzip)静态文件对于静态文件先压缩再输出是再正常不过的事了,但是这种压缩都是动态的,在每次请求都会先压缩再输出,大大浪费了很多 cpu。如果前端加了反向代理缓存,那能减少这种浪费。但不是每个生产环境前端都有缓存的。而nginx有个模块Gzip Precompression,这个模块的作用是对于需要压缩的文件,直接读取已经压缩好的文件(文件名为加.gz),而不是动态压缩,对于不支持 gzip的请求则读取原文件。

编译nginx的时候需要把--with-http_gzip_static_module参数加上。

#启动预压缩功能,对所有类型的文件都有效

gzip_static o

#找不到预压缩文件,进行动态压缩

gzip o

gzip_min_length 1000

gzip_buffers 4 16k

gzip_comp_level 5

gzip_types text/plain application/x-javascript text/css application/xml

#gzip公共配置

gzip_http_version 1.1

gzip_proxied expired no-cache no-store private auth

#纠结的配置

#对于ie有个bug,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用gzip。

gzip_disable "MSIE [1-6]\."

#开启Http Vary头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。

gzip_vary o

gzip_static配置优先级高于gzip 开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件 gzip_types设置对gzip_static无效

Nginx配置下载资源文件

使用Nginx时,如果要让一些附件比如 txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载)

则可以在nginx的加上头配置如下:

if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls)$){

add_header Content-Disposition: ‘attachment;’

}

使用nginx apache限制ip并发访问 限制ip连接

apache

要使apache服务器做对同一IP地址的连接限制,需要mod_limitipconn来实现。一般需要手动编译。不过模块作者也提供了一些编译好的模块,根据自己的apache版本可以直接使用。

1。编译方式:

tar zxvf mod_limitipconn-0.XX.tar.gz

cd mod_limitipconn-0.XX

make apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

make install apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

2.rpm安装方式:

直接下载mod_limitipconn-0.xx.rpm

rpm -Uhv mod_limitipconn-0.xx.rpm

然后确认产生的mod_limitipconn.so文件在apache服务器模块目录里。

3。编辑httpd.conf文件:

ExtendedStatus O

LoadModule limitipconn_module modules/mod_limitipconn.so < IfModule mod_limitipconn.c >

lt; Location / > # 所有虚拟主机的/目录

MaxConnPerIP 3 # 每IP只允许3个并发连接

NoIPLimit image/* # 对图片不做IP限制

lt; /Location>

lt; Location /mp3 > # 所有主机的/mp3目录

MaxConnPerIP 1 # 每IP只允许一个连接请求

OnlyIPLimit audio/mpeg video # 该限制只对视频和音频格式的文件

lt; /Location >

lt; /IfModule>

ginx

ginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量

1.添加limit_zone

这个变量只能在http使用

vi /usr/local/nginx/conf/nginx.conf

limit_zone one $binary_remote_addr 10m

2.添加limit_co

这个变量可以在http, server, location使用

我只限制一个站点,所以添加到server里面

vi /usr/local/nginx/conf/host/gaojinbo.com.conf

limit_conn one 10

3.重启nginx

killall nginx -HUP

vi /usr/local/nginx/conf/vhosts/down.redocn.com.conf

limit_zone one $binary_remote_addr 10m

erver

{

listen 80

erver_name down.redocn.com

index index.html index.htm index.ph

root /data/www/wwwroot/dow

error_page 404 /index.ph

# redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html

location = /50x.html {

root html

}

#Zone limit

location / {

limit_conn one 1

limit_rate 20k;//限速

}

# serve static file

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

root /data/www/wwwroot/dow

expires 30d

}

}

通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况。Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过web界面,一种是通过命令,web查看要比命令查看显示的结果精确一些。下面介绍这两种查看方法

No1、通过浏览器查看

通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 –with-http_stub_status_module 然后配置Nginx.conf,在server点里面加入如下内容

location /nginx_status { stub_status on; access_log off; allow 192.168.1.100;  访问IP deny all; }

配置完后重新启动Nginx后我们可以通过浏览器访问http://localhost/status 查看,如下图

解析:

Active connections //当前 Nginx 正处理的活动连接数。

erver accepts handledrequests //总共处理了8 个连接 , 成功创建 8 次握手,总共处理了500个请求。

Reading //nginx 读取到客户端的 Header 信息数。

Writing //nginx 返回给客户端的 Header 信息数。

Waiting //开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接

No2、通过命令查看

#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

TIME_WAIT 17

ESTABLISHED 3254

LAST_ACK 236

FIN_WAIT_1 648

FIN_WAIT_2 581

CLOSING 7

CLOSE_WAIT 4916

解析:

CLOSED //无连接是活动的或正在进行

LISTEN //服务器在等待进入呼叫

SYN_RECV //一个连接请求已经到达,等待确认

SYN_SENT //应用已经开始,打开一个连接

ESTABLISHED //正常数据传输状态/当前并发连接数

FIN_WAIT1 //应用说它已经完成

FIN_WAIT2 //另一边已同意释放

ITMED_WAIT //等待所有分组死掉

CLOSING //两边同时尝试关闭

TIME_WAIT //另一边已初始化一个释放

LAST_ACK //等待所有分组死掉

通过查看Nginx的并发连接,我们可以更清除的知道网站的负载情况。Nginx并发查看有两种方法(之所以这么说,是因为笔者只知道两种),一种是通过web界面,一种是通过命令,web查看要比命令查看显示的结果精确一些。下面介绍这两种查看方法

No1、通过浏览器查看

通过web界面查看时Nginx需要开启status模块,也就是安装Nginx时加上 –with-http_stub_status_module 然后配置Nginx.conf,在server点里面加入如下内容

location /nginx_status { stub_status on; access_log off; allow 192.168.1.100;  访问IP deny all; }

配置完后重新启动Nginx后我们可以通过浏览器访问http://localhost/status 查看,如下图

解析:

Active connections //当前 Nginx 正处理的活动连接数。

erver accepts handledrequests //总共处理了8 个连接 , 成功创建 8 次握手,总共处理了500个请求。

Reading //nginx 读取到客户端的 Header 信息数。

Writing //nginx 返回给客户端的 Header 信息数。

Waiting //开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接

No2、通过命令查看

#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

TIME_WAIT 17

ESTABLISHED 3254

LAST_ACK 236

FIN_WAIT_1 648

FIN_WAIT_2 581

CLOSING 7

CLOSE_WAIT 4916

解析:

CLOSED //无连接是活动的或正在进行

LISTEN //服务器在等待进入呼叫

SYN_RECV //一个连接请求已经到达,等待确认

SYN_SENT //应用已经开始,打开一个连接

ESTABLISHED //正常数据传输状态/当前并发连接数

FIN_WAIT1 //应用说它已经完成

FIN_WAIT2 //另一边已同意释放

ITMED_WAIT //等待所有分组死掉

CLOSING //两边同时尝试关闭

TIME_WAIT //另一边已初始化一个释放

LAST_ACK //等待所有分组死掉

ginx使用proxy模块时,默认的读取超时时间是60s。

1. send_timeout

yntax: send_timeout the time

default: send_timeout 60

context: http, server, locatio

Directive assigns response timeout to client. Timeout is established not on entire transfer of answer, but only between two operations of reading, if after this time client will take nothing, then nginx is shutting down the connection.

2. 负载均衡配置时的2个参数:fail_timeout和max_fail

这2个参数一起配合,来控制nginx怎样认为upstream中的某个server是失效的当在fail_timeout的时间内,某个server连 接失败了max_fails次,则nginx会认为该server不工作了。同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。

个人认为,nginx不应该把这2个时间用同一个参数fail_timeout来控制,要是能再增加一个fail_time,来控制接下来的多长时间内,不再使用down掉的server就更好了~

如果不设置这2个参数,fail_timeout默认为10s,max_fails默认为1。就是说,只要某个server失效一次,则在接下来的10s内,就不会分发请求到该server上

3. proxy模块的 proxy_connect_timeout

yntax: proxy_connect_timeout timeout_in_second

context: http, server, locatio

This directive assigns a timeout for the connection to the proxyserver. This is not the time until the server returns the pages, this is the proxy_read_timeout statement. If your proxyserver is up, but hanging (e.g. it does not have enough threads to process your request so it puts you in the pool of connections to deal with later), then this statement will not help as the connection to the server has been made. It is necessary to keep in mind that this time out cannot be more than 75 seconds.

4. proxy模块的proxy_read_timeout

yntax: proxy_read_timeout the_time

default: proxy_read_timeout 60

context: http, server, locatio

This directive sets the read timeout for the response of the proxied server. It determines how long NGINX will wait to get the response to a request. The timeout is established not for entire response, but only between two operations of reading.

In contrast to proxy_connect_timeout, this timeout will catch a server that puts you in it's connection pool but does not respond to you with anything beyond that. Be careful though not to set this too low, as your proxy server might take a longer time to respond to requests on purpose (e.g. when serving you a report page that takes some time to compute). You are able though to have a different setting per location, which enables you to have a higher proxy_read_timeout for the report page's location.

If the proxied server nothing will communicate after this time, then nginx is shut connection.

另一个参考:504 Gateway Time-out问题

常见于使用nginx作为web server的服务器的网站

我遇到这个问题是在升级discuz论坛的时候遇到的

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的, 这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就极有可能导致504 Gateway Time-out

现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K

默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点

在nginx.conf里, 加入:

fastcgi_buffers 8 128k

这表示设置fastcgi缓冲区为8×128k

当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点, 例如设置成60秒:

end_timeout 60

调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验累计才有结果。

roxy_redirect off

roxy_set_header Host $host

roxy_set_header X-Real-IP $remote_addr

roxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

client_max_body_size 10m

client_body_buffer_size 128k

roxy_connect_timeout 90

roxy_send_timeout 90

roxy_read_timeout 90

roxy_buffer_size 4k

roxy_buffers 32 4k

roxy_busy_buffers_size 64k

ginx使用proxy模块时,默认的读取超时时间是60s。

1. send_timeout

yntax: send_timeout the time

default: send_timeout 60

context: http, server, locatio

Directive assigns response timeout to client. Timeout is established not on entire transfer of answer, but only between two operations of reading, if after this time client will take nothing, then nginx is shutting down the connection.

2. 负载均衡配置时的2个参数:fail_timeout和max_fail

这2个参数一起配合,来控制nginx怎样认为upstream中的某个server是失效的当在fail_timeout的时间内,某个server连 接失败了max_fails次,则nginx会认为该server不工作了。同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。

个人认为,nginx不应该把这2个时间用同一个参数fail_timeout来控制,要是能再增加一个fail_time,来控制接下来的多长时间内,不再使用down掉的server就更好了~

如果不设置这2个参数,fail_timeout默认为10s,max_fails默认为1。就是说,只要某个server失效一次,则在接下来的10s内,就不会分发请求到该server上

3. proxy模块的 proxy_connect_timeout

yntax: proxy_connect_timeout timeout_in_second

context: http, server, locatio

This directive assigns a timeout for the connection to the proxyserver. This is not the time until the server returns the pages, this is the proxy_read_timeout statement. If your proxyserver is up, but hanging (e.g. it does not have enough threads to process your request so it puts you in the pool of connections to deal with later), then this statement will not help as the connection to the server has been made. It is necessary to keep in mind that this time out cannot be more than 75 seconds.

4. proxy模块的proxy_read_timeout

yntax: proxy_read_timeout the_time

default: proxy_read_timeout 60

context: http, server, locatio

This directive sets the read timeout for the response of the proxied server. It determines how long NGINX will wait to get the response to a request. The timeout is established not for entire response, but only between two operations of reading.

In contrast to proxy_connect_timeout, this timeout will catch a server that puts you in it's connection pool but does not respond to you with anything beyond that. Be careful though not to set this too low, as your proxy server might take a longer time to respond to requests on purpose (e.g. when serving you a report page that takes some time to compute). You are able though to have a different setting per location, which enables you to have a higher proxy_read_timeout for the report page's location.

If the proxied server nothing will communicate after this time, then nginx is shut connection.

另一个参考:504 Gateway Time-out问题

常见于使用nginx作为web server的服务器的网站

我遇到这个问题是在升级discuz论坛的时候遇到的

一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的, 这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就极有可能导致504 Gateway Time-out

现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K

默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点

在nginx.conf里, 加入:

fastcgi_buffers 8 128k

这表示设置fastcgi缓冲区为8×128k

当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点, 例如设置成60秒:

end_timeout 60

调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验累计才有结果。

roxy_redirect off

roxy_set_header Host $host

roxy_set_header X-Real-IP $remote_addr

roxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for

client_max_body_size 10m

client_body_buffer_size 128k

roxy_connect_timeout 90

roxy_send_timeout 90

roxy_read_timeout 90

roxy_buffer_size 4k

roxy_buffers 32 4k

roxy_busy_buffers_size 64k

ginx 预压缩(gzip)静态文件对于静态文件先压缩再输出是再正常不过的事了,但是这种压缩都是动态的,在每次请求都会先压缩再输出,大大浪费了很多 cpu。如果前端加了反向代理缓存,那能减少这种浪费。但不是每个生产环境前端都有缓存的。而nginx有个模块Gzip Precompression,这个模块的作用是对于需要压缩的文件,直接读取已经压缩好的文件(文件名为加.gz),而不是动态压缩,对于不支持 gzip的请求则读取原文件。

编译nginx的时候需要把--with-http_gzip_static_module参数加上。

#启动预压缩功能,对所有类型的文件都有效

gzip_static o

#找不到预压缩文件,进行动态压缩

gzip o

gzip_min_length 1000

gzip_buffers 4 16k

gzip_comp_level 5

gzip_types text/plain application/x-javascript text/css application/xml

#gzip公共配置

gzip_http_version 1.1

gzip_proxied expired no-cache no-store private auth

#纠结的配置

#对于ie有个bug,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用gzip。

gzip_disable "MSIE [1-6]\."

#开启Http Vary头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。

gzip_vary o

gzip_static配置优先级高于gzip 开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件 gzip_types设置对gzip_static无效 nginx 预压缩(gzip)静态文件对于静态文件先压缩再输出是再正常不过的事了,但是这种压缩都是动态的,在每次请求都会先压缩再输出,大大浪费了很多 cpu。如果前端加了反向代理缓存,那能减少这种浪费。但不是每个生产环境前端都有缓存的。而nginx有个模块Gzip Precompression,这个模块的作用是对于需要压缩的文件,直接读取已经压缩好的文件(文件名为加.gz),而不是动态压缩,对于不支持 gzip的请求则读取原文件。

编译nginx的时候需要把--with-http_gzip_static_module参数加上。

#启动预压缩功能,对所有类型的文件都有效

gzip_static o

#找不到预压缩文件,进行动态压缩

gzip o

gzip_min_length 1000

gzip_buffers 4 16k

gzip_comp_level 5

gzip_types text/plain application/x-javascript text/css application/xml

#gzip公共配置

gzip_http_version 1.1

gzip_proxied expired no-cache no-store private auth

#纠结的配置

#对于ie有个bug,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用gzip。

gzip_disable "MSIE [1-6]\."

#开启Http Vary头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。

gzip_vary o

gzip_static配置优先级高于gzip 开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件 gzip_types设置对gzip_static无效

使用Nginx时,如果要让一些附件比如 txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载)

则可以在nginx的加上头配置如下:

if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls)$){

add_header Content-Disposition: ‘attachment;’

}

使用Nginx时,如果要让一些附件比如 txt,pdf,doc等不直接在浏览器打开,而弹出另存为的对话框(也就是下载)

则可以在nginx的加上头配置如下:

if ($request_filename ~* ^.*?\.(txt|pdf|doc|xls)$){

add_header Content-Disposition: ‘attachment;’

}

apache

要使apache服务器做对同一IP地址的连接限制,需要mod_limitipconn来实现。一般需要手动编译。不过模块作者也提供了一些编译好的模块,根据自己的apache版本可以直接使用。

1。编译方式:

tar zxvf mod_limitipconn-0.XX.tar.gz

cd mod_limitipconn-0.XX

make apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

make install apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

2.rpm安装方式:

直接下载mod_limitipconn-0.xx.rpm

rpm -Uhv mod_limitipconn-0.xx.rpm

然后确认产生的mod_limitipconn.so文件在apache服务器模块目录里。

3。编辑httpd.conf文件:

ExtendedStatus O

LoadModule limitipconn_module modules/mod_limitipconn.so < IfModule mod_limitipconn.c >

lt; Location / > # 所有虚拟主机的/目录

MaxConnPerIP 3 # 每IP只允许3个并发连接

NoIPLimit image/* # 对图片不做IP限制

lt; /Location>

lt; Location /mp3 > # 所有主机的/mp3目录

MaxConnPerIP 1 # 每IP只允许一个连接请求

OnlyIPLimit audio/mpeg video # 该限制只对视频和音频格式的文件

lt; /Location >

lt; /IfModule>

ginx

ginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量

1.添加limit_zone

这个变量只能在http使用

vi /usr/local/nginx/conf/nginx.conf

limit_zone one $binary_remote_addr 10m

2.添加limit_co

这个变量可以在http, server, location使用

我只限制一个站点,所以添加到server里面

vi /usr/local/nginx/conf/host/gaojinbo.com.conf

limit_conn one 10

3.重启nginx

killall nginx -HUP

vi /usr/local/nginx/conf/vhosts/down.redocn.com.conf

limit_zone one $binary_remote_addr 10m

erver

{

listen 80

erver_name down.redocn.com

index index.html index.htm index.ph

root /data/www/wwwroot/dow

error_page 404 /index.ph

# redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html

location = /50x.html {

root html

}

#Zone limit

location / {

limit_conn one 1

limit_rate 20k;//限速

}

# serve static file

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

root /data/www/wwwroot/dow

expires 30d

}

}

apache

要使apache服务器做对同一IP地址的连接限制,需要mod_limitipconn来实现。一般需要手动编译。不过模块作者也提供了一些编译好的模块,根据自己的apache版本可以直接使用。

1。编译方式:

tar zxvf mod_limitipconn-0.XX.tar.gz

cd mod_limitipconn-0.XX

make apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

make install apxs=/usr/local/apache/bin/apxs —–这里要按你自己的路径设置

2.rpm安装方式:

直接下载mod_limitipconn-0.xx.rpm

rpm -Uhv mod_limitipconn-0.xx.rpm

然后确认产生的mod_limitipconn.so文件在apache服务器模块目录里。

3。编辑httpd.conf文件:

ExtendedStatus O

LoadModule limitipconn_module modules/mod_limitipconn.so < IfModule mod_limitipconn.c >

lt; Location / > # 所有虚拟主机的/目录

MaxConnPerIP 3 # 每IP只允许3个并发连接

NoIPLimit image/* # 对图片不做IP限制

lt; /Location>

lt; Location /mp3 > # 所有主机的/mp3目录

MaxConnPerIP 1 # 每IP只允许一个连接请求

OnlyIPLimit audio/mpeg video # 该限制只对视频和音频格式的文件

lt; /Location >

lt; /IfModule>

ginx

ginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量

1.添加limit_zone

这个变量只能在http使用

vi /usr/local/nginx/conf/nginx.conf

limit_zone one $binary_remote_addr 10m

2.添加limit_co

这个变量可以在http, server, location使用

我只限制一个站点,所以添加到server里面

vi /usr/local/nginx/conf/host/gaojinbo.com.conf

limit_conn one 10

3.重启nginx

killall nginx -HUP

vi /usr/local/nginx/conf/vhosts/down.redocn.com.conf

limit_zone one $binary_remote_addr 10m

erver

{

listen 80

erver_name down.redocn.com

index index.html index.htm index.ph

root /data/www/wwwroot/dow

error_page 404 /index.ph

# redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html

location = /50x.html {

root html

}

#Zone limit

location / {

limit_conn one 1

limit_rate 20k;//限速

}

# serve static file

location ~ ^/(images|javascript|js|css|flash|media|static)/ {

root /data/www/wwwroot/dow

expires 30d

}

}

上一篇:Linuxshellread命令
下一篇:XP硬盘安装Fedora14图文教程