nginx进阶-3(32-34天)学习笔记

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;       #开启nginx站点目录索引功能
            #index  index.html index.htm;
        }
        }
        
        
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

效果图



img

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;       #开启nginx站点目录索引功能
            charset utf-8;      #设置字符集为utf-8编码
                  #index  index.html index.htm;
        }
        }
        
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.#第二步 nginx配置
cd /usr/local/nginx/conf/vhost

#tips:进入到你需要更改的域名配置目录,添加一下子域名就可以了

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


#tips:输入你自己的域名就可以了,再看一下你自己设置的别名可不可以访问

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

#tips2:此过程就是重新编译安装,注意这里是进入到你的nginx安装包路径,不是配置路径也不是发布路径




2.#配置nginx.conf文件

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;{          #这个位置就是定义一个监控status模块  模块名你可以随便取  ,后面监控是域名+你定义的监控模块名就可以了
        stub_status;
        }
        }
        
        
 3.#重启nginx       
 
 nginx -t
 nginx -s reload
 
 nginx -s  stop
 nginx
 
 
 #tips:扩展模块 一般都需要关闭nginx在启动,模块才可生效
 
        

4.#测试

www.it.com/status





#tips:模块注解

    location =/status;{          #这个位置就是定义一个监控status模块  模块名你可以随便取  ,后面监控是域名+你定义的监控模块名就可以了
        stub_status;
        }













02---第二种方式 新建域名配置监控文件(此操作是把监控模块与主域名分开关闭,主域名就是主域名 ,监控模块设置其他域名,但都是一个发布目录)


1.#新建配置文件

#tips:进入发布目录
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

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.错误日志讲解

#打开nginx日志目录(分析)

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;  #这个warn就是一个错误级别,当然你也可以定义其他级别,但是不影响nginx运行
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

include /usr/local/nginx/conf/vhost/*.conf;
}





#tips:

worker_processes  2;
events {
    worker_connections  2024;
}
error_log /usr/local/nginx/logs/nginx-error.log warn;  #这个warn就是一个错误级别,当然你也可以定义其他级别,但是不影响nginx运行





#错误级别讲解:
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


#目的实现:
每个网站都要有对应自己的日志文件,便于管理





#步骤


#1.配置nginx配置目录 (nginx目录)
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;
}






#2.配置nginx网站目录(网站目录配置文件)
cd /usr/local/nginx/conf/vhost/

vi www.conf


#tips:添加是这行内容access_log  /data/logs/www.log  main;
如果没有/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;
        }
        }
     
 
 
 #2.2配置bbs网站目录
 cd /usr/local/nginx/conf/vhost/
 vi bbs.conf
 
 #tips:同样添加日志文件目录到bbs网站的配置文件里
 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;
        }
        }
 
 
 
 
 
 
 #3.检查-重启nginx
 
 nginx -t
 nginx -s reload
 
 nginx -s stop
 nginx
 
 
 
 #4.测试
 
 cd /data/logs
 ls
 
 tips :打开你的网站,不断刷新,查看/data/logs下是否有日志日志
 用tail 来查看,tail可以事实查看文件状态
 
 
 
 
 
 
 
 重点了解
 #5添加的配置日志文件注释
 
$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 =    # 精准匹配 01                             1
2. location ^~   # 带参前缀匹配                             2
3. location ~    # 正则匹配(区分大小写)                     3
4. location ~*   # 正则匹配(不区分大小写)                    4
5. location /a   # 普通前缀匹配,优先级低于带参数前缀匹配。       5
6. location /    # 任何没有匹配成功的,都会匹配这里处理          6

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





#步骤

#tips:需了解字符

^	指定匹配字符串开始
$	指定匹配字符串结束
.	任意非空字符
+	量词,匹配1次或多次
*	匹配0次或多次
[]	匹配括号中的任意一项
?	量词,匹配0次或1次
()	作为一个整体匹配



#需求1 打开网站跳转到百度

1.#配置nginx网站配置目录

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



#tips:重定向脚本注解


这段配置指令来自于 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/ 加上前缀的相同路径。










#需求2 打开网站跳转到https协议
需求解释:就是原先的http80端口不安全,需要把http80端口跳转到安全的https协议443端口,所以


http: 80            

https:443

(需要ssl的证书)


1.#配置nginx网站配置目录

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:一般掌握重定向就可以了,https这个都是在生产环境中成型的,不太需要配置,除非新上线的网站,域名需要配置https















#总结:为什么需要重定向?什么场合?


1.网站迁移或改版: 当网站的域名发生变化、或者网站结构调整(例如从HTTP迁移到HTTPS,或者目录结构变动),可以通过重定向将旧网址引导到新网址,保持用户访问的连贯性,同时也利于搜索引擎更新索引,减少因网址变更带来的流量损失。

2.URL规范化: 为了统一网站的访问入口,可以设置重定向规则,例如去除URL末尾的斜杠“/”、转换大小写字母、去掉多余的参数等,以提升SEO效果及用户体验。

3.维护公告或临时关闭: 当网站进行临时维护时,可以将所有访问重定向到一个公告页面,通知用户网站目前不可用,并给出预计恢复时间。

4.移动端适配: 对于响应式设计之外的网站,可以针对不同设备或分辨率进行重定向,例如将手机和平板用户的访问重定向到专门优化过的移动版网站。

5.清除会话或登录状态: 当用户注销账户后,可以通过重定向将其导航到首页或者其他预设页面,确保用户在注销后不会停留在含有敏感信息的页面。














posted @ 2024-03-24 21:42  三思博客  阅读(45)  评论(0编辑  收藏  举报