【发布时间】:2022-08-04 16:42:29
【问题描述】:
我有我的配置的生产版本。但是我对服务器的一些请求可能需要超过 1、2、10、15 秒。这是随机的。 20 个请求中有 1 个是这样的。我有很好的服务器:8RAM,4CPU。我的代码中的问题。
如何将其设置为生产?
我的架构: 服务器 NGINX -> docker NGINX -> uvicorn -> FastAPI 应用程序
服务器 NGINX 配置:
server {
listen 80;
server_name blabla.com;
location / {
proxy_pass http://0.0.0.0:8040$request_uri;
proxy_set_header HOST $host;
}
Docker NGINX 配置:
user www-data;
pid /run/nginx.pid;
events {
# multi_accept on;
}
http {
# Basic settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 20480;
client_max_body_size 30m;
# access_log off;
#
include /etc/nginx/mime.types;
default_type application/octet-stream;
# GZIP
gzip on;
server {
listen 80;
server_name ${EXTERNAL_HOST};
access_log /data/logs/nginx.log;
error_log /data/logs/nginx.err warn;
root /;
location /api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://api:5000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection \"Upgrade\";
}
}
}
Dockerfile:
FROM python:3.10
WORKDIR .
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ARG PROTOCOL HOST
ENV SERVER \"${PROTOCOL}://${HOST}/api/\"
ENV CLIENT \"${PROTOCOL}://${HOST}/\"
码头工人撰写配置:
api:
image: blabla/api
build:
dockerfile: ../docker/api/Dockerfile
context: ../api
args:
- PROTOCOL=${PROTOCOL}
- HOST=${EXTERNAL_HOST}
restart: unless-stopped
env_file: .env
volumes:
- ../data/load:/data/load
- type: bind
source: ../data/logs/api.log
target: /app.log
deploy:
mode: replicated
replicas: 1
resources:
limits:
cpus: \"0.75\"
memory: 1500M
reservations:
cpus: \"0.25\"
memory: 500M
command: uvicorn app:app --host 0.0.0.0 --port 5000 --proxy-headers
应用程序.py
from fastapi import FastAPI, Request
app = FastAPI(title=\'Web app\')
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=[\'*\'],
allow_credentials=True,
allow_methods=[\'*\'],
allow_headers=[\'*\'],
)
@app.post(\'/\')
async def index(data: Input, request: Request):
return {\'bla\': \'bla\'}
-
我在这里看不到任何工人配置?您正在生成多少个应用程序实例?性能问题也可能来自很多事情,即使是你的代码,如果没有它会变得复杂
标签: nginx fastapi production uvicorn high-load