网站访问不是一个东西,而是一条链路。
浏览器访问域名时,会先通过 DNS 找到服务器 IP,再通过 80 或 443 端口进入 Nginx,Nginx 再把请求交给网站文件或后端服务。链路上任何一环错了,表现都可能是“打不开”。
Skills · Website Infrastructure
用 Nginx、Certbot、Cloudflare、DNS 与端口完成可维护的发布链路。
浏览器访问域名时,会先通过 DNS 找到服务器 IP,再通过 80 或 443 端口进入 Nginx,Nginx 再把请求交给网站文件或后端服务。链路上任何一环错了,表现都可能是“打不开”。
ssh root@服务器IP。/var/www/站点名。
如果终端 SSH 能连,通常说明服务器和 SSH 服务没坏。VS Code 一直要密码,多半是 Remote-SSH 用错用户、用错 Host 配置、远程 .vscode-server 残留,或本地 SSH key 没被读取。
| 阶段 | 要完成的事 | 通过标准 |
|---|---|---|
| 1. SSH | 终端能用 root 或指定用户登录服务器。 | ssh root@服务器IP 后进入 shell。 |
| 2. VS Code | Remote-SSH 使用同一个 Host、User、Port。 | 能打开服务器目录,不再循环输入密码。 |
| 3. Nginx | 安装 Nginx,确认服务运行。 | http://服务器IP 能看到默认页。 |
| 4. 网站文件 | 上传 HTML、CSS、图片到 /var/www/my-site。 |
Nginx root 指向该目录后能打开首页。 |
| 5. 域名 DNS | Cloudflare 添加 A/CNAME,首次申请证书前先 DNS only。 | http://域名 能到你的服务器。 |
| 6. Certbot | 用 certbot --nginx 申请证书并写入 HTTPS 配置。 |
https://域名 返回 200 或 301。 |
| 7. Cloudflare HTTPS | 代理切回橙色云,SSL/TLS 选择 Full strict。 | 浏览器访问域名显示 HTTPS 正常。 |
| 8. 端口收口 | 只保留 SSH、80、443 和必要的后台端口。 | ss -lntp 和安全组结果一致。 |
ssh root@服务器IP
如果你在终端看到 root@主机名:~#,说明 SSH 链路是通的。VS Code 应该使用同样的用户和 IP,而不是默认用本地 Mac 用户名去连。
Host my-site
HostName 服务器IP
User root
Port 22
Remote-SSH 里选择 my-site,不要只输入 IP。只输入 IP 时,VS Code 可能默认使用你本地电脑的用户名。
apt update
apt upgrade -y
apt install -y curl wget vim unzip git ufw nginx
如果服务器刚买来,先不要急着改 SSH 端口。确认 22 端口、root 登录、VS Code 连接都稳定以后,再做进一步加固。
systemctl status nginx
nginx -t
systemctl reload nginx
浏览器打开 http://服务器IP 能看到 Nginx 默认页,说明 80 端口、Nginx 服务和最基础的访问链路已经通了。
mkdir -p /var/www/my-site
chown -R root:root /var/www/my-site
# 放入 index.html、style.css、img 等文件
ls -la /var/www/my-site
纯 HTML/CSS/JS 网站不需要 Node 常驻进程。Nginx 直接读取目录里的文件并返回给浏览器。
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/my-site;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/my-site
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
使用 Certbot 的 HTTP 验证时,Let's Encrypt 需要直接访问你的服务器 80 端口。Cloudflare 橙色云代理有时会让排错变复杂,所以第一次申请证书前,建议先把 A 记录临时切成 DNS only。
apt update
apt install -y certbot python3-certbot-nginx
Debian/Ubuntu 上推荐安装 python3-certbot-nginx,这样 Certbot 可以读取 Nginx 配置并自动写入 443 server block。
certbot --nginx -d example.com -d www.example.com
执行过程中按提示填写邮箱、同意条款,并选择是否把 HTTP 自动重定向到 HTTPS。推荐选择自动跳转,成功后再执行 nginx -t 和 systemctl reload nginx。
nginx -t
systemctl reload nginx
curl -I https://example.com
certbot renew --dry-run
systemctl list-timers | grep certbot
Certbot 通常会安装 systemd timer 自动续期。只要 dry run 成功,后续一般不需要手动续签。
https://example.com 可以直接访问。server {
server_name example.com www.example.com;
root /var/www/my-site;
index index.html;
location / {
try_files $uri $uri/ =404;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
不建议第一次就手写证书路径。让 certbot --nginx 自动改配置更稳,之后再读它生成的文件,理解 80 跳转和 443 证书路径。
| 记录 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| A | @ |
服务器 IPv4 | Proxied,橙色云 |
| CNAME | www |
example.com |
Proxied,橙色云 |
域名接入 Cloudflare 后,需要到域名注册商那里把 Nameserver 改成 Cloudflare 提供的两条 NS。DNS 生效可能需要等待。
新手容易选错 SSL/TLS 模式。推荐最终使用 Full strict,前提是服务器 Nginx 上已经有有效证书。临时测试可以用 Flexible 或 Full,但不要长期混用导致跳转循环。
| 端口 | 用途 | 是否需要公网开放 |
|---|---|---|
22 |
SSH / VS Code Remote-SSH | 需要,但可以限制来源 IP 或改端口 |
80 |
HTTP / Let's Encrypt 验证 / 自动跳转 HTTPS | 网站通常需要 |
443 |
HTTPS | 网站通常需要 |
| 自定义面板端口 | 例如 3x-ui、后台管理面板 | 不建议直接暴露,优先 SSH 隧道或限制 IP |
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status verbose
云服务器控制台的安全组也要同步开放。系统防火墙放行但安全组没放行,外部仍然访问不到。
ss -lntp
curl -I http://127.0.0.1
curl -I http://你的域名
如果本机 127.0.0.1 能访问,但公网 IP 不能访问,通常查防火墙、安全组或 Nginx listen 配置。
my-site。/var/www/my-site 文件夹。index.html、style.css 和图片。本地用户名@服务器IP,而不是 root@服务器IP。Remote-SSH: Kill VS Code Server on Host。~/.vscode-server 后重连。~/.ssh/config 固定 Host、User 和 Port。systemctl status nginx
nginx -t
ss -lntp | grep -E ':80|:443'
curl -I http://127.0.0.1
curl -I http://服务器IP
curl -I http://你的域名
tail -n 80 /var/log/nginx/error.log
不要一上来格式化服务器。先判断问题发生在 SSH、Nginx、本机防火墙、云安全组、DNS、Cloudflare 代理还是证书链路。