nginx配置文件详解

如何查看nginx的版本和编辑加载过的模块

[root@server1 ~]# nginx -V     ##看版本以及加载过的模块
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-http_realip_module
[root@server1 ~]# nginx -v    ##查看版本
nginx version: nginx/1.14.2

配置文件中典型参数

#Nginx的worker进程运行用户以及用户组
user  nobody nobody;


#Nginx开启的进程数,建议为CPU的核数
worker_processes  1;
#worker_processes auto;


#以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用01指定分配方式.
#这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。eg:
worker_processes 4     #4核CPU 
worker_cpu_affinity 0001 0010 0100 1000

#最大连接数
worker_connections 65535	##当nginx做反向代理时,这个数字要除以2来看最大连接数

#定义全局错误日志定义类型,[debug|info|notice|warn|crit]
error_log  logs/error.log  info;

#指定进程ID存储文件位置
#pid        logs/nginx.pid;

cpu详情


[root@server2 nginx]# cat  /proc/cpuinfo ##cpu详细信息文件
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 42
model name	: Intel Xeon E312xx (Sandy Bridge)
stepping	: 1
microcode	: 0x1
cpu MHz		: 2491.910
cache size	: 4096 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx hypervisor lahf_lm xsaveopt
bogomips	: 4983.82
clflush size	: 64
cache_alignment	: 64
address sizes	: 36 bits physical, 48 bits virtual
power management:
[root@server2 nginx]# cat  /proc/cpuinfo | grep 'processor' | wc -l
1
打开虚拟机管理器,添加cpu个数
[root@server2 conf]# cat  /proc/cpuinfo | grep 'processor' 
processor	: 0
processor	: 1
processor	: 2
processor	: 3
[root@server2 ~]# cat  /proc/cpuinfo | grep 'processor' | wc -l
4

nginx开启进程

[root@server2 conf]# ps -aux   ##nginx服务会以root用户身份运行一个master,以nginx身份运行worker(当没有更改nginx.conf文件时,是以nobody身份运行)
root      2092  0.0  0.1  45396  1120 ?        Ss   15:17   0:00 nginx: master p
nginx     2093  0.0  0.1  45856  1896 ?        S    15:17   0:00 nginx: worker p
nginx     2094  0.0  0.1  45856  1896 ?        S    15:17   0:00 nginx: worker p
nginx     2095  0.0  0.1  45856  1896 ?        S    15:17   0:00 nginx: worker p
nginx     2096  0.0  0.1  45856  1896 ?        S    15:17   0:00 nginx: worker p
root      2100  0.0  0.1 151056  1884 pts/0    R+   15:19   0:00 ps -aux
[root@server2 conf]# cd /proc/2093
[root@server2 2093]# ls
attr        coredump_filter  gid_map    mountinfo   oom_score      schedstat  status
autogroup   cpuset           io         mounts      oom_score_adj  sessionid  syscall
auxv        cwd              limits     mountstats  pagemap        setgroups  task
cgroup      environ          loginuid   net         personality    smaps      timers
clear_refs  exe              map_files  ns          projid_map     stack      uid_map
cmdline     fd               maps       numa_maps   root           stat       wchan
comm        fdinfo           mem        oom_adj     sched          statm
[root@server2 2093]# cat limits 
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             3883                 3883                 processes 
Max open files            1024                 4096                 files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       3883                 3883                 signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us

对客户端并发量,下载速度的限制

我们经常会遇到这种情况,服务器流量异常,负载过大等等。对于大流量恶意的攻击访问,会带来带宽的浪费,服务器压力,影响业务,往往考虑对同一个ip的连接数,并发数进行限制

1.限制客户端请求的并发量为1
编辑配置文件

[root@server1 ~]# vim /usr/local/nginx/conf/nginx.conf
 34     limit_conn_zone $binary_remote_addr zone=addr:10m;

 48         location /download/ {    # download是客户端访问nginx的一个发布目录,需要在默认发布目录下创建这个目录
 49             limit_conn addr 1;    # 并发量为1
 50         }

[root@server1 conf]# cd /usr/local/nginx/html/
[root@server1 html]# mkdir download
[root@server1 html]# cd download/
[root@server1 download]# ls
vim.jpg
[root@server1 download]# du -h vim.jpg 
444K	vim.jpg

测试:
模拟客户端的请求,并发量为1,没有错误请求

[root@server1 download]# ab -c 1 -n 10 http://172.25.34.2/download/vim.jpg

如果并发为10 ,即-c 10,会有错误请求

[root@server1 logs]# cat access.log |grep 200
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
172.25.0.250 - - [12/Apr/2019:23:11:29 -0400] "GET /download/vim.jpg HTTP/1.0" 200 611847 "-" "ApacheBench/2.3"
可以看到请求成功的200数量远远少于503的请求错误

2.(1)限制客户端下载速率为50k

 49         location /download/ {
 50             limit_conn addr 1;
 51             limit_rate 50k;
 52         }

测试:

[root@server1 download]# ab -c 1 -n 10 http://172.25.34.2/download/vim.jpgv

会发现访问速度变慢

(2)

33     #gzip  on;
 34     limit_conn_zone $binary_remote_addr zone=addr:10m;
 35     limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
       说明:区域名称为one(自定义),占用空间大小为10m,平均处理的请求频率不能超过每秒一次
 49         location /download/ {
 50             limit_conn addr 1;
 51             #limit_rate 50k;
 52             limit_req zone=one burst=5;  #burst用于指定最大突发请求数。许多场景下,单一地限制rate并不能满足需求,设置burst,可以延迟处理超过rate限制的请求。
 53         }

测试:

[root@server1 download]# ab -c 1 -n 10 http://172.25.34..2/download/vim.jpg

上述34,35行解释

#limit_conn_zone 用来限制同一时间连接数,即并发限制
    #limit_req_zone 用来限制单位时间内的请求数,即速率限制,采用的漏桶算法 “leaky bucket” 
    $binary_remote_addr是限制同一客户端ip地址
    zone=one:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息
    # 定义一个名为addr的limit_conn_zone $binary_remote_addr是限制同一客户端ip地址

    #以下两条语句写在server之上
    limit_conn_zone $binary_remote_addr zone=addr:10m; #大小是10M内存 10M的内存来对于IP传输开销
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #1s中不超过一个请求

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值