debian云服务器上搭建个人网站所需要的nginx配置知识

admin | 2026-04-04 10:08 | 阅读 32 次

下面给你一个面向 Debian 云服务器的完整 Nginx 建站配置指南,涵盖 静态站点 / PHP / ASP.NET Core / SSL / HTTP→HTTPS 重定向,适合个人网站或小型生产环境。


一、基础环境准备(Debian)

1. 系统更新

sudo apt update && sudo apt upgrade -y

2. 安装 Nginx

sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

验证:

systemctl status nginx

浏览器访问 http://服务器IP 应看到 Nginx 欢迎页。


二、Nginx 配置基础结构

1. 目录说明

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

2. 基本站点模板

创建站点配置:

sudo nano /etc/nginx/sites-available/mysite.conf
server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/mysite;
    index index.html index.htm;

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

启用站点:

sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

三、PHP 配置(PHP-FPM)

1. 安装 PHP

sudo apt install php-fpm php-mysql -y

查看版本:

php -v

2. 配置 Nginx 支持 PHP

编辑站点配置:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据实际版本调整
}

测试 PHP:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/mysite/index.php

访问 http://example.com/index.php


四、ASP.NET Core 配置(反向代理)

1. 安装 .NET Runtime

wget https://dot.net/v1/dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --runtime aspnetcore --version 8.0.0

添加到 PATH:

export DOTNET_ROOT=$HOME/.dotnet
export PATH=$PATH:$DOTNET_ROOT

2. 发布 ASP.NET Core 应用

dotnet publish -c Release -o /var/www/dotnetapp

3. 使用 Systemd 托管

sudo nano /etc/systemd/system/dotnetapp.service
[Unit]
Description=ASP.NET Core App
After=network.target

[Service]
WorkingDirectory=/var/www/dotnetapp
ExecStart=/usr/bin/dotnet /var/www/dotnetapp/MyApp.dll
Restart=always
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

启动:

sudo systemctl enable dotnetapp
sudo systemctl start dotnetapp

4. Nginx 反向代理配置

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

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

五、SSL 配置(Let's Encrypt)

1. 安装 Certbot

sudo apt install certbot python3-certbot-nginx -y

2. 申请证书

sudo certbot --nginx -d example.com -d www.example.com

自动续期测试:

sudo certbot renew --dry-run

六、HTTP → HTTPS 强制重定向

Certbot 通常会自动添加,也可手动配置:

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

HTTPS server 示例:

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    root /var/www/mysite;
    index index.html index.php;

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

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

七、安全与优化建议

1. 隐藏 Nginx 版本

server_tokens off;

2. 限制文件上传

client_max_body_size 20m;

3. 防火墙

sudo ufw allow 'Nginx Full'
sudo ufw enable

八、常见问题排查

| 问题 | 解决 | |----|----| | 403 Forbidden | 检查目录权限 chown -R www-data:www-data /var/www | | PHP 不解析 | 确认 fastcgi_pass sock 路径 | | HTTPS 无法访问 | 检查 443 端口是否放行 | | .NET 502 | 确认应用监听 http://localhost:5000 |


九、推荐组合方案

| 网站类型 | 推荐 | |--------|------| | 静态博客 | Nginx | | WordPress | Nginx + PHP | | API / Web | Nginx + ASP.NET Core | | 企业官网 | Nginx + SSL |


如果你愿意,我可以:

  • ✅ 帮你生成完整可用的 nginx.conf
  • ✅ 针对 WordPress / 博客 / API 定制配置
  • ✅ 给你一套 安全加固版 Nginx 模板

只要告诉我你的网站类型和域名即可。

评论 (0)

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