首页 文章 分类 关于

2026 年 Linux 服务器运维实战:systemd 与 nginx 进阶

从 systemd 服务单元、nginx 反向代理、SSL 证书自动化等角度,分享生产环境运维经验。

作者头像
杨一一

这个人很懒,什么都没留下

前言

Linux 服务器运维是后端工程师的基本功。本文从 systemd、nginx、SSL 三个角度分享实战经验。

一、systemd 服务单元

推荐配置:

[Service]
Type=simple
User=apps
WorkingDirectory=/home/apps/myapp
EnvironmentFile=/home/apps/myapp/.env
ExecStart=/home/apps/myapp/venv/bin/uvicorn app.main:app --host 127.0.0.1 --port 8000 --workers 2
Restart=always
RestartSec=5

关键点:Restart=always 确保服务异常时自动重启,RestartSec=5 避免疯狂重启。

二、nginx 反向代理

SSL 终结 + 静态资源直服 + 反向代理的标准配置:

server {
    listen 443 ssl http2;
    server_name www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location /static/ {
        alias /home/apps/myapp/static/;
        expires 30d;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

三、SSL 证书自动化

使用 certbot + Let's Encrypt 自动续签:

certbot --nginx -d www.example.com -d example.com
crontab: 0 3 * * * certbot renew --quiet

四、HTTPS 强制跳转

HTTP 80 端口 301 跳转到 HTTPS:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

五、日志轮转

使用 logrotate + nginx reload 避免日志文件无限增长。

总结

稳定的生产环境离不开精细的运维配置。systemd + nginx + certbot 是黄金组合。

1

评论 (0)

暂无评论,快来发表第一条评论吧