yum install httpd-tools -y
// 10个用户,向 http://www.test.com/ 并发发送1000条请求(总请求数=1000)
ab -c 10 -n 1000 http://www.test.com/
http {
# 将请求客户端的IP($binary_remote_addr)存放到perip区域,区域大小为10M,一个IP占用32Byte(32位系统)或64Byte(64位系统)左右
# perip是区域的名字,可自定义
limit_conn_zone $binary_remote_addr zone=perip:10m;
server {
# 每个IP最大并发1条连接
# 该语句还可直接放置到http模块下,这样下属的server都应用该配置
# 该语句还可放置到server中的location模块中,这样仅指定的location应用该配置
limit_conn perip 1;
# 每个连接限速300 k/s
limit_rate 300k;
}
}
http {
# 将请求客户端的IP存放到perip区域,区域大小为10M,并限制同一IP地址的请求每秒钟只处理一次
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
server {
# 当有大量请求爆发时,可以缓存2条请求
# 设置了nodelay,缓存队列的请求会立即处理,若请求数 > rate+burst 时,立即返回503;如果没设置,则会按照rate排队等待处理
# 该语句还可直接放置到http模块下,这样下属的server都应用该配置
# 该语句还可放置到server中的location模块中,这样仅指定的location应用该配置
limit_req zone=perip burst=2 nodelay;
}
}
http {
# 将请求客户端的IP存放到perip区域,区域大小为10M,并限制同一IP地址的请求每秒钟只处理一次
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
server {
# 当有大量请求爆发时,可以缓存2条请求
# 设置了nodelay,缓存队列的请求会立即处理,若请求数 > rate+burst 时,立即返回503;如果没设置,则会按照rate排队等待处理
# 该语句还可直接放置到http模块下,这样下属的server都应用该配置
# 该语句还可放置到server中的location模块中,这样仅指定的location应用该配置
limit_req zone=perip burst=2 nodelay;
}
}
- 测试:ab -c 1 -n 3 http://localhost/
- 3个请求全部成功,因为正在处理的请求数1加上缓存数2,没超过限制
Server Software: nginx/1.18.0
Server Hostname: 192.168.159.128
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.001 seconds
Complete requests: 3
Failed requests: 0
Total transferred: 2535 bytes
HTML transferred: 1836 bytes
Requests per second: 2439.02 [#/sec] (mean)
Time per request: 0.410 [ms] (mean)
Time per request: 0.410 [ms] (mean, across all concurrent requests)
Transfer rate: 2012.67 [Kbytes/sec] received
Server Software: nginx/1.18.0
Server Hostname: 192.168.159.128
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.002 seconds
Complete requests: 4
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Non-2xx responses: 1
Total transferred: 3223 bytes
HTML transferred: 2330 bytes
Requests per second: 2504.70 [#/sec] (mean)
Time per request: 0.399 [ms] (mean)
Time per request: 0.399 [ms] (mean, across all concurrent requests)
Transfer rate: 1970.86 [Kbytes/sec] received
upstream MyName {
server 192.168.0.1:8080 weight=1 max_conns=10;
server 192.168.0.2:8080 weight=1 max_conns=10;
}
http {
server_tokens off;
}
location / {
allow 192.168.1.1;
deny all;
}
location / {
deny 192.168.1.1;
allow all;
}
location /logs {
autoindex on;
root/opt/nginx/;
}
location ^logs~*\.(log|txt)$ {
add_header Content-Type text/plain;
root/opt/nginx/;
}
# 与CPU逻辑核心数一致
worker_processes 12;
events {
# 单个worker最大并发连接数
worker_connection 65535;
}
ulimit -a
ulimit -Hn 100000
ulimit -Sn 100000
vim /etc/security/limits.conf
接下来是文件中需要配置的内容
* soft nofile 100000
* hard nofile 100000
用户/组 软/硬限制 需要限制的项目 限制的值
http {
# 启用gzip
gzip on;
# 允许压缩的最小字节数(即如果response header中的content-length小于该值,就不压缩)
gzip_min_length 2k;
# 按照原数据大小以16k为单位的4倍申请内存用作压缩缓存
gzip_buffers 4 16k;
# 压缩级别,级别越大,压缩率越高,占用CPU时间更长
gzip_comp_level 5;
# 需要被压缩的响应类型,默认值是text/html
gzip_types text/plain application/x-javascript text/css application/xml;
# 配置最低版本的http压缩协议(即1.0时,1.0和1.1都会启用压缩;1.1时,仅1.1时才会启用压缩)
gzip_http_version 1.0;
# IE6及以下禁用压缩
gzip_disable "MSIE [1-6]\.";
}
location /nginxstatus {
stub_status on;
// 禁止将监控信息写入访问日志
access_log off;
}
cd /usr/local/src/nginx-1.18.0
# 如果不是使用的默认路径,使用 --prefix 指定
./configure --with-http_stub_status_module
make && make install
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx
upstream myserver {
# 默认所有服务器权重为 1
server 192.168.250.220:8080;
server 192.168.250.221:8080;
server 192.168.250.222:8080;
}
upstream myserver {
server 192.168.250.220:8080 weight=3;
server 192.168.250.221:8080; # default weight=1
server 192.168.250.222:8080; # default weight=1
}
upstream myserver {
least_conn;
# with default weight for all (weight=1)
server 192.168.250.220:8080;
server 192.168.250.221:8080;
server 192.168.250.222:8080;
}
upstream myserver {
least_conn;
server 192.168.250.220:8080 weight=3;
server 192.168.250.221:8080; # default weight=1
server 192.168.250.222:8080; # default weight=1
}
upstream myserver {
ip_hash;
# with default weight for all (weight=1)
server 192.168.250.220:8080;
server 192.168.250.221:8080;
server 192.168.250.222:8080;
}
upstream myserver {
hash $request_uri;
# with default weight for all (weight=1)
server 192.168.250.220:8080;
server 192.168.250.221:8080;
server 192.168.250.222:8080;
}
vim /usr/local/nginx/nginx_log.sh
#! /bin/bash
# 设置日志文件存放目录(nginx安装目录为/usr/local/nginx)
LOG_HOME="/usr/local/nginx/logs"
# 备份Log名称
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d%H%M)".access.log
# 重命名日志文件
mv ${LOG_HOME}/access.log ${LOG_HOME}/${LOG_PATH_BAK}.log
# 向nginx主进程发信号重新打开日志
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
crontab -e
# 以每分钟切割一次为例
*/1 * * * * sh /usr/local/nginx/nginx_log.sh
# 以每天切割一次为例
0 0 0 1/1 * ? sh /usr/local/nginx/nginx_log.sh
server {
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /usr/local/nginx/static;
# 缓存30天
expires 30d;
}
}
在实际的后台服务器中发布的程序中,使用静态文件时,路径指向设置为静态文件服务器(这里是代理服务器)。