binary-husky / binary-husky/gpt_academic

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

Open
#1,967 4 comments 4 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
71.4k
Forks
8.3k
PR merge metrics
No merged PRs in 30d

Description

### Class | 类型

None

### Feature Request | 功能请求

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

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

## 1. 部署项目

首先,按照[项目的说明文档](https://github.com/binary-husky/gpt_academic?tab=readme-ov-file#installation)部署服务。假设我们将服务部署在服务器的7777端口上。

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

## 2. 安装Nginx

```bash
sudo apt update
sudo apt install nginx
```

## 3. 申请域名

1. 申请域名
2. 将域名托管到Cloudflare
3. 解析域名到服务器IP
> 注意:此时先不要开启Proxy,保持小云朵为灰色状态。

## 4. 申请SSL证书

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

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

## 5. 修改Nginx配置

编辑Nginx配置文件:

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

参考配置如下:

```nginx
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

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

## 7. 测试

1. 测试 `yourdomain.com` 是否可以正常跳转
2. 如果成功,回到Cloudflare点亮代理的小云朵,SSL/TLS encryption mode 改成 Full (strict)

---

**重要提示**:
- Nginx默认的上传文件大小限制为1M,可能不足以上传一篇论文。请根据需求调整 `client_max_body_size`。
- 添加WebSocket专用头非常重要,许多反向代理失败的问题都是由于缺少这个配置造成的。
- 只使用上述方法要先 ”cd gpt_academic“ 再运行main.py,不然网页上的功能是无法使用的(感谢 @GuangmingChan 指出,或者你也可以参考下面他给出的配置)。

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the project's installation instructions linked from the issue and review how the service is run from main.py. Decide where this Nginx and HTTPS deployment guide belongs, then verify the documented commands, configuration path (/etc/nginx/sites-available/default), and nginx -t validation step. Done means the deployment procedure is clear and complete for the described setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
nginx, python
Domain
cloud, devops, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.