Linux-《Nginx-运维基础入门》实验报告

602 阅读4分钟

环境

  • Ubuntu 16.04.7 LTS
  • Nginx 1.10.3
  • Mysql 5.7.33
  • PHP 7.0.33

安装LNMP环境

  • 安装 nginx

方式一:apt-get 安装
$ apt-get update && apt-get install -y nginx
方式二:源码安装
$ wget http://nginx.org/download/nginx-1.9.9.tar.gz
$ tar -zxvf nginx-1.9.9.tar.gz -C /usr/local/src

  • 浏览器访问

15325592-a4da0f9abf7b4dda.png

  • 修改 nginx 配置文件 $ vim /etc/nginx/sites-available/default
    这个模块的作用就是让php和nginx一起工作 location ~ .php$ {
    limit_req zone=one burst=5 nodelay; # 加入这行
    include snippets/fastcgi-php.conf;
    With php7.0-cgi alone:
    fastcgi_pass 127.0.0.1:9000;
    With php7.0-fpm:
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

  • 测试 nginx 配置文件是否正确

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  • nginx 常用命令

方式一
$ /etc/init.d/nginx start
$ /etc/init.d/nginx stop
$ /etc/init.d/nginx restart
方式二
$ service nginx start
$ service nginx stop
$ service nginx restart

  • 安装 mysql $ apt-get install mysql-server mysql-client
  • 启动 mysql $ service mysql start
  • 安装 php $ apt-get install -y php7.0-fpm
  • 安装 php7.0-mysql 模块 $ apt-get install php7.0-mysql
  • 启动 php7.0-fpm 服务 $ service php7.0-fpm start
  • 验证 php 是否支持 mysql $ php -m | grep mysql
    mysqli
    mysqlnd
    pdo_mysql

Nginx配置文件详解

#  nginx的用户,因为PHP默认是以www-data用户运行的。
user www-data;

#  worker进程数量,这个优化值受到包括CPU内核数、存储数据的磁盘数、负载值在内的许多因素的影响。设置为auto,将会尝试自动检测可用的值。
worker_processes auto;

#  nginx运行的父级进程号。
pid /run/nginx.pid;

#  events模块包括了Nginx中处理链接的全部设置。
events {
	#  设置了一个worker进程可以同时打开的链接数。
	worker_connections 768;
	
	#  开启后Nginx在收到新链接的请求通知时,尽可能接受链接。
	#  multi_accept on;
}

#  http模块是处理请求的核心。
http {

	## 
	#  Basic Settings
	## 
	
	#  直接从磁盘上读取数据到操作系统缓冲,开着就好。
	sendfile on;
	
	#  配置 Nginx 在一个包中发送全部的头文件,而不是一个一个发送。
	tcp_nopush on;
	
	#  配置 Nginx 不要缓存数据,应该快速的发送小数据(这仅仅应该用于频繁发送小的碎片信息而无需立刻获取响应的,需要实时传递数据的应用中)。
	tcp_nodelay on;
	
	#  指定了与客户端的 keep-alive 链接的超时时间。服务器会在这个时间后关闭链接。我们可以降低这个值,以避免让 worker 过长时间的忙碌。
	keepalive_timeout 65;
	
	types_hash_max_size 2048;
	#  server_tokens off;

	#  server_names_hash_bucket_size 64;
	#  server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	## 
	#  SSL Settings
	## 

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #  Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	## 
	#  Logging Settings
	## 

	#  确定了 Nginx 是否保存访问日志。将这个设置为关闭可以降低磁盘 IO 而提升速度。
	access_log /var/log/nginx/access.log;
	
	#  设置 Nginx 应当记录临界错误。
	error_log /var/log/nginx/error.log;

	## 
	#  Gzip Settings
	## 

	#  设置 nginx gzip 压缩发送的数据。这会减少需要发送的数据的数量。
	gzip on;
	
	#  为指定的客户端禁用 gzip 功能。
	gzip_disable "msie6";

	#  gzip_vary on;
	
	#  允许或禁止基于请求、响应的压缩。设置为 any,就可以 gzip 所有的请求。
	#  gzip_proxied any;
	
	#  设置了数据压缩的等级。等级可以是 1-9 的任意一个值,9 表示最慢但是最高比例的压缩。
	#  gzip_comp_level 6;
	
	#  gzip_buffers 16 8k;
	#  gzip_http_version 1.1;
	
	#  设置进行 gzip 的类型。有下面这些,不过还可以添加更多。
	#  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	## 
	#  Virtual Host Configs
	## 

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
	limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;  #  加入这行
}

#  邮件模块
# mail {
# 	#  See sample authentication script at:
# 	#  http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
# 	#  auth_http localhost/auth.php;
# 	#  pop3_capabilities "TOP" "USER";
# 	#  imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
# 	server {
# 		listen     localhost:110;
# 		protocol   pop3;
# 		proxy      on;
# 	}
# 
# 	server {
# 		listen     localhost:143;
# 		protocol   imap;
# 		proxy      on;
# 	}
# }

实现流量限制具体配置

  • 修改 /etc/nginx/sites-available/default 配置文件
location / {
	#  First attempt to serve request as file, then
	#  as directory, then fall back to displaying a 404.
	
	#  限制向客户端传送响应的速率限制。参数 rate 的单位是字节/秒,设置为 0 将关闭限速。nginx 按连接限速,所以如果某个客户端同时开启了两个连接,那么客户端的整体速率是这条指令设置值的 2 倍。
	limit_rate_after 3m;
	
	#  设置不限速传输的响应大小。当传输量大于此值时,超出部分将限速传送。
	limit_rate 20k;
	
	try_files $uri $uri/ =404;
}

传输量限制为 3m,速率限制为 20k/s。

  • 测试下载
$ wget http://121.5.206.236/music.m4a
--2022-03-01 15:04:53--  http://121.5.206.236/music.m4a
Connecting to 121.5.206.236:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10506987 (10M) [audio/x-m4a]
Saving to: ‘music.m4a’

music.m4a            41%[=======>            ]   4.14M  28.0KB/s    eta 1m 55s

在 nginx 访问根目录放入文件大小大于3m的文件,看到传输量大于设定值的部分会受到限制。

实现并发连接数限制的具体配置

  • 修改 /etc/nginx/nginx.conf 配置文件
http {
	......
	......
	#  定义一个叫one的记录区,总容量为10M。
	limit_conn_zone  $binary_remote_addr  zone=one:10m;
	......
	......
}
  • 修改 /etc/nginx/sites-available/default 配置文件
location / {
	#  First attempt to serve request as file, then
	#  as directory, then fall back to displaying a 404.
	
	#  限制根目录下,一个会话只能进行一个连接。简单点说,一个IP只能发起一个连接,多过一个,一律503。
	limit_conn one 1;
	
	try_files $uri $uri/ =404;
}
  • 测试并发

15325592-0c74fd401c5277ae.png

15325592-e382b3c8ac4c2db9.png

访问控制配置

  • 修改 /etc/nginx/sites-available/default 配置文件
location / {
	#  First attempt to serve request as file, then
	#  as directory, then fall back to displaying a 404.
	
	#  IPv4的网络中10.1.1.0/16和192.168.1.0/24 IP段拒绝访问,10.0.2.14 IP拒绝访问。
	deny  10.0.2.14;
	deny  192.168.1.0/24;
	deny  10.1.1.0/16;
	
	#  对于 IPv6 的网络,2001:0db8::/32 拒绝访问。
    deny 2001:0db8::/32;
	
	#  除了以上拒绝访问的,其他IP均可访问。
    allow all;
	
	try_files $uri $uri/ =404;
}
  • 测试访问
#  创建访问文件
$ echo "Hello World" > /var/www/html/index

$ curl 121.5.206.236/index
Hello World

$ curl 10.0.2.14/index
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

DDOS预防配置

  • 修改 /etc/nginx/nginx.conf 配置文件
http {
	......
	......
	#  定义一个叫one的记录区,总容量为10M,限制频率为每秒10个请求。
	limit_conn_zone  $binary_remote_addr  zone=one:10m;
	......
	......
}
  • 修改 /etc/nginx/sites-available/default 配置文件
location / {
	#  First attempt to serve request as file, then
	#  as directory, then fall back to displaying a 404.
	
	#  允许超过频率限制的请求数不多于5个,超时的请求不被延迟处理。
	limit_req zone=one burst=5 nodelay;
	
	try_files $uri $uri/ =404;
}
  • ab 压力测试
#  安装ab压测工具
$ apt install apache2-utils
#  其中-n代表每次并发量,-c代表总共发送的数量。这条命令表示10个请求一次并发出去。
$ ab -n 10 -c 10  http://localhost/index
  • 查看错误日志
$ tail /var/log/nginx/error.log
2022/03/01 16:07:43 [error] 17247# 17247: *21 limiting requests, excess: 6.000 by zone "one", client: 127.0.0.1, server: 121.5.206.236, request: "GET /index HTTP/1.0", host: "localhost"
2022/03/01 16:07:43 [error] 17247# 17247: *22 limiting requests, excess: 6.000 by zone "one", client: 127.0.0.1, server: 121.5.206.236, request: "GET /index HTTP/1.0", host: "localhost"
2022/03/01 16:07:43 [error] 17247# 17247: *23 limiting requests, excess: 6.000 by zone "one", client: 127.0.0.1, server: 121.5.206.236, request: "GET /index HTTP/1.0", host: "localhost"
2022/03/01 16:07:43 [error] 17247# 17247: *24 limiting requests, excess: 6.000 by zone "one", client: 127.0.0.1, server: 121.5.206.236, request: "GET /index HTTP/1.0", host: "localhost"

可以看到,并发了10条请求,有4条访问失败。 转载 Linux-《Nginx-运维基础入门》实验报告 | Yohann's blog (yuhal.com)