-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastAPI
More file actions
71 lines (60 loc) · 2.24 KB
/
FastAPI
File metadata and controls
71 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 80 Port 使用
#server {
# listen 80;
# server_name 你的Domain; # 替換為你的域名或 IP 地址
#
# # 提供靜態文件 (index.html)
# location / {
# root /var/www/html; # 靜態文件的根目錄
# index index.html;
# }
#
# # FastAPI 路由的反向代理,將所有 /api/ 路由轉發到 FastAPI
# location /api/ {
# proxy_pass http://127.0.0.1:8000; # 假設 FastAPI 運行在 localhost 的 8000 埠
# 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;
# }
#}
# HTTP (80) 端口的設置,將流量重定向到 HTTPS
server {
listen 80;
server_name 你的Domain; # 替換為你的域名或 IP 地址
# 將所有流量重定向到 HTTPS
return 301 https://$host$request_uri;
}
# HTTPS (443) 端口的設置
server {
listen 443 ssl;
server_name 你的Domain; # 替換為你的域名或 IP 地址
# SSL 設置
ssl_certificate /opt/SSL/certificate.crt; # 替換為你的證書路徑
ssl_certificate_key /opt/SSL/private.key; # 替換為你的密鑰路徑
# SSL 配置(可選,你可以根據需要添加)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers off;
# 提供靜態文件 (index.html)
location / {
root /var/www/html; # 靜態文件的根目錄
index index.html;
}
# FastAPI 路由的反向代理,將所有 /api/ 路由轉發到 FastAPI
location /api/ {
proxy_pass http://127.0.0.1:8000; # 假設 FastAPI 運行在 localhost 的 8000 埠
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;
# CORS 設置
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE";
add_header Access-Control-Allow-Headers "Content-Type, Authorization";
# 預檢請求 OPTIONS 直接返回成功
if ($request_method = OPTIONS) {
return 204;
}
}
}