在当今的IT领域,Linux操作系统和Nginx服务器已成为开发者必备的技能栈。无论是后端开发、DevOps还是系统运维,熟练掌握Linux基础命令和Nginx配置都是职业发展的关键基石。本文将系统性地介绍Linux常用命令、权限管理机制以及Nginx的核心配置方法,帮助读者快速构建起Linux环境下的Web服务能力。
# 查看当前工作目录
pwd
# 切换目录
cd /path/to/directory # 绝对路径
cd ../parent # 相对路径
cd ~ # 返回家目录
cd - # 返回上一个目录
# 列出目录内容
ls # 简单列出
ls -l # 详细列表(长格式)
ls -a # 显示隐藏文件
ls -lh # 人类可读的文件大小
ls -t # 按修改时间排序
# 创建文件
touch filename.txt
# 创建目录
mkdir dirname
mkdir -p parent/child # 创建多级目录
# 复制文件/目录
cp file1 file2
cp -r dir1 dir2 # 递归复制目录
# 移动/重命名
mv oldname newname
mv file /target/path/
# 删除文件/目录
rm file
rm -r dir # 递归删除目录
rm -rf dir # 强制递归删除(慎用!)
# 查看文件内容
cat file # 显示全部内容
less file # 分页查看(可上下翻页)
head -n 5 file # 查看前5行
tail -n 5 file # 查看后5行
tail -f logfile # 实时追踪日志
# 查看系统信息
uname -a # 显示所有系统信息
cat /etc/os-release # 查看发行版信息
# 查看资源使用情况
top # 动态查看进程(类似任务管理器)
htop # 增强版top(需安装)
free -h # 内存使用情况
df -h # 磁盘空间
du -sh * # 当前目录各文件/目录大小
# 查看网络信息
ifconfig # 网络接口信息(需安装net-tools)
ip addr # 新版替代命令
netstat -tulnp # 查看端口监听情况
ss -tulnp # 新版替代命令
# 查看进程
ps aux # 查看所有进程
ps -ef | grep nginx # 查找特定进程
# 进程控制
kill -9 PID # 强制终止进程
killall process_name # 终止同名所有进程
pkill -f pattern # 按模式终止进程
# 后台任务管理
command & # 后台运行
jobs # 查看后台任务
fg %1 # 将任务1调到前台
bg %1 # 继续后台运行任务1
nohup command & # 退出终端仍保持运行
grep "pattern" file # 基本搜索
grep -i "pattern" file # 忽略大小写
grep -r "pattern" /path # 递归搜索目录
grep -v "pattern" file # 反向匹配(不包含)
grep -E "regex" file # 扩展正则表达式
egrep "regex" file # 同上
sed 's/old/new/' file # 替换首次匹配
sed 's/old/new/g' file # 全局替换
sed -i 's/old/new/g' file # 直接修改文件
sed '/pattern/d' file # 删除匹配行
sed -n '5,10p' file # 打印5-10行
awk '{print $1}' file # 打印每行第一个字段
awk -F: '{print $1}' /etc/passwd # 指定冒号为分隔符
awk '$3 > 100 {print $0}' file # 条件筛选
awk '{sum+=$1} END{print sum}' # 求和计算
Linux采用经典的"用户-组-其他"三元权限模型:
-rwxr-xr-- 1 user group 4096 Jan 1 10:00 file
↑↑↑↑↑↑↑↑↑
││││││││└─ 其他用户的权限(r--)
││││└─└─└─ 所属组的权限(r-x)
│└─└─└─ 所属用户的权限(rwx)
└─ 文件类型(-普通文件,d目录,l链接等)
权限表示:
r (read):读取权限(4)
w (write):写入权限(2)
x (execute):执行权限(1)
# 数字模式
chmod 755 file # rwxr-xr-x
chmod -R 644 dir # 递归修改目录
# 符号模式
chmod u+x file # 给所有者添加执行权限
chmod g-w file # 移除组的写权限
chmod o=r file # 设置其他用户只读
chmod a+x script.sh # 给所有人添加执行权限
chown user:group file # 修改所有者和组
chown -R user:group dir # 递归修改
chown :group file # 只修改组
# SUID (4)
chmod u+s file # 执行时以所有者身份运行
# SGID (2)
chmod g+s dir # 目录下新建文件继承组
# Sticky Bit (1)
chmod +t /tmp # 只有所有者能删除自己的文件
# 数字表示法
chmod 4755 file # SUID + rwxr-xr-x
当基础权限模型不能满足需求时,可以使用ACL(Access Control List):
# 查看ACL
getfacl file
# 设置ACL
setfacl -m u:username:rwx file # 给特定用户权限
setfacl -m g:groupname:r-x dir # 给特定组权限
setfacl -x u:username file # 删除ACL条目
# 默认ACL(对新创建文件有效)
setfacl -d -m u:username:rwx dir
Nginx配置文件通常位于/etc/nginx/nginx.conf
,主要包含以下结构:
main # 全局配置
├── events # 事件模块配置
└── http # HTTP模块配置
├── server # 虚拟主机配置
│ ├── location # 请求路由配置
│ └── ...
└── ...
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /images/ {
expires 30d;
access_log off;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 超时设置
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
}
}
upstream backend {
least_conn; # 最少连接算法
server 10.0.0.1:8080 weight=3;
server 10.0.0.2:8080;
server 10.0.0.3:8080 backup; # 备用服务器
}
server {
location / {
proxy_pass http://backend;
}
}
server {
listen 443 ssl http2;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# SSL优化配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256...';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# HSTS安全头
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
# 强制HTTPS跳转
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
# 全局配置段
worker_processes auto; # 自动设置工作进程数
worker_rlimit_nofile 100000; # 文件描述符限制
events {
worker_connections 4096; # 每个工作进程连接数
multi_accept on; # 同时接受多个连接
use epoll; # Linux高性能事件模型
}
http {
# 缓冲和超时设置
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 4k;
sendfile on; # 零拷贝技术
tcp_nopush on; # 优化数据包发送
tcp_nodelay on; # 禁用Nagle算法
keepalive_timeout 30; # 保持连接超时
keepalive_requests 100; # 每个连接最大请求数
# Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;
gzip_min_length 1024;
gzip_comp_level 6;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time';
access_log /var/log/nginx/access.log main buffer=32k flush=5m;
}
error_log /var/log/nginx/error.log warn; # 记录警告及以上级别
使用logrotate管理日志(/etc/logrotate.d/nginx
):
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
# 查找大文件
du -ahx / | sort -rh | head -10
# 查找大日志文件
find /var/log -type f -size +100M -exec ls -lh {} \;
# 清理旧日志
journalctl --vacuum-size=200M # systemd日志
logrotate -f /etc/logrotate.conf
# 查看内存使用
free -h
# 查找内存消耗大的进程
ps aux --sort=-%mem | head
# 清理缓存
echo 3 > /proc/sys/vm/drop_caches
# 测试配置语法
nginx -t
# 详细错误日志
error_log /var/log/nginx/error.log debug;
# 调试特定IP
set_real_ip_from 192.168.1.100;
real_ip_header X-Forwarded-For;
# 查看活跃连接
ss -antp | grep nginx
# 监控请求处理
ngxtop # 需要安装Python工具
goaccess /var/log/nginx/access.log --log-format=COMBINED
通过本文的系统学习,您已经掌握了Linux基础命令、权限管理机制以及Nginx服务器的核心配置方法。这些技能是现代IT基础设施管理的基石,无论是开发、运维还是DevOps工作都离不开这些知识的支持。
进阶学习建议:
深入理解Linux文件系统层次结构标准(FHS)
学习Shell脚本自动化常见任务
掌握Nginx与Lua结合的OpenResty高级用法
了解Linux容器化技术(Docker/Kubernetes)
希望本文能成为您Linux和Nginx学习路上的得力助手。如果您在实践中遇到任何问题,欢迎在评论区留言讨论,我会尽力解答!