nginx进阶-3(33-34天)学习笔记
知识回顾
1. nginx部署单机网站
2.nginx部署多个网站
3.nginx访问方式
4.nginx 安全
5.nginx加密访问
实战
00---nginx企业实战
1.nginx搭建一个文件共享供用户下载的服务器
1.配置nginx文件
cd /usr/local/nginx/conf/vhost
vi bbs.conf
1.1开启站点索引功能+注释index.html索引文件
server {
listen 80;
server_name www.it.com;
location / {
root /html/www;
autoindex on;
}
}
1.2 检查并重启nginx
nginx -t
nginx -s reload
1.3删除你原先网站下的index.html文件(不删掉会影响页面展示,否则还是会打开它的index.html网页)
cd /html/bbs
rm -rf index.html
2.上传文件到发布目录下
cd /html/www
rz(上传或者其他工具上传)
tips:上传你需要上传的文件就可以了,这里需要把你网站的index.html给删掉,注意你设置是那个网站,就删哪个网站的index.html网页文件就可以了
3.测试
10.0.1.106
效果图

2.nginx出现中文乱码如何解决
1.配置nginx文件
cd /usr/local/nginx/vhost
vi www.conf
server {
listen 80;
server_name www.it.com;
location / {
root /html/www;
autoindex on;
charset utf-8;
}
}
tips:
charset utf-8
由于我们这里设置的是网页上传和下载的网页,加上现在网站普遍采用的都是utf-8编码,且都适配utf-8编码,所以为了不让网页出现乱码,加上这个就可以了
nginx -t
nginx -s reload
3.网站页面别名功能
别名==子域名
1.
tips:由于我们是模拟环境,所以再Windows下去做一下域名解析
C:\Windows\System32\drivers\etc
打开hosts文件
添加一下域名
10.0.1.104 www.it.com bbs.it.com blog.it.com it.com
2.
cd /usr/local/nginx/conf/vhost
vi www.conf
server {
listen 80;
server_name www.it.com it.com;
location / {
root /html/www;
index index.html index.htm;
}
}
3.
nginx -t
nginx -s reload
www.it.com 主域名
it.com 别名=子域名
4.网站监控状态页面
01---第一种方式 添加模块
02---第二种方式 新建域名配置监控文件(此操作是把监控模块与主域名分开关闭,主域名就是主域名 ,监控模块设置其他域名,但都是一个发布目录)
1.
tips:yum安装是有默认模块的,可以用nginx -V看一下自己的模块依赖
如果你是编译安装。应该是没有这个模块的,所以这里安装一下
with-http_stub_status_module -----(模块名)
cd /usr/local/src
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make
make install
2.
cd /usr/local/nginx/vhost
vi www.conf
tips:添加一下内容
server {
listen 80;
server_name www.it.com it.com;
location / {
root /html/www;
index index.html index.htm;
}
location =/status;{
stub_status;
}
}
3.
nginx -t
nginx -s reload
nginx -s stop
nginx
4.
www.it.com/status
location =/status;{
stub_status;
}
02---第二种方式 新建域名配置监控文件(此操作是把监控模块与主域名分开关闭,主域名就是主域名 ,监控模块设置其他域名,但都是一个发布目录)
1.
cd /usr/local/nginx/conf/vhost
cp www.conf www1.conf
vi www1.conf
添加以下内容
server {
listen 80;
server_name s.it.com;
stub_status;
}
2.
nginx -t
nginx -s relaod
nginx -s stop
nginx
3.
s.it.com
结果如下
Active connections: 2 :用户连接数
server
accepts:11 接受连接数
handled:11 处理连接数
requests:107 请求连接数
Reading: 0 请求报文 没人点餐
Writing: 1 响应报文
Waiting: 1 队列
5.错误日志讲解
1.
cd /usr/local/nginx/logs 日志目录
error.log
worker_processes 2;
events {
worker_connections 2024;
}
error_log /usr/local/nginx/logs/nginx-error.log warn;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/conf/vhost/*.conf;
}
worker_processes 2;
events {
worker_connections 2024;
}
error_log /usr/local/nginx/logs/nginx-error.log warn;
warn:警告级别 错误日志 但是不影响运行
error: 错误级别 我们服务器出现错误 需要纠正 推荐用这个
debug:调试级别 错误信息详细展示
info:信息级别 显示重要的信息
notice:
6.(自定义配置日志(多个网站))
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 定义日志内容格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
www.it.com 日志 www.log
bbs.it.com 日志 bbs.log
blog.it.com 日志 blog.log
每个网站都要有对应自己的日志文件,便于管理
tips:配置目录 配置目录 配置目录 不是日志目录
cd /usr/local/nginx/conf/
vi nginx.conf
添加如下内容
worker_processes 2;
events {
worker_connections 2024;
}
error_log /usr/local/nginx/logs/nginx-error.log warn;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include /usr/local/nginx/conf/vhost/*.conf;
}
cd /usr/local/nginx/conf/vhost/
vi www.conf
如果没有/data/logs 这个日志存放目录 可以创建一下
mkdir -p /data/logs
server {
listen 80;
server_name www.it.com it.com localhost;
access_log /data/logs/www.log main;
location / {
root /html/www;
index index.html index.htm;
}
location =/status;{
stub_status;
}
}
cd /usr/local/nginx/conf/vhost/
vi bbs.conf
access_log /data/logs/www.log main;
server {
listen 80;
server_name bbs.it.com;
access_log /data/logs/www.log main;
location / {
root /html/www;
index index.html index.htm;
}
location =/status;{
stub_status;
}
}
nginx -t
nginx -s reload
nginx -s stop
nginx
cd /data/logs
ls
tips :打开你的网站,不断刷新,查看/data/logs下是否有日志日志
用tail 来查看,tail可以事实查看文件状态
重点了解
$remote_addr:客户端ip
$remote_user:显示用户的信息
[$time_local]:显示访问网站时间
$request:请求报文方式
$status:状态码
$body_bytes_sent:响应数据信息
"$http_referer":记录调用网站地址信息
$http_user_agent":客户端用什么浏览器访问
"$http_x_forwarded_for"' 客户端真实ip
因为在生产环境中,如果你的nginx服务器上不止一个网站的话,恰好此时领导需要其中一个的日志文件,如果你没有配置,那么几个网站的日志文件肯定会在同一个日志文件里,不便于你做切分 ,所以此时我们就需要给每个网站所生成的配置文件做个单独的日志文件,方便后期维护和管理。
7.location匹配讲解(细细掌握)
location详细配置:
1. location =
2. location ^~
3. location ~
4. location ~*
5. location /a
6. location /
tips:知道匹配规则,知道怎么用
server {
listen 80;
server_name www.it.com
location =/ { 优先级01
return 404;
}
location / { 最后匹配
return 403;
}
location =/abc { 优先03
return 501;
}
location ^~ /pig { 优先级02
return 502;
}
location ~* \.(gif|jpg|jpeg)$ { 优先级03
return 500;
}
}
8.nginx rewrite 实现URL跳转讲解---重定向(nginx重定向)
^ 指定匹配字符串开始
$ 指定匹配字符串结束
. 任意非空字符
+ 量词,匹配1次或多次
* 匹配0次或多次
[] 匹配括号中的任意一项
? 量词,匹配0次或1次
() 作为一个整体匹配
需求1 打开网站跳转到百度
server {
listen 80;
server_name www.it.com it.com localhost ;
access_log /data/logs/www.log main;
rewrite ^/(.*) https://www.baidu.com/$1 permanent;
需求2 打开网站跳转到https协议
http: 80
https:443
^ 指定匹配字符串开始
$ 指定匹配字符串结束
. 任意非空字符
+ 量词,匹配1次或多次
* 匹配0次或多次
[] 匹配括号中的任意一项
? 量词,匹配0次或1次
() 作为一个整体匹配
1.
cd /usr/local/nginx/conf/vhost
vi www.conf
server {
listen 80;
server_name www.it.com it.com localhost ;
access_log /data/logs/www.log main;
rewrite ^/(.*) https://www.baidu.com/$1 permanent;
2.
nginx -t
nginx -s reload
nginx -s stop
nginx
tips:打开网站看跳不跳转百度页面
www.it.com
这段配置指令来自于 Nginx 服务器的重写模块(rewrite module),它的作用是将接收到的请求地址进行重定向。具体来说:
nginx
rewrite ^/(.*) https://www.baidu.com/$1 permanent;
- rewrite:指令关键字,用于在Nginx内部执行URL的重写操作。
- ^/(.*):正则表达式匹配部分,这里的 ^ 表示匹配URL的开始,(.*) 是一个捕获组,它会匹配任何非空字符序列(除了换行符),也就是说,它会匹配从URL开始到结束的所有路径部分。
- https://www.baidu.com/$1:重写的目标地址,其中 $1 是对前面正则表达式中第一个捕获组内容的引用,这意味着原URL中的任何路径都将被复制并粘贴到目标地址的对应位置。
- permanent:重写类型,表示此次重写应以301 Moved Permanently(永久重定向)HTTP状态码的形式告知客户端浏览器,搜索引擎和其他服务这次重定向是永久性的,以便它们可以更新索引和缓存。
因此,这条规则的工作原理是,当Nginx接收到任何以服务器域名开头的请求时,它都会将请求的完整路径(包括查询字符串)重定向到 https://www.baidu.com/ 加上前缀的相同路径。
需求解释:就是原先的http80端口不安全,需要把http80端口跳转到安全的https协议443端口,所以
http: 80
https:443
(需要ssl的证书)
1.
cd /usr/local/nginx/conf/vhost
vi www.conf
server {
listen 80;
server_name www.it.com it.com localhost ;
access_log /data/logs/www.log main;
rewrite ^/(.*) https://www.baidu.com/$1 permanent;
2.
nginx -t
nginx -s reload
nginx -s stop
nginx
1.网站迁移或改版: 当网站的域名发生变化、或者网站结构调整(例如从HTTP迁移到HTTPS,或者目录结构变动),可以通过重定向将旧网址引导到新网址,保持用户访问的连贯性,同时也利于搜索引擎更新索引,减少因网址变更带来的流量损失。
2.URL规范化: 为了统一网站的访问入口,可以设置重定向规则,例如去除URL末尾的斜杠“/”、转换大小写字母、去掉多余的参数等,以提升SEO效果及用户体验。
3.维护公告或临时关闭: 当网站进行临时维护时,可以将所有访问重定向到一个公告页面,通知用户网站目前不可用,并给出预计恢复时间。
4.移动端适配: 对于响应式设计之外的网站,可以针对不同设备或分辨率进行重定向,例如将手机和平板用户的访问重定向到专门优化过的移动版网站。
5.清除会话或登录状态: 当用户注销账户后,可以通过重定向将其导航到首页或者其他预设页面,确保用户在注销后不会停留在含有敏感信息的页面。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南