Bubble's blog
Home
About
Flask 服务器上部署了 Nginx 后,增加 HTTPS 支持
Category:
技术
Date:
2024-10-12T08:35:41Z
### 1. 安装 Nginx 首先,在 Ubuntu 服务器上安装 Nginx: ```bash sudo apt update sudo apt install nginx ``` ### 2. 启动 Nginx 安装完成后,启动 Nginx 并设置为开机自启: ```bash sudo systemctl start nginx sudo systemctl enable nginx ``` ### 3. 获取 SSL 证书 可以使用 Let's Encrypt 免费获取 SSL 证书,推荐使用 Certbot。 #### 安装 Certbot ```bash sudo apt install certbot python3-certbot-nginx ``` #### 获取证书 运行以下命令获取证书(确保将 `your_domain.com` 替换为你的实际域名): ```bash sudo certbot --nginx -d your_domain.com -d www.your_domain.com ``` ### 4. 配置 Nginx 创建或修改 Nginx 的配置文件,通常在 `/etc/nginx/sites-available/` 目录下。 1. **创建新配置文件**: ```bash sudo nano /etc/nginx/sites-available/bluelog ``` 2. **添加以下内容**: ```nginx server { listen 80; server_name your_domain.com www.your_domain.com; # 强制 HTTP 到 HTTPS 重定向 return 301 https://$host$request_uri; } server { listen 443 ssl; server_name your_domain.com www.your_domain.com; ssl_certificate /etc/letsencrypt/live/your_domain.com/fullchain.pem; # Let's Encrypt 证书路径 ssl_certificate_key /etc/letsencrypt/live/your_domain.com/privkey.pem; # 私钥路径 location / { proxy_pass http://127.0.0.1:5000; # Flask 应用运行的地址 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; } } ``` 3. **启用配置**: 创建一个符号链接到 `sites-enabled` 目录: ```bash sudo ln -s /etc/nginx/sites-available/bluelog /etc/nginx/sites-enabled ``` ### 5. 测试 Nginx 配置 在重启 Nginx 之前,测试配置是否正确: ```bash sudo nginx -t ``` ### 6. 重启 Nginx 如果测试通过,重启 Nginx 以应用更改: ```bash sudo systemctl restart nginx ``` ### 7. 设置防火墙(如果需要) 如果你有启用防火墙,确保允许 HTTP 和 HTTPS 流量: ```bash sudo ufw allow 'Nginx Full' ``` ### 8. 访问你的应用 现在,你可以通过浏览器访问 `https://your_domain.com`(替换为你的域名)来访问 Bluelog 应用。 ### 9. 自动续期证书 Let's Encrypt 证书有效期为 90 天,可以通过以下命令设置自动续期: ```bash sudo certbot renew --dry-run ```
Share
Permalink
×
0 Comments
latest
No comments.
Name
Email
Site
Comment
Human check: 1 + 5 = ?
Links
CSDN
GitHub
WHOIS
吾爱破解
看雪论坛
Categories
Default
0
技术
24
时事
0
Change Theme
Perfect Blue
Black Swan
0 Comments latest
No comments.