The Smoldering Ashes of Time

時間的灰燼 - WHAT'S YOUR

部署个人博客服务器

发布于 # 瞎折腾

在 Claw Cloud 购入了一台 1C1G 服务器,记录一下我部署个人博客的过程:

我的需求

我的方案

过程中我遇到的问题

Astro & Nginx 部署

1. 网页无法显示,并出现报错信息

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/plain". Strict MIME type checking is enforced for module scripts per HTML spec.

原因分析:
解决方法:

在 nginx.conf 加载 mime.types

http {
    include       mime.types;
}

因为 mime.types 文件定义了文件扩展名到 MIME 类型的映射,比如:

types {
    application/javascript js;
    text/css               css;
    text/html              html;
}

2. 打开任何路径,始终显示 index.html 的内容

打开 example.com/about 依旧显示 example.com 的内容

解决方法:

添加下面的代码到 nginx.conf

http {
    server {
        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}
原因解释:

这段代码的作用是实现单页应用(SPA)路由回退,这是许多现代前端框架(例如Astro、React、Vue等)的常见需求。

详细解释:

适用场景:

通过这个配置:

为什么可以解决问题?

假如未配置 try_files

配置了 try_files 后:

申请 Https 证书并配置 Nginx

1. 无法将服务器本地目录的 let’s Encrypt 证书,映射到 Nginx 容器中

docker-compose up -d 启动 Nginx 容器,出现报错信息:

nginx | nginx: [emerg] cannot load certificate "/etc/nginx/certs/fullchain.pem": BIO_new_file() failed (SSL: error:80000002:system library::No such file or directory:calling fopen(/etc/nginx/certs/fullchain.pem, r) error:10000080:BIO routines::no such file)

此时我的 docker-compose.yaml 相关配置如下:

services:
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro # 挂载自定义的nginx配置文件
      - /etc/letsencrypt/live/example.com:/etc/nginx/certs #映射主机证书目录

nginx.conf 相关配置如下:

http {
    server {
        # HTTP 部分:将所有请求重定向到 HTTPS
        listen 80;
        server_name example.com www.example.com; # 替换为你的域名

        return 301 https://$host$request_uri;
    }

    server {
        # HTTPS 部分
        listen 443 ssl;
        server_name example.com www.example.com; # 替换为你的域名

        # 配置 HTTPS 证书路径(Let's Encrypt)
        ssl_certificate /etc/nginx/certs/fullchain.pem; # 替换为证书路径
        ssl_certificate_key /etc/nginx/certs/privkey.pem; # 替换为证书路径
    }
}
原因分析:

Let’s Encrypt 目录的符号链接:/etc/letsencrypt/live/example.com 目录通常包含指向实际证书文件的符号链接,而不是直接存放证书文件。因此,直接在 Docker 容器中挂载这个路径会导致问题,因为符号链接可能无法正确解析。

解决方法:

直接挂载实际证书文件,nginx.conf 配置不变,修改后的 docker-compose.yaml 相关配置如下:

services:
  nginx:
    image: nginx:latest
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro # 挂载自定义的nginx配置文件
      - /etc/letsencrypt/live/example.com/fullchain.pem:/etc/nginx/certs/fullchain.pem #映射主机证书目录
      - /etc/letsencrypt/live/example.com/privkey.pem:/etc/nginx/certs/privkey.pem #映射主机证书目录
其他可能由于粗心引发的问题
  1. Nginx 设置好了子域名,但是忘记在域名管理处添加对应的 DNS 记录

最终呈现

  1. 博客主页

  1. 服务器数据看板

  1. 图床服务