惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
U
Unit 42
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
C
Check Point Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
Vercel News
Vercel News
腾讯CDC
GbyAI
GbyAI
有赞技术团队
有赞技术团队
S
SegmentFault 最新的问题
H
Help Net Security
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6
在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取...
About the Author StudyingLover · 2024-04-10 · via StudyingLover's Blog

docker

fastapi

nginx

cloudflare

宝塔

真实ip

fastapi 反向代理

反向代理

docker 172.17.0.1

发布于

在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取请求的真实ip

背景是这样的,我使用docker部署了一个fastapi部署了一个应用,使用request.client.host获取请求的来源ip,但是获取到的都是172.17.0.1这显然是不是正常的,是docker网络下的ip,所以我们需要在nginx进行设置转发真实ip

首先点击宝塔应用商店,找到nginx,点击右边的设置,在配置修改中,找到http模块中的include luawaf.conf;,在下面添加如下两段代码,重载nginx

set_real_ip_from 0.0.0.0/0;
real_ip_header X-Forwarded-For;

同时在/www/server/panel/vhost/nginx/proxy/你的网站 这里路径下面能找到一个配置文件,在location /加上

location / {
    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_pass http://your_fastapi_app;
}

在fastapi中,我们将获取ip的代码改成下面这样

ip_address = request.headers.get("X-Real-IP") if request.headers.get("X-Real-IP") else (request.headers.get("X-Forwarded-For") if request.headers.get("X-Forwarded-For") else request.client.host)