Nginx+React+FastAPI+Docker部署:后端安全与外网访问问题咨询
局域网部署个人主页后外网访问的配置疑问
我折腾了很久(多次咨询Copilot),已经在局域网部署了个人主页。前端采用Firebase Google SSO认证,但后端完全开放——比如访问IP:80/api/emails就能查看邮件表数据,原本以为Nginx能解决这个问题,现在对它的作用存疑。我想把服务部署到连接路由器的树莓派上实现外网访问,用户仅为15位完全可信的朋友,请问还需要哪些配置?
相关配置文件
React/前端 Dockerfile
# 构建阶段 FROM node:16.13 as build WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build
React api.js
import axios from 'axios'; const api = axios.create({ baseURL: 'http://192.168.2.32/api', // 这是我的本地IP }); export default api;
FastAPI/后端 Dockerfile
# 使用官方Python运行时作为基础镜像 FROM python:3.10 # 设置环境变量 ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # 设置容器内工作目录为/app WORKDIR /app # 将当前目录内容复制到容器的/app目录下 COPY . /app # 安装requirements.txt中指定的所需包 RUN pip install --no-cache-dir -r requirements.txt # 开放容器8000端口供外部访问 EXPOSE 8000 # 定义使用uvicorn运行应用的命令 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Nginx Dockerfile
FROM nginx:stable-alpine COPY nginx.conf /etc/nginx/conf.d/default.conf CMD ["nginx", "-g", "daemon off;"]
Nginx nginx.conf
server { listen 80; location / { proxy_pass http://frontend:3000; } location /api { proxy_pass http://backend:8000; } }
Docker-compose
version: '3.8' services: backend: build: ./backend volumes: - './backend:/app' - './backend/database:/app/database' ports: - '8000:8000' frontend: build: context: ./frontend dockerfile: Dockerfile volumes: - './frontend:/app' command: npm start ports: - '3000:3000' nginx: build: context: ./nginx dockerfile: Dockerfile volumes: - './frontend/build:/usr/share/nginx/html' ports: - '80:80'
内容的提问来源于stack exchange,提问作者Maru
相关产品推荐
相关产品推荐

