Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: 分享一个使用在海外服务器用ngnix反向代理https访问的方法 #1967

Open
Cybw opened this issue Sep 14, 2024 · 0 comments

Comments

@Cybw
Copy link

Cybw commented Sep 14, 2024

Class | 类型

None

Feature Request | 功能请求

在海外公网服务器上部署服务并配置HTTPS

将服务部署在海外的公网服务器上有许多优势,包括减少API被封号的风险和实现随时随地的访问。然而,由于项目基于HTTP,尽管有访问密码功能,但非加密传输仍然存在安全隐患。为解决这个问题,我们可以使用Nginx反向代理,将服务器的443端口反向代理到80端口,实现公网的加密传输。

1. 部署项目

首先,按照项目的说明文档部署服务。假设我们将服务部署在服务器的7777端口上。

确保可以通过"服务器ip:7777"成功访问后再进行下一步。

2. 安装Nginx

sudo apt update
sudo apt install nginx

3. 申请域名

  1. 申请域名
  2. 将域名托管到Cloudflare
  3. 解析域名到服务器IP

    注意:此时先不要开启Proxy,保持小云朵为灰色状态。

4. 申请SSL证书

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

注意:申请证书时需要使用80端口,请在防火墙里打开80端口,并暂时关闭其他使用80端口的服务。

5. 修改Nginx配置

编辑Nginx配置文件:

sudo nano /etc/nginx/sites-available/default

参考配置如下:

server {
    listen 443 ssl;
    server_name yourdomain.com www.yourdomain.com; #记得改


    # SSL配置,记得改
    ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;


    # 增加上传文件大小限制
    client_max_body_size 100M;  # 根据需求调整大小


    location / {
        proxy_pass http://localhost:7777;  # 你的HTTP服务端口


        # WebSocket专用头
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";


        # 保持真实的客户端IP和主机信息
        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;


        # 防止超时问题
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
    }
}

6. 重新运行Nginx

sudo nginx -t  # 检查配置是否正确
sudo systemctl reload nginx

7. 测试

  1. 测试 yourdomain.com 是否可以正常跳转
  2. 如果成功,回到Cloudflare点亮代理的小云朵

重要提示

  • Nginx默认的上传文件大小限制为1M,可能不足以上传一篇论文。请根据需求调整 client_max_body_size
  • 添加WebSocket专用头非常重要,许多反向代理失败的问题都是由于缺少这个配置造成的。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant