Docker 查看容器日志
Docker 查看容器日志
容器出问题怎么排查?先看日志,这些命令要会。
1. 查看容器日志
基本命令
# 查看全部日志
docker logs 容器名
# 查看容器ID
docker logs 容器ID
2. 常用参数
看最后几行
# 最后10行
docker logs --tail 10 容器名
# 最后20行
docker logs --tail 20 容器名
实时跟踪日志(最常用)
docker logs -f 容器名
带时间戳
docker logs -t 容器名
组合使用
# 实时跟踪 + 时间戳 + 最后10行
docker logs -f -t --tail 10 容器名
3. 按时间查看
# 最近10分钟的日志
docker logs --since 10m 容器名
# 最近1小时的日志
docker logs --since 1h 容器名
# 指定时间开始
docker logs --since "2024-01-01T00:00:00" 容器名
4. 多容器日志
docker-compose 查看日志
# 查看所有服务日志
docker-compose logs
# 实时跟踪
docker-compose logs -f
# 查看指定服务
docker-compose logs nginx
# 看最后50行
docker-compose logs --tail 50 nginx
5. 日志持久化
json-file 日志驱动(默认)
- 日志存在 /var/lib/docker/containers/
- 单个容器日志文件
配置日志大小限制
# docker-compose.yml
services:
app:
image: nginx
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
6. 常见排查场景
1)容器启动失败
docker logs 容器名
看最后几行报错信息。
2)接口报错
docker logs -f --tail 100 容器名
实时看请求日志。
3)查某个时间点的错误
docker logs --since "2024-01-01T10:00:00" 容器名 | grep "error"
7. 日志清理
清理单个容器日志
# 找到日志文件
sudo find /var/lib/docker/containers/ -name "*.log"
# 清空日志
sudo sh -c "echo > /var/lib/docker/containers/容器ID/容器ID-json.log"
重启容器自动清理
docker restart 容器名
总结
Docker 查看容器日志要点:
- docker logs 查看日志
- -f 实时跟踪最常用
- --tail 看最后几行
- -t 带时间戳
- --since 按时间查
- docker-compose logs 多容器
- 配置日志大小防止占满磁盘
- 排查问题先看日志
容器运维,看日志是第一步。
Linux 查看系统日志
Linux 查看系统日志
排查问题先看日志,这些命令要记住。
1. 系统日志位置
/var/log/messages # 系统主日志
/var/log/syslog # Debian/Ubuntu 系统日志
/var/log/boot.log # 启动日志
/var/log/cron # 定时任务日志
/var/log/secure # 安全日志(登录、sudo等)
/var/log/nginx/ # Nginx 日志
/var/log/httpd/ # Apache 日志
/var/log/mysql/ # MySQL 日志
2. 基本查看命令
cat:查看全部
cat /var/log/messages
tail:看末尾几行(最常用)
# 看最后10行
tail /var/log/messages
# 看最后20行
tail -n 20 /var/log/messages
# 实时跟踪日志(最常用)
tail -f /var/log/messages
head:看开头几行
head -n 10 /var/log/messages
3. 搜索日志
grep:搜索关键词
# 搜索错误信息
grep "error" /var/log/messages
# 不区分大小写
grep -i "error" /var/log/messages
# 显示行号
grep -n "error" /var/log/messages
# 反向匹配(不包含error)
grep -v "debug" /var/log/messages
结合管道
# 实时日志里搜索错误
tail -f /var/log/messages | grep "error"
# 搜索最近的nginx错误
tail -n 1000 /var/log/nginx/error.log | grep "500"
4. journalctl 查看 systemd 日志
# 查看全部日志
journalctl
# 看最后100行
journalctl -n 100
# 实时跟踪
journalctl -f
# 按服务查看
journalctl -u nginx
# 按时间查看
journalctl --since "1 hour ago"
journalctl --since "2024-01-01" --until "2024-01-02"
5. 常用排查场景
1)服务器登录失败
grep "Failed password" /var/log/secure
2)服务起不来
systemctl status nginx
journalctl -u nginx -n 50
3)磁盘满了
df -h
du -sh /var/log/*
4)定时任务没执行
cat /var/log/cron
6. 日志清理
手动删除大日志
# 查看日志大小
du -sh /var/log/*
# 清空日志(不删除文件)
> /var/log/messages
# 删除旧日志
rm /var/log/messages.1
logrotate 自动切割
- Linux 自带日志切割工具
- 配置文件在 /etc/logrotate.conf
- 一般不用管,自动轮转
总结
Linux 查看日志要点:
- 日志都在 /var/log/ 目录
- tail -f 实时看日志最常用
- grep 搜索关键词
- journalctl 查 systemd 服务日志
- 排查问题先看错误日志
- 日志太大要及时清理
- 系统自动轮转不用手动管
运维排查,看日志是第一步。
Nginx 日志分析技巧
Nginx 日志分析技巧
Nginx 日志是排查问题的重要工具,学会分析日志很重要。
1. 日志位置
默认日志路径:
# 访问日志
/var/log/nginx/access.log
# 错误日志
/var/log/nginx/error.log
2. 日志格式
默认日志格式:
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
3. 常用分析命令
1)查看访问量最多的IP:
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head 10
2)查看访问最多的页面:
awk '{print $7}' access.log | sort | uniq -c | sort -nr | head 10
3)查看状态码分布:
awk '{print $9}' access.log | sort | uniq -c | sort -nr
4)查看404错误:
awk '$9 == 404 {print $7}' access.log | sort | uniq -c | sort -nr
4. 排查常用
1)查看哪个IP访问频繁:
grep "127.0.0.1" access.log
2)查看某个时间段日志:
grep "08/Oct/2026:07" access.log
3)查看错误日志:
tail -f error.log
5. 日志切割
用 logrotate 自动切割:
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 30
compress
notifempty
create 0640 nginx adm
postrotate
systemctl reload nginx
endscript
}
6. 常用场景
- 被攻击了:查访问量最多的IP
- 页面报错:查404/500
- 流量突增:查访问最多的页面
- 爬虫多:查 User-Agent
总结
Nginx 日志分析要点:
- 访问日志查访问情况
- 错误日志排查问题
- awk 统计数据
- 日志自动切割
- 定期分析日志
做运维,看日志是基本功。
Nginx报"no input file specified"错误解决
前言
访问 PHP 页面,直接报 "No input file specified",意思是 PHP-FPM 找不到要执行的 PHP 文件。这是 Nginx 配置里 root 路径不对导致的。
报错长什么样
页面上直接显示:
No input file specified.
没有其他信息,就这么一句话。
常见原因
原因1:root 路径不对
PHP location 里的 root 和 server 里的 root 不一致。
错误示例:
server {
root /var/www/html;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME /var/www/php$fastcgi_script_name;
}
}
这样 PHP 找文件的时候就找错目录了。
正确做法:
server {
root /var/www/html;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
用 $document_root 自动取 server 里的 root。
原因2:文件真的不存在
URL 对应的 PHP 文件根本就没有。
检查:
ls /var/www/html/test.php
原因3:文件权限不对
文件存在,但 Nginx/PHP-FPM 读不了。
解决: 确保文件权限是 644,目录是 755,属主是 www。
完整正确的配置
server {
listen 80;
server_name www.example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
排查步骤
第一步:确认文件在不在
ls /var/www/html/test.php
第二步:看 Nginx 错误日志
tail -f /var/log/nginx/error.log
访问一下报错的页面,看日志里写的路径是什么。
第三步:确认 root 路径
grep root /etc/nginx/conf.d/你的站点.conf
常见坑
坑1:fastcgi_param 写错了
写成了 $request_filename 或者其他变量。
解决: 一定要用 $document_root$fastcgi_script_name。
坑2:伪静态和 PHP 冲突
配了伪静态,导致 PHP 请求被重写到别的地方了。
解决: 确保伪静态规则在 PHP location 之前,而且 PHP location 能正常匹配到 .php 请求。
坑3:多站点 root 不一致
A 站点的 PHP 请求跑到 B 站点去了。
原因: 多个站点的 root 配置不对。
解决: 每个站点的 root 都写清楚,别共用。
坑4:PHP-FPM 配置不对
PHP-FPM 的 www.conf 里的 listen 路径和 Nginx 配置的不一致。
解决: 确认两边的 socket 路径一样。
总结
90% 的情况都是 root 路径不对。
检查这三点:
- root 路径写对了没
- 文件真的存在不
- 文件权限对不对
记住: PHP 配置里一定要用 $document_root$fastcgi_script_name,别写死路径。
遇到问题加QQ23979811 协助处理
Nginx报504 Gateway Timeout超时排查
前言
访问网站提示 504 Gateway Timeout,意思是 Nginx 把请求转发给后端(PHP-FPM 或其他服务),后端半天没响应,Nginx 等不及了就返回 504。本文讲一下 504 的排查思路。
504 和 502 的区别
- 502 Bad Gateway:后端服务直接挂了,连不上
- 504 Gateway Timeout:后端服务在跑,但响应太慢,超时了
常见原因
原因1:PHP 程序执行太慢
一个 PHP 请求要跑 30 秒,Nginx 默认等 60 秒就超时了。
解决: 优化 PHP 代码,或者调大 Nginx 超时时间。
原因2:PHP-FPM 进程不够
PHP-FPM 进程数用完了,新请求排队等着。
解决: 增加 PHP-FPM 进程数。
原因3:后端服务挂了
后端的 Java/Node.js 服务没启动,或者端口不对。
解决: 确认后端服务在跑。
排查步骤
第一步:看 Nginx 错误日志
tail -f /var/log/nginx/error.log
找具体是哪个请求超时了。
第二步:看 PHP-FPM 状态
ps aux | grep php-fpm
看进程数是不是满了。
第三步:直接访问后端
绕过 Nginx,直接访问后端服务:
curl http://127.0.0.1:9000/
看后端自己响应快不快。
调大 Nginx 超时时间
如果程序确实需要跑很久,调大超时时间:
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# 超时时间
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;
}
参数说明:
| 参数 | 说明 |
|---|---|
fastcgi_connect_timeout |
连接后端超时时间 |
fastcgi_send_timeout |
发送请求超时时间 |
fastcgi_read_timeout |
读响应超时时间 |
增加 PHP-FPM 进程数
PHP-FPM 配置文件一般在 /etc/php-fpm.d/www.conf:
pm = static
pm.max_children = 50
改成 50 个进程,重载 PHP-FPM。
常见坑
坑1:只调了 Nginx 超时
调了 Nginx 超时,但 PHP 自己也有超时(max_execution_time)。
解决: 同时改 php.ini:
max_execution_time = 60
坑2:后端服务重启了但 Nginx 没重载
后端服务换了端口,Nginx 还连旧端口。
解决: 改完配置 nginx -s reload。
坑3:大文件上传超时
上传大文件,跑到一半 504。
原因: 上传慢,超时了。
解决: 调大 client_body_timeout 和 fastcgi_read_timeout。
坑4:CDN 也有超时
用了 CDN,CDN 自己也有超时限制。
解决: CDN 那边也调大超时时间。
总结
504 排查三步:
- 看 Nginx 错误日志
- 确认后端服务在跑
- 程序优化 or 调大超时时间
记住: 504 是后端响应太慢,不是 Nginx 本身的问题。
遇到问题加QQ23979811 协助处理
Nginx配置访问日志,日志格式详解
前言
网站出问题了,想查一下谁访问过哪个页面,怎么查?Nginx 的访问日志就记录了所有请求。本文讲一下 Nginx 怎么配置访问日志,日志格式都有什么字段。
默认日志位置
Nginx 默认访问日志在:
/var/log/nginx/access.log
错误日志在:
/var/log/nginx/error.log
查看实时日志
tail -f /var/log/nginx/access.log
日志格式定义
Nginx 自带一个默认格式叫 combined:
log_format combined '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
常用日志变量
| 变量 | 说明 |
|---|---|
$remote_addr |
客户端 IP |
$time_local |
本地时间 |
$request |
请求行(方法+URL+协议) |
$status |
状态码 |
$body_bytes_sent |
发送字节数 |
$http_referer |
来源页面 |
$http_user_agent |
浏览器 UA |
自定义日志格式
想多记点信息,比如响应时间、X-Forwarded-For:
log_format myformat '$remote_addr - [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'rt=$request_time uct="$upstream_connect_time"';
access_log /var/log/nginx/access.log myformat;
单独给某个站点配日志
多站点的时候,每个站点单独记日志:
server {
listen 80;
server_name www.aaa.com;
root /var/www/aaa;
index index.html;
access_log /var/log/nginx/aaa_access.log;
error_log /var/log/nginx/aaa_error.log;
}
关闭日志
不想记日志(不推荐):
access_log off;
常见坑
坑1:用了 CDN,日志里都是 CDN IP
访问日志里记的都是 CDN 的 IP,不是真实用户的。
原因: 请求经过 CDN 转发了。
解决: 记 X-Forwarded-For:
log_format combined '$http_x_forwarded_for - $remote_user [$time_local] ...';
坑2:日志太大占磁盘
访问日志一直写,把磁盘占满了。
解决: 配 logrotate 自动切割日志。
坑3:查不到某个 IP 的访问记录
想查某个 IP 今天访问了多少次,日志里没有。
原因: 日志已经被切割轮转了。
解决: 看昨天的日志文件:
grep "IP地址" /var/log/nginx/access.log.1
坑4:日志权限不对
Nginx 写不了日志。
解决: 确保日志目录属主是 www:
chown -R www:www /var/log/nginx/
常用日志分析命令
统计访问量前10的IP:
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
统计 404 最多的页面:
awk '($9 == 404)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -nr
总结
一行配置开启日志:
access_log /var/log/nginx/access.log;
记住: 日志是排查问题的第一手资料,出事先看日志。
遇到问题加QQ23979811 协助处理
Nginx报404 Not Found原因,文件路径检查
前言
访问网站出现 404 Not Found,意思是 Nginx 找不到文件。大部分情况是路径配置不对。本文讲一下 404 的常见原因和排查方法。
404 的常见原因
原因1:root 路径不对
Nginx 配置里的 root 指向的目录不对,文件不在那个目录里。
检查:
server {
root /var/www/html; # 确认这个路径
}
去服务器上看一下:
ls /var/www/html/
看 index.html 在不在。
原因2:index 首页没配置
访问根目录,Nginx 不知道默认打开哪个文件。
解决: 配置 index:
server {
root /var/www/html;
index index.php index.html index.htm;
}
原因3:伪静态没配好
ThinkPHP、WordPress 这种程序,需要伪静态规则。没配的话,访问文章页就 404。
解决: 加上伪静态规则:
location / {
try_files $uri $uri/ /index.php?$args;
}
原因4:文件权限不对
文件存在,但 Nginx 读不了。
检查:
ls -la /var/www/html/
确保文件权限是 644,目录是 755,属主是 www。
排查步骤
第一步:确认文件真的存在
ls -la /var/www/html/index.html
第二步:确认 Nginx 配置的 root 路径
grep root /etc/nginx/conf.d/你的站点.conf
第三步:看 Nginx 错误日志
tail -f /var/log/nginx/error.log
访问一下 404 的页面,看日志里写的什么。
常见坑
坑1:root 和 alias 搞混了
很多人分不清 root 和 alias。
区别:
root:把请求路径拼到 root 后面alias:把请求路径替换成 alias 后面的路径
举个例子:
请求 /a/test.html
# root 方式
root /var/www;
# 实际找 /var/www/a/test.html
# alias 方式
location /a/ {
alias /var/www/;
}
# 实际找 /var/www/test.html
坑2:首页能访问,内页 404
首页打开正常,点文章就 404。
原因: 伪静态没配。
解决: 给程序配上伪静态规则。
坑3:PHP 文件 404
访问 PHP 文件,直接下载或者 404。
原因: PHP 解析没配置。
解决: 确保有 PHP 的 location 配置:
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
坑4:改了文件名但浏览器还缓存
本地改了文件名,访问还是 404。
原因: 浏览器缓存了旧的 404 响应。
解决: 清浏览器缓存,或者用无痕模式测试。
总结
404 排查三步走:
- 文件在不在?
- root 路径对不对?
- 伪静态配没配?
记住: 排查 404 先看错误日志,比瞎猜快多了。
遇到问题加QQ23979811 协助处理
Nginx报502 Bad Gateway完整排查思路(PHP-FPM场景)
Nginx 出现 502 Bad Gateway 是最常见的报错之一,90% 的情况都是 PHP-FPM 出了问题。下面是完整的排查思路,按顺序一步步来,基本都能解决。
1. 先看 PHP-FPM 是否在运行
systemctl status php-fpm
如果显示 failed 或 inactive,直接重启:
systemctl restart php-fpm
注意:CentOS 7 一般是
php-fpm,Ubuntu 可能是php7.4-fpm之类的,看你装的版本。
2. 看 Nginx 错误日志(最关键)
tail -f /var/log/nginx/error.log
常见错误信息:
错误1:connect() failed (111: Connection refused) while connecting to upstream
说明 PHP-FPM 没启动,或者监听地址不对。
错误2:upstream timed out
PHP 脚本执行超时了,或者 PHP-FPM 进程不够用。
错误3:Primary script unknown
Nginx 配置里的 SCRIPT_FILENAME 路径不对,找不到 PHP 文件。
3. 检查 Nginx 配置是否正确
nginx -t
如果显示 test is successful 说明配置语法没问题。
重点检查 fastcgi_pass 这一行:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000; # 或者 unix:/run/php-fpm/www.sock
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
常见问题:
- fastcgi_pass 地址和 PHP-FPM 实际监听地址不一致
- SCRIPT_FILENAME 路径写错了
4. 检查 PHP-FPM 监听地址
netstat -tlnp | grep php-fpm
或者:
ss -tlnp | grep php-fpm
看 PHP-FPM 是监听在 9000 端口还是 unix sock 文件,然后和 Nginx 配置里的 fastcgi_pass 对应上。
5. 检查 PHP-FPM 进程数是否够
看 PHP-FPM 配置:
vi /etc/php-fpm.d/www.conf
重点参数:
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30
如果访问量大,进程数不够就会 502。
6. 检查 PHP 错误日志
tail -f /var/log/php-fpm/www-error.log
有时候 PHP 代码本身报错,也会导致 502。
7. 其他常见原因
磁盘满了
df -h
内存不够
free -h
如果内存不足,PHP-FPM 进程会被杀掉,也会 502。
SELinux 没关
参考我之前的文章,关掉 SELinux 试试。
排查顺序总结
- 重启 PHP-FPM → 看能不能恢复
- 看 Nginx 错误日志 → 定位具体错误
- 检查 Nginx 配置 → fastcgi_pass 和 SCRIPT_FILENAME
- 检查 PHP-FPM 状态 → 监听地址、进程数
- 检查系统资源 → 磁盘、内存
- 检查 SELinux
遇到服务器、Nginx、MySQL 报错,可以加微信远程协助排查问题。