day34-nginx常用模块

## 0. 网络面试题

网络面试题:
TCP三次握手
TCP四次挥手
DNS解析流程
OSI七层模型
抓包工具 tcpdump
RAID级别区别
开机启动流程
如何实现不同的网段之间通信(路由器)
ip route add 192.168.1.0 255.255.255.0  下一跳的地址或者接口
探测服务器开启了哪些端口(无法登录服务器)
nmap  10.0.0.200
nmap  www.linuxnc.com
查看服务器开启了哪些端口(已经登录服务器)
netstat -tnulp
ss -tnulp

启动服务
systemctl start nginx
systemctl status nginx  # 查看服务状态信息
netstat -tnulp # 查看服务端口是否启动成功

进程
查看进程 ps -auxf静态  top动态
杀死进程 kill pid号
进程放到后台持续运行 nohup xxx &  screen
01.知识点回顾
启动服务: systemctl start nginx
服务对应的端口号:
ssh    22
nginx  80
mysql  3306
redis  6379
rsync  873
02.Nginx 常用模块
模块 1.配置 index 索引列表
创建索引配置文件
[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;				# 中文乱码问题 指定字符集

	location / {
	root /code/index;
 	autoindex on;					# 开启目录索引以列表的方式显示
	autoindex_localtime on;		    # 文件以当前系统时间为准
	autoindex_exact_size off;	    # 以K M G显示文件的大小
	}
}


创建代码目录
[root@web01:conf.d]#mkdir /code/index
在代码目录中创建文件
[root@web01:conf.d]#touch /code/index/1.txt
测试语法并重启nginx
hosts解析
10.0.0.7 www.index.com

浏览器测试访问

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

模块 2.限速模块
[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;

	location / {
	root /code/index;
 	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate_after 100m;			# 前100M不限速
	limit_rate       50k;		    # 100M后限速每秒传输50KB
	}
}

模块 3.Nginx 状态模块
[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;

	location / {
	root /code/index;
 	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate_after 100m;
	limit_rate       50k;
	}

	location /nginx_status {# 浏览器访问www.index.omc/nginx/status
	stub_status;	# nginx状态模块
	}
}
[root@web01:conf.d]#nginx -t

[root@web01:conf.d]#systemctl restart nginx

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Active connections  # 当前活动的连接数
accepts             # 已接收T的总TCP连接数量
handled             # 已处理的TCP连接数量
requests            # 当前http请求数

Reading             # 当前读取请求头数量
Writing             # 当前响应的请求头数量
Waiting             # 等待的请求数,开启了keepalive

# 注意, 一次TCP的连接,可以发起多次http的请求, 如下参数可配置进行验证
keepalive_timeout  0;   # 类似于关闭长连接
keepalive_timeout  65;  # 65s没有活动则断开连接

HTTP请求头部:
accept: text/html	#  请求类型
accept-encoding: gzip, deflate  # 请求编码 gzip压缩
accept-language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.	# 请求语言类型
cache-control:  no-cache	# 请求缓存
connection:  keep-alive	    # 长连接  1.0 1.1 2.0  默认使用1.1
cookie:
wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_4d3dfc2d7997ebbb6f8c4f291b7d3e51=admin%7C1723165561%7CG2Fa5IDQbAEziFeVRE7U2UdILPENaglORCrNzMVWVEq%7Ca08cbc5fb7fb239120a04080b54d308d77358b29fb0e647cb7d410d2b4aaec71; wp-settings-time-1=1723084874		# 会话保持使用 浏览器将cookie用户名密码 请求服务端
host: www.wp.com				   # 请求主机 域名
pragma: no-cache				   # 缓存
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0   # 客户端浏览器的信息

HTTP响应头部:
connection: keep-alive	   # 响应长连接
content-type: text/html; charset=UTF-8  # 响应文件类型
date: Mon, 12 Aug 2024 02:37:50 GMT     # 响应时间
location: http://www.wp.com/		    # 响应主机
server: nginx/1.26.1					# 响应服务的版本号
PHP/7.2.34							    # 响应的PHP版本

模块 4.访问限制模块
[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;

	location / {
	root /code/index;
 	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate_after 100m;
	limit_rate       50k;
	}

	location /nginx_status {
  # 使用场景 公司业务后台 管理页面
	stub_status;
	allow 10.0.0.1;
  # 先设置允许访问此页面的IP地址 10.0.0.1   只允许班长进来
	deny all;
  # 拒绝所有其他IP访问此页面				  其他所有同学拒绝进入
	}
}
[root@web01:conf.d]#nginx -t

[root@web01:conf.d]#systemctl restart nginx

测试
1.使用10.0.0.5 curl
先做hosts解析
[root@lb01:~]#cat /etc/hosts
127.0.0.1
::1
10.0.0.7 www.index.com

[root@lb01:~]#curl www.index.com/nginx_status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>

2.浏览器访问测试

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

第二种使用场景

[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;

	location / {
	root /code/index;
 	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate_after 100m;
	limit_rate       50k;
	}

	location /nginx_status {
	stub_status;
	deny 10.0.0.1;	# 拒绝攻击服务器的IP地址访问此页面
	allow all;			# 允许其他所有的IP地址通过
	}
}

模块 5.登录模块 auth_basic
[root@web01:conf.d]#cat aotu.conf
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;

	location / {
	root /code/index;
 	autoindex on;
	autoindex_localtime on;
	autoindex_exact_size off;
	limit_rate_after 100m;
	limit_rate       50k;
	auth_basic           "Linux94";
  # 指定描述信息 必须存在
  auth_basic_user_file conf/passwd;
  # 用户名和密码位置 /etc/nginx/conf/passwd
	}

	location /nginx_status {
	stub_status;
	deny 10.0.0.1;
	allow all;
	}
}
创建目录
[root@web01:conf.d]#mkdir ../conf
[root@web01:conf.d]#pwd
/etc/nginx/conf.d

生成密码信息:
[root@web01:~]#htpasswd -b -c /etc/nginx/conf/passwd oldboy oldboy
Adding password for user oldboy
[root@web01:~]#cat /etc/nginx/conf/passwd
oldboy:$apr1$Dt1AbFao$07I8LAN5FDl/D0BjQ89tV.

语法测试重启服务
[root@web01:~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01:~]#systemctl restart nginx

浏览器访问www.index.com 登录测试
03.错误代码重定向
...
server {
	listen 80;
	server_name www.index.com;
	charset utf-8,gbk;
	limit_conn conn_zone 1;
	#limit_req zone=req_zone burst=3 nodelay;
	limit_req zone=req_zone burst=3;
	limit_req_status 478;
	error_page 404 478 /error.html;
  # 如果用户访问的页面出现404或者478 都会定向到error.html

	location / {
	root /code/index;			# 代码目录
 	autoindex on;
	autoindex_localtime on;

 ...

error.html的位置
[root@web01:conf.d]#ll /code/index/error.html
-rw-r--r-- 1 root root 51 Aug 12 14:56 /code/index/error.html
[root@web01:conf.d]#cat /code/index/error.html
<img style='width:100%;height:100%;' src=/478.png>

图片的位置
[root@web01:conf.d]#ll /code/index/478.png
-rw-r--r-- 1 root root 294968 Aug 12 14:57 /code/index/478.png
04.location 匹配规则
[root@web01:conf.d]#cat test.conf
server {
    listen 80;
    server_name test.oldboy.com;
    default_type text/html;
    location = / {
    return 200 "configuration A";
    }
    location  / {
    return 200 "configuration B";
    }

    location /documents/ {
    return 200 "configuration C";
    }

    location ^~ /images/ {
    return 200 "configuration D";
    }

    location ~* \.(gif|jpg|jpeg)$ {
    return 200 "configuration E";
    }
}
[root@web01:conf.d]#nginx -t

[root@web01:conf.d]#systemctl restart nginx

LB01测试
[root@lb01:~]#cat /etc/hosts
127.0.0.1
::1
10.0.0.7 www.index.com test.oldboy.com


1.测试精确匹配
[root@lb01:~]#curl test.oldboy.com
configuration A
2.测试访问test.oldboy.com/index.html
[root@lb01:~]#curl test.oldboy.com/index.html
configuration B

3.测试访问
[root@lb01:~]#curl test.oldboy.com/documents/document.html
configuration C

4.测试访问
[root@lb01:~]#curl test.oldboy.com/images/1.gif
configuration D

5.测试访问
[root@lb01:~]#curl test.oldboy.com/documents/1.jpg
configuration E



测试结果: 面试题
=       # 优先级最高 精确匹配
^~      # 正则表达式
~       # 区分大小写
~*      # 不区分大小写
/images # 路径匹配
/       # 默认匹配

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值