diff --git a/sankey.conf b/sankey.conf new file mode 100644 index 0000000..3fae8aa --- /dev/null +++ b/sankey.conf @@ -0,0 +1,34 @@ +# HTTPS server +server { + listen 443 ssl; + server_name sankey.biboer.cn; + + ssl_certificate /etc/letsencrypt/live/sankey.biboer.cn/fullchain.pem; # managed by Certbot + ssl_certificate_key /etc/letsencrypt/live/sankey.biboer.cn/privkey.pem; # managed by Certbot + include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot + + root /home/gavin/sankey/dist; + index index.html; + + location / { + try_files $uri $uri/ /index.html; + } + + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} + +# HTTP to HTTPS redirect +server { + listen 80; + server_name sankey.biboer.cn; + + if ($host = sankey.biboer.cn) { + return 301 https://$host$request_uri; + } # managed by Certbot + + return 404; +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index 4d1408a..9dc2635 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,12 +1,20 @@ import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; -import { readFileSync } from 'node:fs'; +import { readFileSync, existsSync } from 'node:fs'; import { homedir } from 'node:os'; import { join } from 'node:path'; const certDir = join(homedir(), 'mac.biboer.cn_ecc'); -const httpsCert = readFileSync(join(certDir, 'fullchain.cer')); -const httpsKey = readFileSync(join(certDir, 'mac.biboer.cn.key')); +const certPath = join(certDir, 'fullchain.cer'); +const keyPath = join(certDir, 'mac.biboer.cn.key'); + +// 只在证书存在时启用 HTTPS(开发环境) +const httpsConfig = existsSync(certPath) && existsSync(keyPath) + ? { + cert: readFileSync(certPath), + key: readFileSync(keyPath) + } + : undefined; export default defineConfig({ plugins: [vue()], @@ -15,9 +23,6 @@ export default defineConfig({ port: 5173, strictPort: true, allowedHosts: ['mac.biboer.cn'], - https: { - cert: httpsCert, - key: httpsKey - } + https: httpsConfig } });