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

1. nginx跳转功能补充
1.1 map指令(ngx_http_map_module)
- 问题:负载均衡服务器配置了监控功能后,每隔三秒会向web服务器发送检测请求,后端服务器会有大量的检查日志(访问日志)
- 解决方案:web服务器根据请求的UA进行不同处理
- UA=lb_check(负载均衡的检查),不记录日志
- 其他,则记录日志
- map用法:
- 注意正则匹配符号与后面字符没有空格
shell
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;
- 子配置文件:
shell
[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的区别
指令 | 区别 |
---|---|
return | 301、302跳转,推荐使用return |
rewrite | 使用正则表达式匹配uri、对url进行加工或处理(伪静态)、特殊内部跳转 |
- rewrite的标记
rewrite标记 | 说明 |
---|---|
permanent | 永久跳转 |
redirect | 临时跳转 |
break | 请求结束,后面的location不会再续匹配 |
last | 类似于continue,结束当前location,继续匹配下面location |
1.2.1 重定向
- permanent:301,永久跳转
- 不写或redirect,则是临时跳转
shell
[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 文件,适合频繁更新的网站。
- SEO 优化 :静态化 URL 更易被搜索引擎收录(如
- 测试:break
shell
[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 ...notice
location / {
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
shell
[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 ...notice
location / {
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证书
shell
[root@web01 /etc/nginx/keys]# rz
[root@web01 /etc/nginx/keys]# ll
-rw-r--r-- 1 root root 4127 7月 1 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 Download
inflating: 520skx.com.pem
inflating: 520skx.com.key
2.2.2 编辑子配置文件
shell
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模块
shell
[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
- 修改子配置文件:
shell
[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服务器配置
shell
[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配置
shell
[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
shell
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 配置优化
shell
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期-孙克旭」一起进行文档协作