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
- 安装
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;
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 指定站点首页文件;
- }
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
故障分类:
🔶 服务启动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处理用户请求流程检查.
4.网站访问慢,比较复杂