当前位置: 首页 > news >正文

day045-nginx跳转功能补充与https

文章目录

  • 0. 老男孩思想-高效记忆
  • 1. nginx跳转功能补充
    • 1.1 map指令(ngx_http_map_module)
    • 1.2 rewrite
      • 1.2.1 重定向
      • 1.2.2 伪静态
  • 2. https
    • 2.1 申请ssl数字证书
    • 2.2 配置ssl证书
      • 2.2.1 上传ssl证书
      • 2.2.2 编辑子配置文件
      • 2.2.3 测试
    • 2.3 使用http2协议
    • 2.4 为博客站点配置ssl证书
      • 2.4.1 lb服务器配置
      • 2.4.2 web服务器中,添加php配置
      • 2.4.3 数据库修改
      • 2.4.4 测试
    • 2.5 配置优化
  • 3. 思维导图

0. 老男孩思想-高效记忆

在这里插入图片描述

1. nginx跳转功能补充

1.1 map指令(ngx_http_map_module)

  • 问题:负载均衡服务器配置了监控功能后,每隔三秒会向web服务器发送检测请求,后端服务器会有大量的检查日志(访问日志)
  • 解决方案:web服务器根据请求的UA进行不同处理
    • UA=lb_check(负载均衡的检查),不记录日志
    • 其他,则记录日志
  • map用法:
    • 注意正则匹配符号与后面字符没有空格
map $status $loggable {~^[23] 0;404 1;403 1;"~*405|^5" 1;default 1;
}
如果$status状态码,是以2或3开头,则$loggable值0
如果$status状态码是404,则$loggable值1
如果状态是405或5开头的, 则$loggable值1
默认情况类似于case语句中*,默认则$loggable值1# if=0,则不记录日志,if=1,记录日志
access_log /var/log/nginx/lb_access.log main if=$loggable;
  • 子配置文件:
[root@web01 /etc/nginx/conf.d]# cat lb.oldboy.cn.conf 
map $http_user_agent $log {"~*lb_check|curl|wget"	0;default 	1;
}
server {listen 80;server_name lb.oldboy.cn;root /app/code/lb;access_log /var/log/nginx/lb.oldboy.cn-access.log main if=$log;error_log /var/log/nginx/lb.oldboy.cn-error.log notice;location / {index index.html;}
}
  • 测试

在这里插入图片描述

1.2 rewrite

  • rewrite与return的区别
指令区别
return301、302跳转,推荐使用return
rewrite使用正则表达式匹配uri、对url进行加工或处理(伪静态)、特殊内部跳转
  • rewrite的标记
rewrite标记说明
permanent永久跳转
redirect临时跳转
break请求结束,后面的location不会再续匹配
last类似于continue,结束当前location,继续匹配下面location

1.2.1 重定向

  • permanent:301,永久跳转
  • 不写或redirect,则是临时跳转
[root@web01 /etc/nginx/conf.d]# cat baidu.oldboy.cn.conf
server {listen 80;server_name baidu.oldboy.cn;
#	return 301 http://www.baidu.com$request_uri;rewrite ^(.*)$ http://www.baidu.com$1 permanent;
}

在这里插入图片描述

1.2.2 伪静态

伪静态(Pseudo-Static) 是一种通过服务器技术(如 URL 重写)将动态网页(如 PHP、ASP 等生成的页面)的 URL 转换成类似静态网页(如 .html)的形式,但实际上仍然是动态生成的技术。

  • 伪静态的优点:
    • SEO 优化:静态化 URL 更易被搜索引擎收录(如 /product/1.html/product.php?id=1 更友好)。
    • 用户体验:短且易记的 URL 提升用户信任度。
    • 安全性:隐藏真实动态路径(如 .php),减少攻击面。
    • 兼容性:无需真正生成 HTML 文件,适合频繁更新的网站。

  • 测试:break
[root@web01 /etc/nginx/conf.d]# cat flag.oldboy.cn.conf 
server {listen 80;server_name flag.oldboy.cn;root /app/code/flag;error_log /var/log/nginx/flag-error.log notice;rewrite_log on; #需要错误日志debug ...noticelocation / {rewrite /1.html /2.html break;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /b.html;}location /3.html {rewrite /3.html /a.html;}
}
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.oldboy.cn http://10.0.0.7/1.html
2.html url
  • last
[root@web01 /etc/nginx/conf.d]# cat flag.oldboy.cn.conf 
server {listen 80;server_name flag.oldboy.cn;root /app/code/flag;error_log /var/log/nginx/flag-error.log notice;rewrite_log on; #需要错误日志debug ...noticelocation / {rewrite /1.html /2.html last;rewrite /2.html /3.html;}location /2.html {rewrite /2.html /b.html;}location /3.html {rewrite /3.html /a.html;}
}
[root@web01 /etc/nginx/conf.d]# curl -H Host:flag.oldboy.cn http://10.0.0.7/1.html
b.html url

2. https

HTTPS 是 HTTP 的安全版本,在 HTTP 基础上增加了 SSL/TLS 加密层,确保数据在传输过程中不被窃听或篡改。

2.1 申请ssl数字证书

在这里插入图片描述

在这里插入图片描述

2.2 配置ssl证书

  • nginx的ssl配置官方地址:[Module ngx_http_ssl_module](https://nginx.org/en/docs/http/ngx_http_ssl_module.html)

2.2.1 上传ssl证书

[root@web01 /etc/nginx/keys]# rz[root@web01 /etc/nginx/keys]# ll
-rw-r--r-- 1 root root 4127  71 21:37 19225328_520skx.com_nginx.zip
[root@web01 /etc/nginx/keys]# unzip 19225328_520skx.com_nginx.zip 
Archive:  19225328_520skx.com_nginx.zip
Aliyun Certificate Downloadinflating: 520skx.com.pem  inflating: 520skx.com.key   

2.2.2 编辑子配置文件

cat 520skx.com.conf 
server {listen 80;server_name 520skx.com;access_log off;return 302 https://520skx.com$request_uri;
}
server {listen 443 ssl;server_name 520skx.com;root /app/code/skx;ssl_certificate     /etc/nginx/keys/520skx.com.pem;ssl_certificate_key /etc/nginx/keys/520skx.com.key;location / {index index.html;}
}

2.2.3 测试

在这里插入图片描述

在这里插入图片描述

2.3 使用http2协议

  • 先检查有无安装http2模块
[root@web01 /etc/nginx/conf.d]# nginx -V |& grep http_v2
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=./modules/ngx_http_upstream_check_module --add-module=./modules/ngx_http_upstream_session_sticky_module
  • 修改子配置文件:
[root@web01 /etc/nginx/conf.d]# cat 520skx.com.conf 
server {listen 80;server_name 520skx.com;access_log off;return 302 https://520skx.com$request_uri;
}
server {listen 443 ssl http2; # 此处添加http协议server_name 520skx.com;root /app/code/skx;ssl_certificate     /etc/nginx/keys/520skx.com.pem;ssl_certificate_key /etc/nginx/keys/520skx.com.key;location / {index index.html;}
}

在这里插入图片描述

2.4 为博客站点配置ssl证书

2.4.1 lb服务器配置

[root@lb01 /etc/nginx/conf.d]# cat 520skx.com.conf 
upstream skx_pools {server 172.16.1.7:80;server 172.16.1.8:80;check interval=3000 rise=2 fall=5 timeout=1000 type=http;check_http_send "HEAD / HTTP/1.0\r\nHost: blog.oldboy.cn\r\nUser-Agent: lb_check\r\n\r\n";check_http_expect_alive http_2xx http_3xx;
}
# 配置http重定向
server {listen 80;server_name 520skx.com;access_log off;return 302 https://520skx.com$request_uri; 
}
server {listen 443 ssl;server_name 520skx.com;access_log /var/log/nginx/520skx.com-access.log main;error_log /var/log/nginx/520skx.com-error.log notice;ssl_certificate     /etc/nginx/keys/520skx.com.pem; # 注意ssl证书的位置ssl_certificate_key /etc/nginx/keys/520skx.com.key;location / {proxy_pass http://skx_pools;proxy_set_header Host blog.oldboy.cn; # 此处使用blog域名proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-Ip $remote_addr;}location /lb_status {allow 10.0.0.1;allow 172.16.1.0/24;deny all;check_status;}location /ngx_status {allow 10.0.0.1;allow 172.16.1.0/24;deny all;stub_status;}
}
  • db、nfs服务器也要打开

2.4.2 web服务器中,添加php配置

[root@web01 /etc/nginx/conf.d]# cat blog.oldboy.cn.conf
server {listen 80;server_name blog.oldboy.cn;root /app/code/blog;error_log /var/log/nginx/blog.oldboy.cn-error.log notice;access_log /var/log/nginx/blog.oldboy.cn-access.log main;location / {index index.php;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param HTTPS on; # 添加该参数include fastcgi_params;}
}

2.4.3 数据库修改

  • mysql数据库中,WordPress记录url是http://blog.oldboy.cn,现在需要修改成目前使用的url:https://520skx.com
mysqldump -uroot -p1 -A >db-all.sql  
grep 'http://blog.oldboy.cn' db-all.sql 
sed -i 's#http://blog.oldboy.cn#https://520skx.com#g' db-all.sql 
mysql -uroot -p1 <db-all.sql 

2.4.4 测试

在这里插入图片描述

  • 测试总结:
    • 太过完美

在这里插入图片描述

2.5 配置优化

server {listen 443 ssl;keepalive_timeout 70;ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #指定ssl加密协议的版本 不要加上TLSv1.0不安全.#加密算法. 需要排除不安全的算法#排除null空算法, md5算法ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5;ssl_certificate /usr/local/nginx/conf/cert.pem;ssl_certificate_key /usr/local/nginx/conf/cert.key;#设置https 会话缓存 10MB大小的空间用于存储缓存.ssl_session_cache shared:SSL:10m;#超时时间 10分钟ssl_session_timeout 10m;
...
}

3. 思维导图

https://kdocs.cn/join/gpuxq6r?f=101\r\n邀请你加入共享群「老男孩教育Linux运维99期-孙克旭」一起进行文档协作

http://www.lryc.cn/news/578817.html

相关文章:

  • 安全风险监测预警平台对企业的价值
  • 【AI智能体】基于Coze 制作高质量PPT实战操作详解
  • Android Native 之 inputflinger进程分析
  • flutter flutter_vlc_player播放视频设置循环播放失效、初始化后获取不到视频宽高
  • PyQt5-高级控件-容器StackedWidget
  • 学习笔记(29):训练集与测试集划分详解:train_test_split 函数深度解析
  • Servlet开发流程(包含IntelliJ IDEA项目添加Tomcat依赖的详细教程)
  • 玄机——某学校系统中挖矿病毒应急排查
  • 打造Docker Swarm集群服务编排部署指南:从入门到精通
  • 【公司环境下发布个人NPM包完整教程】
  • 网络协议概念与应用层
  • 解释LLM怎么预测下一个词语的
  • 图像二值化方法及 Python OpenCV 实现
  • 使用v-bind指令绑定属性
  • 【第三章:神经网络原理详解与Pytorch入门】01.神经网络算法理论详解与实践-(1)神经网络预备知识(线性代数、微积分、概率等)
  • 新能源汽车功率级测试自动化方案:从理论到实践的深度解析
  • 如何将文件从 iPhone 传输到 Android(新指南)
  • 网安-XSS-pikachu
  • MUX-VLAN基本概述
  • 【格与代数系统】格与哈斯图
  • 【分明集合】特征函数、关系与运算
  • 【HarmonyOS】鸿蒙使用仓颉编程入门
  • 【1.6 漫画数据库设计实战 - 从零开始设计高性能数据库】
  • UniApp完全支持快应用QUICKAPP-以及如何采用 Uni 模式开发发行快应用优雅草卓伊凡
  • 飞算智造JavaAI:智能编程革命——AI重构Java开发新范式
  • uniapp内置蓝牙打印
  • WPF中Style和Template异同
  • LEFE-Net:一种轴承故障诊断的轻量化高效特征提取网络
  • 设计模式(七)
  • 08跨域