域名绑定服务器小白教程

域名绑定与 Docker 容器部署指南

1. 获取云服务器公网 IP

登录云服务提供商控制台记录服务器公网 IP(例:123.456.78.90)

2. 配置域名 DNS 解析

登录域名注册商控制台添加 A 记录:

主机记录:@类型:A值:服务器公网 IPTTL:默认

3. 安装 Nginx

sudo apt update

sudo apt install nginx -y

sudo systemctl start nginx

sudo systemctl enable nginx

4. 配置 Nginx 反向代理

创建配置文件:

sudo nano /etc/nginx/sites-available/【你的域名】

配置内容:

server {

listen 80;

server_name 【你的域名】 www.【你的域名】;

location / {

proxy_pass http://localhost:8080;

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;

}

}

启用配置:

sudo ln -s /etc/nginx/sites-available/【你的域名】 /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

5. 配置防火墙

sudo ufw status

sudo ufw allow 'Nginx Full'

sudo ufw status

6. 测试域名访问

在浏览器访问:http://【你的域名】

7. 配置 HTTPS(可选)

sudo apt install certbot python3-certbot-nginx -y

sudo certbot --nginx -d 【你的域名】 -d www.【你的域名】

sudo certbot renew --dry-run

关键点: 执行sudo certbot renew --dry-run出错误,可执行sudo less /var/log/letsencrypt/letsencrypt.log查看日志。 出现邮箱错误,检查是否是符合下列标准:user.example.com(去掉@),然后使用下面命令修正:

sudo certbot --nginx -d 【你的域名】 -d www.【你的域名】 --email your-email@user.example.com

参考资源

Nginx 官方文档Let’s Encrypt 官方文档

注:所有【你的域名】部分需替换为实际域名