温馨提示×

怎样调debian nginx

小樊
50
2026-09-06 15:54:58
栏目: 智能运维

下面按常见场景给你一份「Debian 上调 Nginx」的实用指南,从安装到常用配置和排错。


一、安装 Nginx(Debian)

sudo apt update
sudo apt install nginx -y

安装后常用命令:

sudo systemctl status nginx      # 查看状态
sudo systemctl start nginx       # 启动
sudo systemctl stop nginx        # 停止
sudo systemctl restart nginx     # 重启
sudo systemctl reload nginx      # 热加载配置(不中断)

开机自启:

sudo systemctl enable nginx

二、Nginx 目录结构(Debian)

/etc/nginx/
├── nginx.conf            # 主配置
├── sites-available/      # 可用站点配置
├── sites-enabled/        # 启用的站点(软链接)
├── conf.d/               # 额外配置
└── modules-enabled/      # 模块

Web 根目录默认:

/var/www/html

三、配置一个站点(推荐方式)

1️⃣ 新建站点配置

sudo nano /etc/nginx/sites-available/example.com

示例(PHP + 静态):

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }

    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;
}

2️⃣ 启用站点

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

3️⃣ 删除默认站点(可选)

sudo rm /etc/nginx/sites-enabled/default

4️⃣ 测试并重载

sudo nginx -t
sudo systemctl reload nginx

四、防火墙(很重要)

如果开了 ufw

sudo ufw allow 'Nginx Full'

或只开 80:

sudo ufw allow 80

五、HTTPS(Let’s Encrypt)

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com

自动续签测试:

sudo certbot renew --dry-run

六、常见调优(性能 & 安全)

worker 进程(nginx.conf)

worker_processes auto;
worker_connections 1024;

开启 Gzip

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;

禁止目录列表

autoindex off;

七、常见错误排查

sudo nginx -t
journalctl -u nginx
tail -f /var/log/nginx/error.log

常见问题:

  • 403 → 文件权限 / 目录不存在
  • 404 → root 或 try_files 配置错
  • 502 → PHP-FPM 没启动或 sock 路径错

八、你具体想调什么?

我可以更精准帮你,比如:

  • ✅ 反向代理(Node / Docker / 后端)
  • ✅ 负载均衡
  • ✅ 限流 / 防刷
  • ✅ 静态资源缓存
  • ✅ WordPress / Laravel / Vue

告诉我你的使用场景就行。

0