web服务与nginx
web服务
Linux Web 服务是指在 Linux 操作系统上运行,用于处理 HTTP/HTTPS 请求、提供网页内容或 API 服务的软件,核心以Apache、Nginx和Lighttpd为主,覆盖从通用场景到高性能、轻量场景的不同需求。
常见问题与优化
- 端口冲突:Web 服务默认使用 80(HTTP)和 443(HTTPS)端口,若冲突需通过配置文件修改
listen指令(如listen 8080;)。 - HTTPS 配置:需申请 SSL 证书(如 Let’s Encrypt 免费证书),在配置文件中添加
ssl_certificate(证书路径)和ssl_certificate_key(私钥路径)。 - 性能优化:Nginx 可通过调整
worker_processes(工作进程数,建议等于 CPU 核心数)、worker_connections(单进程最大连接数)提升并发能力。
web选型
中间件:
- web中间件:web服务
- 缓存中间件:Redis
- 数据库中间件: 数据库mysql ShardingSphere,MyCat
| 中间件说明 |
|---|
| 代码(网站代码) |
| 服务<===中间件 |
| 系统 |
| 硬件 |
| web 中间件选型 | 说明 |
|---|---|
| nginx | 目前使用最多 web 服务,使用简单。性能优. |
| apache(httpd) | 之前使用多,现在份额基本都被 nginx 或其他占用. |
| nginx 二次开发版本 | tengine (淘宝,基于 nginx) 需要编译安装. |
| nginx 二次开发版本 | openresty :nginx+lua 模块 lua 语言,给 nginx 增加语言功能. |
| cady | 新型 web 服务,性能高,golang 写的. |
| …… | |
| 云厂商的 web | cloudflare web 服务器 |
| 特定场景 | tomcat (运行 java 代码)…… |
环境准备
ngx安装方案
- yum/apt---->不一定是最新的
- 编译安装:增加/关闭nginx功能
环境
| 角色 | 主机名 | ip |
|---|---|---|
| web服务器 | web01 | 10.0.0.7/172.16.1.7 |
| web服务器 | web02 | 10.0.0.8/172.16.1.8 |
- 配置yum源(注意修改releasever部分为7)
vim /etc/yum.repos.d/nginx.repo
-----------------------------
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/8/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
- ubt配置nginx源
vim /etc/apt/sources.list
#在末尾添加(jammy对应22.04,其他版本需替换代号)
deb http://nginx.org/packages/ubuntu/ jammy nginx
deb-src http://nginx.org/packages/ubuntu/ jammy nginx
#导入官方 GPG 密钥(确保包安全性)
wget https://nginx.org/keys/nginx_signing.key
sud apt-key add nginx_signing.key
apt update
apt install -y nginx
- 安装
yum install -y nginx
- 查看端口80与进程nginx
ps -ef | grep nginx
ss -lntup | grep nginx
- 启动
systemctl enable --now nginx
- 查看防火墙
systemctl stop firewalld #ufw
#或放行端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
- 浏览器访问
ip

nginx配置文件
| nginx配置 | 说明 | 配置建议 |
|---|---|---|
| 主配置文件 | /etc/nginx/nginx.conf | 避免配置文件内容过多,全局核心 |
| 子配置文件(如果有) | include /etc/nginx/conf.d/*.conf | 站点信息写到子配置文件 |

- 主配置文件内容
nginx.conf
[root@web01 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;#notice级别
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
- main变量的函数
| 变量名 | 核心含义 | 实际示例(对应日志中的值) |
|---|---|---|
$remote_addr | 客户端 IP(无代理时为真实 IP,有代理时可能是代理 IP) | 192.168.1.100 或 203.0.113.5 |
$remote_user | 远程认证用户(仅 HTTP 认证生效,否则为 -) | - 或 admin |
$time_local | 服务器本地时间(格式:dd/Mon/yyyy:HH:mm:ss 时区) | 12/Oct/2025:14:30:45 +0800 |
$request | 完整请求信息(请求方法 + 请求 URL + 协议版本) | "GET /index.html HTTP/1.1" 或 "POST /api/login HTTP/2.0" |
$status | HTTP 响应状态码(表示请求处理结果) | 200(成功)、404(未找到)、500(服务器错误) |
$body_bytes_sent | 发送给客户端的响应体字节数(不含响应头) | 1520(表示响应体大小为 1520 字节) |
$http_referer | 客户端来源页面(Referer 头,直接访问时为 -) | "https://example.com/home" 或 - |
$http_user_agent | 客户端用户代理(浏览器 / 设备信息,包含类型、版本、系统) | "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/118.0.0.0" |
$http_x_forwarded_for | 代理链中的真实客户端 IP(X-Forwarded-For 头,无代理时为 -) | "192.168.1.100, 203.0.113.5" 或 - |
- 子配置文件
/etc/nginx/conf.d/*.conf
[root@web01 ~]# egrep -v '^$|#' /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
主配置文件nginx.conf
核心区域:用户,错误日志
events区域:连接数
http区域(处理http/https请求)
- 访问日志格式与日志
- include /etc/nginx/conf.d/*.conf
- xxxx.conf 站点
- server区域:{}
- listen 80;端口
- server_name 域名
- root 指定站点目录
- location / {
- index 指定站点首页文件;
- }
- server区域:{}
- xxxx.conf 站点
403-404故障复现
403故障复现,首页文件不存在
#先查看文件内容
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf | egrep -v '^$|#'
server {
listen 80;
server_name www.songhong.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
#将指定文件移动到其他地方
mv /usr/share/nginx/html/index.html nginx_web/
[root@web01 ~]# ls /usr/share/nginx/html/
50x.html
- 查看错误日志
tail -f /var/log/nginx/error.log
#directory index of "/usr/share/nginx/html/" is forbidden
#/usr/share/nginx/html/是被禁止的

- 查看访问日志
tail -f /var/log/nginx/access.log

403故障复现,权限被拒绝
#修改文件权限
[root@web01 /usr/share/nginx/html]# ll
总用量 8
-rw-r--r-- 1 root root 497 4月 23 2025 50x.html
-rw-r--r-- 1 root root 615 4月 23 2025 index.html
#修改文件权限
[root@web01 /usr/share/nginx/html]# chmod 600 index.html
[root@web01 /usr/share/nginx/html]# ll
总用量 8
-rw-r--r-- 1 root root 497 4月 23 2025 50x.html
-rw------- 1 root root 615 4月 23 2025 index.html
- 查看错误日志
tail -f /var/log/nginx/error.log
#Permission denied

- 查看访问日志
tail -f /var/log/nginx/access.log

浏览器访问ip

404故障复现,指定文件不存在,站点目录不存在
#查看子配置文件
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf | egrep -v '^$|#'
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
#修改指定目录名字
[root@web01 ~]# cd /usr/share/nginx/
[root@web01 /usr/share/nginx]# ls
html
[root@web01 /usr/share/nginx]# mv html html-bak
- 查看错误日志
tail -f /var/log/nginx/error.log

- 查看访问日志
tail -f /var/log/nginx/access.log

浏览器访问ip

部署小鸟飞飞
通过网盘分享的文件:bird.tar.gz
链接: https://pan.baidu.com/s/1CKcPYwQjoAqOUHVLeM5_JA?pwd=fhbs 提取码: fhbs
–来自百度网盘超级会员v2的分享
[root@web01 ~]# ls
anaconda-ks.cfg bird.tar.gz initial-setup-ks.cfg
cat >/etc/nginx/conf.d/bird.songhong.com.conf<<EOF
server{
listen 80;
server_name bird.songhong.com;
root /app/code/bird/;
location{
index index.html;
}
}
EOF
nginx -t #检查语法
[root@web01 ~]# mkdir -p /app/code/bird/
[root@web01 ~]# tar -xf bird.tar.gz -C /app/code/bird/
[root@web01 ~]# systemctl reload nginx
curl -H Host:bird.songhong.com http://10.0.0.7/
- 在windows的hosts文件设置DNS解析
C:\Windows\System32\drivers\etc\hosts
10.0.0.7 bird.songhong.com

部署霸王游戏机
通过网盘分享的文件:FC小霸王怀旧游戏机-HTML源码.zip
链接: https://pan.baidu.com/s/1cLyYq-1XpNupojLvBbVTaw 提取码: 2nje
–来自百度网盘超级会员v2的分享
cat >/etc/nginx/conf.d/90.songhong.com.conf<<EOF
server{
listen 80;
server_name 90.songhong.com;
root /app/code/90/;
location / {
index index.html;
}
}
EOF
[root@web01 ~]# mkdir -p /app/code/90/
[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 ~]# unzip FC小霸王怀旧游戏机-HTML源码.zip -d /app/code/90/
[root@web01 ~]# systemctl reload nginx
- 在windows的hosts文件设置DNS解析
C:\Windows\System32\drivers\etc\hosts
10.0.0.7 90.songhong.com

- 在wireshark抓包
#指定站点抓包
tcp.port == 80 and http ~ "90.songhong.com"

搭建共享站点
-
搭建公司内部使用的站点:共享软件
-
要求:
- 用户打开网站后,显示网站目录下的内容,而不是显示指定首页文件(首页文件不存在)
- 使用8848端口
- 网站现在只能局域网访问,网段限制,类似rsync,allow
-
站点目录:/app/code/share/目录
-
可以不用配置域名
安全限制
#限制访问-开始
allow 10.0.0.1;
allow 172.16.1.0/24;
deny all
#限制访问-结束
共享站点必备,目录列表功能(没有首页文件)
#首页文件不存在,不用403,要显示目录内容
#目录列表功能
autoindex on;
#关闭显示字节大小,人类可读
autoindex_exact_size off;
#根据Linux时间显示文件时间,否则相差8小时
autoindex_localtime on;
字符集
#支持中文
charset utf8;
开始搭建
cat >/etc/nginx/conf.d/share.conf<<EOF
server{
listen 8848;
root /app/code/share/;
#限制访问-开始
allow 10.0.0.1;
allow 172.16.1.0/24;
deny all;
#限制访问-结束
#目录列表功能
autoindex on;
#关闭显示字节大小,人类可读
autoindex_exact_size off;
#根据Linux时间显示文件时间,否则相差8小时
autoindex_localtime on;
#支持中文
charset utf8;
location / {
index index.html;
}
}
EOF
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# ls
anaconda-ks.cfg FC小霸王怀旧游戏机-HTML源码.zip
bird.tar.gz initial-setup-ks.cfg
[root@web01 ~]# mkdir -p /app/code/share/
[root@web01 ~]# mv FC小霸王怀旧游戏机-HTML源码.zip /app/code/share/
[root@web01 ~]# mv bird.tar.gz /app/code/share/
[root@web01 ~]# systemctl reload nginx
访问ip:8848

https://www.yuque.com/lidao996/errors/gw50d8257pmkw0z2?singleDoc#
故障分类:
🔶 服务启动ngx,php,mysql无法启动. 看日志,检查语法. 数据库看日志.
🔶 网站无法访问
1️⃣ 直接显示状态码 403(权限,首页文件),404,500,502(请求通过ngx进行转发,后面没有人接着.),504,xxxxx
- 根据状态码分析,可能原因
- 看日志错误日志,访问日志.
- 找出对应的日志分析.
2️⃣ 浏览器页面有些错误提示,不是状态提示.
-
根据浏览器提示翻译与分析
-
ERR_CONNNECTION_REFUSED连接拒绝. ping/telnet/curl
3️⃣ 无法访问,没有提示.类似于白屏.
F12开启调试模式,查看页面对应的状态码.
windows ping/curl linux curl 检查
根据ngx处理用户请求流程检查.
.网站访问慢,比较复杂
nginx日志
错误日志
- 指令error_log
| error_log指令 | 说明 |
|---|---|
| 语法格式: | error_log file 路径; |
| 默认: | error_log logs/error.log error; |
| 上下文(放在哪) | main,http,mail,steam,server,location |
日志级别
debug,info,notice,warn,error,crit,alert,or,emerg
一般使用notice,调试测试的时候可以选用debug,线上生产环境禁用DEBUG级别
访问日志
- log_format 定义访问日志的格式
- access_log 类似于error_log指定日志的位置和格式
| log_format指令 | 说明 |
|---|---|
| Syntax: | log_fromat name ‘格式内容(包含变量和固定字符串)’; |
| Default: | log_fromat combined “…”; |
| ;Context: | http |
- 规范网站的访问日志和错误日志
server {
listen 80;
server_name bird.songhong.com;
root /app/code/bird/;
error_log /var/log/nginx/bird.songhong.com_error.log notice; #指定错误日志位置,级别
access_log /var/log/nginx/bird.songhong.com_access.log main; #指定访问日志位置,使用main格式
location / {
index index.html;
}
}
- 访问日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
| 访问日志格式 (ngx 变量) | 说明 | 分析 |
|---|---|---|
| $remote_addr | 远程地址 (客户端 ip) | 分析日志,客户端 ip 次数. |
| $remote_user | 远程用户 (ngx 简单认证功能用户) | 一般都是空的 - |
| $time_local | 时间 | 分析的时候注意格式. |
| $request | http 请求起始行 3 个内容 请求方法 URI http/1.1 | 分析核心:请求方法 URI |
| $status | 状态码 | 分析每个状态码次数 |
| $body_bytes_sent | 服务端→客户端:返回数据的大小 | 可以用于统计流量情况. |
| $http_referer | 资源 / 请求从哪里来的,从哪里跳转过来的 | 可以用于分析网站访问的情况 (跳转)搜索引擎竞价分析 SEM. |
| $http_user_agent | 客户端浏览器. | 可以分析浏览器类型,可以分析是否有异常攻击. |
| “$http_x_forwarded_for” | 代理 / 负载均衡场景下,web 节点用于记录客户端 ip 地址. |
location规则
- location与if类似
- location针对请求的uri进行判断过滤
- if结合各种nginx变量实现判断
| url地址 | URI部分 |
|---|---|
| http://www.songhongfei.com | / |
| http://www.songhongfei.com/song.txt | /song.txt |
| http://www.songhongfei.com/img/song.jpg | /img/song.jpg |
| http://www.songhongfei.com/xxxxx/ddd/ddd/d/ddd | /xxxxx/ddd/ddd/d/ddd |
location匹配uri写法说明
| location 匹配 uri 写法 | 说明 | 补充说明 |
|---|---|---|
location / {} | 默认规则,其他 location 规则都失败了,这个保底. | |
location /lidao/ {} | 用于匹配对应的 URI http://www.oldboylinux.cn/lidao/、http://www.oldboylinux.cn/lidao/lidao.txt;不匹配http://www.oldboylinux.cn/lidao996/ | 匹配路径 |
location ~ 正则 {} | 支持正则匹配 URI, 区分大小写 | 匹配图片,静态资源 |
location ~* 正则 {} | 支持正则匹配 URI, 不区分大小写 | 匹配图片,静态资源 |
location = 内容 | 精确匹配,一般配合error_page指定错误页面 | |
location ^~ 内容 | 不是正则,匹配字符,优先级高,用的较少. | |
location @xxx | 命名的 location, 内部跳转 |
优先级顺序
Nginx 对 location 的匹配遵循以下优先级(从高到低):
- 精确匹配(
=) → 2. 前缀优先匹配(^~) → 3. 正则匹配(~/~\*,按配置顺序匹配第一个) → 4. 普通前缀匹配(/开头)
示例
- 精确匹配(
=)
location = / {
# 仅匹配请求为根路径(http://example.com/)
root /usr/share/nginx/html;
index index.html;
}
location = /api/info {
# 仅匹配 http://example.com/api/info
proxy_pass http://backend:8080;
}
- 前缀优先匹配(
^~)
location ^~ /static {
# 匹配所有以 /static 为前缀的请求(如 /static/css/main.css、/static/js/app.js)
root /var/www;
expires 30d; # 设置缓存过期时间
}
- 正则匹配(
~/~*)
# 区分大小写匹配 PHP 脚本,转发到 PHP-FPM
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 不区分大小写匹配图片文件,设置长缓存
location ~* \.(jpg|png|gif)$ {
root /var/www/images;
expires 30d;
}
- 普通前缀匹配(
/开头)
# 匹配所有以 /blog 为前缀的请求(如 /blog、/blog/article/123)
location /blog {
proxy_pass http://blog-backend;
}
# 根路径的默认匹配(优先级最低,通常作为“兜底”规则)
location / {
root /usr/share/nginx/html;
index index.html;
}
- 常见场景
静态资源与动态请求分离
# 静态资源(优先匹配,避免正则干扰)
location ^~ /static {
root /var/www;
expires 30d;
}
# PHP 动态请求(正则匹配,处理 .php 结尾)
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
# 其他请求(默认匹配)
location / {
proxy_pass http://backend;
}
精确接口与通用路径结合
# 精确匹配登录接口
location = /api/login {
proxy_pass http://auth-service;
}
# 匹配所有 /api 前缀的请求(除了 /api/login)
location /api {
proxy_pass http://api-service;
}
URL 重写与正则匹配
# 将 /old-url 永久重定向到 /new-url
location = /old-url {
return 301 /new-url;
}
# 匹配带参数的动态链接,重写为静态路径
location ~ ^/article/(\d+)$ {
rewrite ^/article/(\d+)$ /article.php?id=$1 last;
}
前端简单了解
| 技术 | 作用 | 核心知识点 |
|---|---|---|
| HTML | 构建页面结构(骨架),定义内容的语义(如标题、段落、按钮、表单等)。 | 标签语义化(header/footer/article)、表单控件、多媒体标签(video/audio)、无障碍属性(aria-*)等。 |
| CSS | 美化页面样式(外观),控制布局、颜色、字体、动画等视觉效果。 | 选择器(类、ID、伪类)、盒模型、Flex/Grid 布局、响应式设计(@media)、动画(transition/animation)、CSS 变量等。 |
| JavaScript | 实现页面交互(行为),处理用户操作(如点击、输入)、数据动态更新等。 | 变量 / 数据类型、函数、DOM 操作、事件机制、异步编程(Promise/async/await)、ES6 + 特性(箭头函数、解构赋值等)。 |
案例01小鸟飞飞设置404
- 给小鸟飞飞网站添加一个/admin/功能(页面),只能172.16.1.0/24访问
- 搭建bird小鸟飞飞网站,给网站加速,设置缓存,网站中js,css结尾的文件缓存1天,图片缓存1小时,浏览器缓存
- 指定错误404页面
缓存使用 浏览器缓存
expires 1d;#1天
expires 1h;#1小时
指令
- 配置文件/etc/nginx/conf.d/bird.songhong.com.conf
server{
listen 80;
server_name bird.songhong.com;
root /app/code/bird;
#日志
error_log /var/log/nginx/bird.songhong.com-error.log notice;
access_log /var/log/nginx/bird.songhong.com-access.log main;
location / {
index index.html;
}
#后台访问限制
location /admin{
allow 172.16.1.0/24;
deny all;
index index.html;
}
#静态资源缓存配置
location ~* \.(js|css)$ {
expires 1d;
}
location ~* \.(jpg|png|bmg|jpeg)$ {
expires 1h;
}
#404页面
error_page 404 /404.html;
location = /404.html {
root /app/code/error;
}
}
#检查语法
nginx -t
#重新加载配置文件
systemctl reload nginx
#创建admin目录
mkdir -p /app/code/bird/admin/
#写入index.html内容
echo Hallo word > /app/code/bird/admin/index.html
#创建error目录
mkdir -p /app/code/error/
#将404.html写入内容
vim /app/code/error/404.html
404.html文件内容
<!DOCTYPE html>
<!-- 编码格式,防止乱码 -->
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>404</title>
<!-- 404图标的css样式 -->
<style>
.page-404 {
max-width: 700px;
margin: 50px auto;
text-align: center;
}
.page-404 h1 {
font-size: 22px;
color: #333333;
margin: 0 0 30px;
}
.page-404 a {
display: inline-block;
font-size: 14px;
color: #ffffff;
height: 38px;
line-height: 38px;
padding: 0 20px;
border-radius: 4px;
background-color: #52c41a;
text-decoration: none;
}
.page-404 a:hover {
opacity: .8;
}
.page-404 svg {
display: block;
width: 100%;
height: auto;
max-width: 300px;
margin: 0 auto;
}
</style>
</head>
<body>
<section class="page-404">
<!-- <a href="{SITE_URL}">返回网站首页</a> -->
<!-- 404图标 其中fill为颜色属性,可自行调节 -->
<svg t="1657115709491" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
p-id="2516" width="300" height="300">
<path
d="M260.266667 789.333333c-21.333333 0-38.4-17.066667-38.4-38.4v-59.733333H38.4c-12.8 0-29.866667-8.533333-34.133333-21.333333-4.266667-17.066667-4.266667-29.866667 4.266666-42.666667l221.866667-294.4c8.533333-12.8 25.6-17.066667 42.666667-12.8 17.066667 4.266667 25.6 21.333333 25.6 38.4v256h34.133333c21.333333 0 38.4 17.066667 38.4 38.4s-17.066667 38.4-38.4 38.4H298.666667v59.733333c0 21.333333-17.066667 38.4-38.4 38.4z m-145.066667-179.2h106.666667V469.333333l-106.666667 140.8zM913.066667 742.4c-21.333333 0-38.4-17.066667-38.4-38.4v-59.733333h-183.466667c-12.8 0-29.866667-8.533333-34.133333-21.333334-8.533333-12.8-4.266667-29.866667 4.266666-38.4l221.866667-294.4c8.533333-12.8 25.6-17.066667 42.666667-12.8 17.066667 4.266667 25.6 21.333333 25.6 38.4v256h34.133333c21.333333 0 38.4 17.066667 38.4 38.4s-17.066667 38.4-38.4 38.4h-34.133333v59.733334c0 17.066667-17.066667 34.133333-38.4 34.133333zM768 567.466667h106.666667V426.666667L768 567.466667zM533.333333 597.333333c-46.933333 0-85.333333-25.6-119.466666-68.266666-29.866667-38.4-42.666667-93.866667-42.666667-145.066667 0-55.466667 17.066667-106.666667 42.666667-145.066667 29.866667-42.666667 72.533333-68.266667 119.466666-68.266666 46.933333 0 85.333333 25.6 119.466667 68.266666 29.866667 38.4 42.666667 93.866667 42.666667 145.066667 0 55.466667-17.066667 106.666667-42.666667 145.066667-34.133333 46.933333-76.8 68.266667-119.466667 68.266666z m0-362.666666c-55.466667 0-98.133333 68.266667-98.133333 149.333333s46.933333 149.333333 98.133333 149.333333c55.466667 0 98.133333-68.266667 98.133334-149.333333s-46.933333-149.333333-98.133334-149.333333z"
fill="#978CFF" p-id="2517"></path>
<path d="M354.133333 691.2a162.133333 21.333333 0 1 0 324.266667 0 162.133333 21.333333 0 1 0-324.266667 0Z"
fill="#E3E5FC" p-id="2518"></path>
<path d="M8.533333 832a162.133333 21.333333 0 1 0 324.266667 0 162.133333 21.333333 0 1 0-324.266667 0Z"
fill="#E3E5FC" p-id="2519"></path>
<path
d="M661.333333 797.866667a162.133333 21.333333 0 1 0 324.266667 0 162.133333 21.333333 0 1 0-324.266667 0Z"
fill="#E3E5FC" p-id="2520"></path>
</svg>
<!-- 提示语 其中color为颜色属性,可自行调节 -->
<taro-text-core class="xpx-phone-call" style="color: rgb(151 140 255);font-size: 20;">
尊敬的用户您好,本系统正在进行升级维护,给您带来的不便,敬请谅解。
</taro-text-core>
</section>
</body>
</html>
检查是否配置成功
- 检查admin目录下的html文件
[root@web01 ~]# curl -H "Host: bird.songhong.com" 172.16.1.7/admin/
Hallo word
[root@web01 ~]# curl -H "Host: bird.songhong.com" 172.16.1.7/admin
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>
注意
301 重定向的原因:Nginx 自动补全目录斜杠
当 Nginx 识别到请求的是一个 目录(而非文件)时:
- 如果 URL 以
/结尾(如/admin/),Nginx 会直接查找该目录下的index.html(符合你的index index.html配置),返回内容。 - 如果 URL 不以
/结尾(如/admin),Nginx 会认为这是 “目录的简写形式”,为了规范 URL(避免/admin和/admin/被视为两个不同的 URL 但指向同一目录),会自动发送 301 重定向,告诉客户端 “请访问带斜杠的/admin/”。
检查静态资源缓存配置,访问域名bird.songhong.com
浏览器按F12进入调试模式,鼠标右击,将Cache-Control,Content-Encoding选中


检查404页面是否设置成功
#修改birdm
[root@web01 ~]# cd /app/code/
[root@web01 /app/code]# ls
90 bird error share
[root@web01 /app/code]# mv bird bird.bak
访问bird.songhong.com

nginx设置默认站点
cp default.conf default.conf.bak
cat /etc/nginx/conf.d/default.conf
------------------------------
server {
listen 80 default_server;#
server_name _;
root /app/code/default/;
charset utf8;
return 418;
# location / {
# index index.html index.htm;
#}
}
--------------------------
mkdir -p /app/code/default/
systemctl reload nginx
访问服务器公网ip


wordpress下载主题
通过网盘分享的文件:wordpress主题
链接: https://pan.baidu.com/s/1EMj4Hb9wGYSZyS3-_9nWsg?pwd=z9hw 提取码: z9hw
–来自百度网盘超级会员v2的分享

上传会发现报413状态码,表示上传的文件过大

需要在web服务器设置
cat /etc/nginx/nginx.conf
#在http区域添加
client_max_body_size 100m;
systemctl reload nginx
继续上传,会发现新的错误,链接已过期

需修改php文件
egrep -n '^upload_max|^post_max' /etc/php.ini
#需修改这两行
672:post_max_size = 8M
825:upload_max_filesize = 2M
#ubt
egrep -n '^upload_max|^post_max' /etc/php/8.3/fpm/php.ini
[root@web01 ~]# egrep -n '^upload_max|^post_max' /etc/php.ini
672:post_max_size = 100M
825:upload_max_filesize = 100M
systemctl reload php-fpm
继续上传,就成功了

网站监控功能
- 检查nginx是否有stub_status模块
nginx -V |& grep stub_status
cat /etc/nginx/conf.d/status.songhong.com.conf
----------------------------------------
server{
listen 80;
server_name status.songhong.com;
error_log /var/log/nginx/status-error.log notice;
access_log /var/log/nginx/status-access.log main;
#auth_basic "songhong status";
#auth_basic_user_file /etc/nginx/passwd;
location / {
stub_status;
index index.html;
}
}
--------------------------------------
systemctl reload nginx
curl -H Host:status.songhong.com http://10.0.0.7/
Active connections: 2
server accepts handled requests
22 22 108
Reading: 0 Writing: 1 Waiting: 1
浏览器访问status.songhong.com

| nginx 状态 | 说明 |
|---|---|
| Active connections | 已经建立的连接。数量。实时更新. |
| server accepts | 接收了多少请求 总数,重启 ngx 后清空. |
| handled | 处理了多少请求。总数,重启 ngx 后清空. |
| requests | 用户发出的请求数量。总数,重启 ngx 后清空. |
| Reading 读 | 正在读取多少个请求头。数量。实时更新 |
| Writing 写 | 正在发送多少个响应,数量。实时更新 |
| Waiting | 多少等待。数量。实时更新 |
简单认证功能
cat /etc/nginx/conf.d/status.songhong.com.conf
-----------------------------------
server{
listen 80;
server_name status.songhong.com;
error_log /var/log/nginx/status-error.log notice;
access_log /var/log/nginx/status-access.log main;
#增加认证功能
auth_basic "songhong status";
auth_basic_user_file /etc/nginx/passwd;
location / {
stub_status;
index index.html;
}
}
---------------------------------
htpasswd -bc /etc/nginx/passwd oldboy 1
[root@web01 ~]# htpasswd -bc /etc/nginx/passwd oldboy 1
Adding password for user oldboy
-b 允许在命令行中直接指定密码
-c 创建新的密码文件
chmod 600 /etc/nginx/passwd
chown -R nginx:nginx /etc/nginx/passwd
systemctl reload nginx
| 配置指令 | 作用说明 |
|---|---|
auth_basic "songhong status"; | 开启 HTTP 基本认证,引号中的字符串(songhong status)是认证弹窗的提示信息(用户访问时会看到)。若设置为 auth_basic off; 则关闭认证。 |
auth_basic_user_file /etc/nginx/passwd; | 指定存储用户名和加密密码的文件路径(此处为 /etc/nginx/passwd),Nginx 会读取该文件验证用户凭据。 |
浏览器访问status.songhong.com

- 500内部错误

#这个错误是passwd密码文件没有设置nginx用户所有者
chown -R nginx:nginx /etc/nginx/passwd
#刷新一下
更多推荐

所有评论(0)