网站Logo 北之屿

Nginx 配置与 SSL 证书部署指南

beiyu
17
2025-09-12

目录结构说明

  • 项目文件目录: /var/www/

  • Nginx 配置文件目录: /etc/nginx/sites-available/

  • 启用站点目录: /etc/nginx/sites-enabled/

1. 静态页面配置

1.1 创建项目目录

sudo mkdir -p /var/www/your-domain.com
sudo chown -R $USER:$USER /var/www/your-domain.com
sudo chmod -R 755 /var/www/your-domain.com

1.2 创建 Nginx 配置文件

/etc/nginx/sites-available/ 下创建配置文件:

sudo nano /etc/nginx/sites-available/your-domain.com

1.3 静态页面配置示例

server {
    listen 80;
    listen [::]:80;
    server_name zj.beiyupro.top;

    # 静态文件根目录
    root /var/www/zj.beiyupro.top;
    index index.html index.htm;

    # 主要路由配置
    location / {
        try_files $uri $uri/ /index.html;
    }

    # 静态资源缓存配置
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # 安全头配置
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
}

2. 反向代理配置

2.1 Node.js 应用反向代理示例

server {
    listen 80;
    listen [::]:80;
    server_name beiyupro.top;

    location / {
        proxy_pass http://127.0.0.1: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_set_header X-Forwarded-Proto $scheme;
        
        # 文件上传大小限制
        client_max_body_size 25m;
        
        # 超时配置
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # WebSocket 支持(如需要)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # API 路由示例
    location /api/ {
        proxy_pass http://127.0.0.1:3001;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

3. 启用配置并测试

3.1 启用站点

# 创建符号链接到 sites-enabled
sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/

# 测试配置文件语法
sudo nginx -t

# 重新加载配置
sudo systemctl reload nginx

3.2 常用 Nginx 命令

# 测试配置
sudo nginx -t

# 重新加载 systemd 守护进程(如果修改了 service 文件才需要)
sudo systemctl daemon-reload

# 重新加载配置
sudo systemctl reload nginx

# 重启 Nginx
sudo systemctl restart nginx

# 查看 Nginx 状态
sudo systemctl status nginx

# 查看错误日志
sudo tail -f /var/log/nginx/error.log

4. SSL 证书配置 (Certbot)

4.1 安装 Certbot

# Ubuntu/Debian
sudo apt update
sudo apt install certbot python3-certbot-nginx

# CentOS/RHEL
sudo yum install epel-release
sudo yum install certbot python3-certbot-nginx

4.2 首次获取 SSL 证书

# 手动指定域名单个(智能命令)
sudo certbot --nginx -d your-domain.com  

# 手动指定域名多个
sudo certbot --nginx -d example.com -d www.example.com

# 仅获取证书,不自动配置
sudo certbot certonly --nginx -d your-domain.com

# 重新安装现有证书到 Nginx 配置中(更新 Nginx 配置文件,添加 SSL 相关配置)
sudo certbot --nginx -d example.com --reinstall

4.3 证书续期

# 测试续期
sudo certbot renew --dry-run

# 手动续期
sudo certbot renew

# 自动续期(添加到 crontab)
sudo crontab -e
# 添加以下行(每天凌晨2点检查)
0 2 * * * /usr/bin/certbot renew --quiet

4.4 查看证书信息

# 查看所有证书
sudo certbot certificates

# 查看特定域名证书
sudo certbot certificates -d your-domain.com

5. 配置密码 (HTTP Basic Auth)

5.1 安装生成密码的工具

sudo apt update
sudo apt install -y apache2-utils

5.2 创建用户名/密码文件

第一次创建用 -c(只用一次,后面加用户别再带 -c,不然会覆盖):

sudo htpasswd -c /etc/nginx/.htpasswd yourname

它会提示你输入两遍密码。

以后再加一个用户:

sudo htpasswd /etc/nginx/.htpasswd anothername

5.3 在 Nginx 里启用认证

把下面两行加到你的 server { ... }

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

最终大概像这样

server {
    server_name mon.vioiv.cc;

    root /var/www/mon.vioiv.cc;
    index index.html index.htm;

    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    location /api/ {
        proxy_pass http://127.0.0.1:3002;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }
    ...
}

5.4 检查并重载

sudo nginx -t
sudo systemctl reload nginx

5.5 只保护网页,不影响 /api

如果你不想让接口也弹窗(比如给 App 调用),可以把认证只放在 location / { ... } 里,而不是 server 级别:

location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    try_files $uri $uri/ /index.html;
}

动物装饰