{"id":65485,"date":"2026-01-25T02:40:21","date_gmt":"2026-01-24T18:40:21","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/65485.html"},"modified":"2026-01-25T02:40:21","modified_gmt":"2026-01-24T18:40:21","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e6%9c%8d%e5%8a%a1%e5%99%a8-ubuntu-22-04-%e4%b8%8a%e9%83%a8%e7%bd%b2-fastapi-uvicorn-nginx-%e7%94%9f%e4%ba%a7%e7%ba%a7-python-web-%e6%9c%8d%e5%8a%a1%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/65485.html","title":{"rendered":"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357"},"content":{"rendered":"<p>\u672c\u6587\u4ece\u57fa\u7840\u73af\u5883\u51c6\u5907\u3001\u90e8\u7f72\u67b6\u6784\u8bbe\u8ba1\u3001\u6027\u80fd\u8c03\u4f18\u3001\u5b89\u5168\u914d\u7f6e\u5230\u76d1\u63a7\u6307\u6807\u91c7\u96c6&#xff0c;\u5168\u6d41\u7a0b\u8bb2\u89e3\u5982\u4f55\u5728 Ubuntu 22.04 \u670d\u52a1\u5668 \u4e0a\u6784\u5efa\u4e00\u4e2a\u53ef\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u7684 FastAPI &#043; Uvicorn &#043; Nginx Python Web \u670d\u52a1\u5e73\u53f0\u3002A5\u6570\u636e\u91cd\u70b9\u805a\u7126\u5b9e\u6218\u7ec6\u8282\u3001\u7cfb\u7edf\u53c2\u6570\u914d\u7f6e\u3001\u6027\u80fd\u8bc4\u6d4b\u4e0e\u95ee\u9898\u6392\u67e5\u65b9\u6cd5&#xff0c;\u9002\u5408\u6709\u4e00\u5b9a Linux \/ \u7f51\u7edc \/ Python \u7ecf\u9a8c\u7684\u5f00\u53d1\u4e0e\u8fd0\u7ef4\u4eba\u5458\u9605\u8bfb\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u76ee\u6807\u67b6\u6784\u4e0e\u9002\u7528\u573a\u666f<\/h3>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e0b&#xff0c;\u5355\u7eaf\u4f7f\u7528 Uvicorn \u76d1\u542c\u5916\u90e8\u8bf7\u6c42\u5b58\u5728\u6027\u80fd\u548c\u5b89\u5168\u98ce\u9669&#xff0c;\u56e0\u6b64\u6211\u4eec\u91c7\u7528\u5982\u4e0b\u90e8\u7f72\u67b6\u6784&#xff1a;<\/p>\n<p>Internet<br \/>\n   \u2502<br \/>\n   \u25bc<br \/>\n Nginx (\u53cd\u5411\u4ee3\u7406 &#043; SSL\/TLS)<br \/>\n   \u2502  proxy_pass<br \/>\n   \u25bc<br \/>\nUvicorn Workers (\u57fa\u4e8e uvloop &#043; Gunicorn \u7ba1\u7406)<br \/>\n   \u2502<br \/>\nFastAPI Application<br \/>\n   \u2502<br \/>\n PostgreSQL \/ Redis \/ \u540e\u7aef\u5fae\u670d\u52a1<\/p>\n<p>\u9002\u7528\u573a\u666f\u5305\u62ec&#xff1a;<\/p>\n<ul>\n<li>\u9ad8\u5e76\u53d1 API \u670d\u52a1<\/li>\n<li>\u5fae\u670d\u52a1\u67b6\u6784\u4e2d HTTP \u63a5\u53e3\u90e8\u7f72<\/li>\n<li>\u9700\u8981TLS\/HTTPS\u5b89\u5168\u8bbf\u95ee\u7684\u751f\u4ea7\u73af\u5883<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e8c\u3001\u9999\u6e2f\u670d\u52a1\u5668www.a5idc.com\u786c\u4ef6\u4e0e\u7cfb\u7edf\u5efa\u8bae<\/h3>\n<table>\n<tr>\u9879\u76ee\u63a8\u8350\u914d\u7f6e\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>CPU<\/td>\n<td>4 \u6838\u53ca\u4ee5\u4e0a<\/td>\n<td>\u5e76\u53d1\u8bf7\u6c42\u66f4\u7a33\u5b9a<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58<\/td>\n<td>8GB \u4ee5\u4e0a<\/td>\n<td>Python GC &#043; \u7f13\u5b58\u7a7a\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\u78c1\u76d8<\/td>\n<td>NVMe 150GB \u4ee5\u4e0a<\/td>\n<td>\u5feb\u901f\u65e5\u5fd7\u5199\u5165<\/td>\n<\/tr>\n<tr>\n<td>\u7f51\u7edc<\/td>\n<td>\u516c\u7f51\u5e26\u5bbd 100Mbps<\/td>\n<td>API \u5bf9\u5916\u8bbf\u95ee<\/td>\n<\/tr>\n<tr>\n<td>\u7cfb\u7edf<\/td>\n<td>Ubuntu 22.04 LTS<\/td>\n<td>\u6700\u65b0\u7a33\u5b9a\u7248<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5916\u7f51\u8bbf\u95ee\u5efa\u8bae\u90e8\u7f72\u5177\u6709 DDoS \u9632\u62a4\u7684 BGP \u7ebf\u8def\u670d\u52a1\u5668&#xff0c;\u4f8b\u5982 CN2 \/ \u7535\u4fe1\u76f4\u8fde\u65b9\u6848&#xff0c;\u4ee5\u964d\u4f4e\u7f51\u7edc\u6296\u52a8\u548c\u4e22\u5305\u7387\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u7cfb\u7edf\u521d\u59cb\u5316\u4e0e\u4f9d\u8d56\u5b89\u88c5<\/h3>\n<h4>3.1 \u7cfb\u7edf\u66f4\u65b0<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> update <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> upgrade -y<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">reboot<\/span><\/p>\n<h4>3.2 \u5b89\u88c5 Python \u73af\u5883\u4e0e\u5fc5\u5907\u5de5\u5177<\/h4>\n<p><span class=\"token comment\"># \u5b89\u88c5 Python3.10<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> -y python3.10 python3.10-venv python3.10-dev python3-pip build-essential<\/p>\n<p><span class=\"token comment\"># \u5e38\u7528\u5de5\u5177<\/span><br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> -y nginx ufw <span class=\"token function\">git<\/span><\/p>\n<h4>3.3 \u5efa\u7acb\u865a\u62df\u73af\u5883<\/h4>\n<p>\u8fdb\u5165\u5e94\u7528\u76ee\u5f55 \/opt\/fastapi_app&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">mkdir<\/span> -p \/opt\/fastapi_app<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">chown<\/span> <span class=\"token environment constant\">$USER<\/span><span class=\"token builtin class-name\">:<\/span><span class=\"token environment constant\">$USER<\/span> \/opt\/fastapi_app<br \/>\n<span class=\"token builtin class-name\">cd<\/span> \/opt\/fastapi_app<\/p>\n<p>python3.10 -m venv venv<br \/>\n<span class=\"token builtin class-name\">source<\/span> venv\/bin\/activate<\/p>\n<hr \/>\n<h3>\u56db\u3001\u5e94\u7528\u4ee3\u7801\u7ed3\u6784\u4e0e\u793a\u4f8b<\/h3>\n<h4>4.1 \u6700\u7b80 FastAPI \u9879\u76ee\u7ed3\u6784<\/h4>\n<p>fastapi_app\/<br \/>\n\u251c\u2500\u2500 app\/<br \/>\n\u2502   \u251c\u2500\u2500 main.py<br \/>\n\u2502   \u251c\u2500\u2500 api\/<br \/>\n\u2502   \u2502   \u2514\u2500\u2500 v1.py<br \/>\n\u251c\u2500\u2500 requirements.txt<br \/>\n\u2514\u2500\u2500 logging.conf<\/p>\n<h4>4.2 \u793a\u4f8b\u4ee3\u7801<\/h4>\n<p>app\/main.py<\/p>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> FastAPI<br \/>\n<span class=\"token keyword\">import<\/span> uvicorn<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> FastAPI<span class=\"token punctuation\">(<\/span>title<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;\u793a\u4f8b FastAPI \u670d\u52a1&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/health&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">health_check<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;status&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;OK&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;app<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/items\/{item_id}&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">read_item<\/span><span class=\"token punctuation\">(<\/span>item_id<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">int<\/span><span class=\"token punctuation\">,<\/span> q<span class=\"token punctuation\">:<\/span> <span class=\"token builtin\">str<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">None<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;item_id&#034;<\/span><span class=\"token punctuation\">:<\/span> item_id<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;q&#034;<\/span><span class=\"token punctuation\">:<\/span> q<span class=\"token punctuation\">}<\/span><\/p>\n<p>app\/api\/v1.py<\/p>\n<p><span class=\"token keyword\">from<\/span> fastapi <span class=\"token keyword\">import<\/span> APIRouter<\/p>\n<p>router <span class=\"token operator\">&#061;<\/span> APIRouter<span class=\"token punctuation\">(<\/span>prefix<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;\/v1&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token decorator annotation punctuation\">&#064;router<span class=\"token punctuation\">.<\/span>get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\/ping&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">ping<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token string\">&#034;message&#034;<\/span><span class=\"token punctuation\">:<\/span> <span class=\"token string\">&#034;pong&#034;<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p>requirements.txt<\/p>\n<p>fastapi&#061;&#061;0.99.1<br \/>\nuvicorn&#061;&#061;0.23.2<br \/>\ngunicorn&#061;&#061;20.1.0<\/p>\n<hr \/>\n<h3>\u4e94\u3001\u4f7f\u7528 Gunicorn \u7ba1\u7406 Uvicorn Workers<\/h3>\n<h4>5.1 Gunicorn &#043; Uvicorn \u914d\u7f6e<\/h4>\n<p>\u521b\u5efa\u542f\u52a8\u811a\u672c gunicorn_conf.py&#xff1a;<\/p>\n<p><span class=\"token keyword\">import<\/span> multiprocessing<\/p>\n<p>workers <span class=\"token operator\">&#061;<\/span> multiprocessing<span class=\"token punctuation\">.<\/span>cpu_count<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">2<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><br \/>\nworker_class <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;uvicorn.workers.UvicornWorker&#034;<\/span><br \/>\nbind <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;127.0.0.1:8000&#034;<\/span><br \/>\ntimeout <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">30<\/span><br \/>\nkeepalive <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">5<\/span><\/p>\n<p><span class=\"token comment\"># \u65e5\u5fd7<\/span><br \/>\nerrorlog <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;\/opt\/fastapi_app\/logs\/gunicorn.error.log&#034;<\/span><br \/>\naccesslog <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;\/opt\/fastapi_app\/logs\/gunicorn.access.log&#034;<\/span><br \/>\nloglevel <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;info&#034;<\/span><\/p>\n<h4>5.2 \u542f\u52a8\u547d\u4ee4<\/h4>\n<p><span class=\"token function\">mkdir<\/span> -p logs<br \/>\ngunicorn app.main:app -c gunicorn_conf.py<\/p>\n<hr \/>\n<h3>\u516d\u3001Nginx \u53cd\u5411\u4ee3\u7406\u914d\u7f6e<\/h3>\n<h4>6.1 Nginx \u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u521b\u5efa \/etc\/nginx\/sites-available\/fastapi&#xff1a;<\/p>\n<p>server {<br \/>\n    listen 80;<br \/>\n    server_name example.com;<\/p>\n<p>    location \/ {<br \/>\n        proxy_set_header X-Real-IP $remote_addr;<br \/>\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br \/>\n        proxy_set_header Host $host;<br \/>\n        proxy_http_version 1.1;<br \/>\n        proxy_set_header Connection &#034;&#034;;<br \/>\n        proxy_pass http:\/\/127.0.0.1:8000;<br \/>\n    }<\/p>\n<p>    location \/static\/ {<br \/>\n        alias \/opt\/fastapi_app\/app\/static\/;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u542f\u7528\u914d\u7f6e&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">ln<\/span> -s \/etc\/nginx\/sites-available\/fastapi \/etc\/nginx\/sites-enabled\/<br \/>\n<span class=\"token function\">sudo<\/span> nginx -t<br \/>\n<span class=\"token function\">sudo<\/span> systemctl restart nginx<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u751f\u4ea7\u73af\u5883 SSL\/TLS<\/h3>\n<p>\u4f7f\u7528 Certbot \u83b7\u53d6\u514d\u8d39 Let\u2019s Encrypt \u8bc1\u4e66&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> -y certbot python3-certbot-nginx<br \/>\n<span class=\"token function\">sudo<\/span> certbot &#8211;nginx -d example.com<\/p>\n<p>\u81ea\u52a8\u7eed\u671f\u68c0\u67e5&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> systemctl status certbot.timer<\/p>\n<hr \/>\n<h3>\u516b\u3001\u9632\u706b\u5899\u4e0e\u5b89\u5168<\/h3>\n<p>\u542f\u7528 UFW \u5e76\u5141\u8bb8\u5fc5\u8981\u7aef\u53e3&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> ufw allow OpenSSH<br \/>\n<span class=\"token function\">sudo<\/span> ufw allow <span class=\"token string\">&#039;Nginx Full&#039;<\/span><br \/>\n<span class=\"token function\">sudo<\/span> ufw <span class=\"token builtin class-name\">enable<\/span><\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u65e5\u5fd7\u3001\u76d1\u63a7\u4e0e\u6027\u80fd\u6307\u6807<\/h3>\n<h4>9.1 \u65e5\u5fd7\u7b56\u7565<\/h4>\n<p><span class=\"token comment\"># \u65e5\u5fd7\u76ee\u5f55<\/span><br \/>\n\/opt\/fastapi_app\/logs\/<br \/>\n<span class=\"token comment\"># Nginx access log<\/span><br \/>\n\/var\/log\/nginx\/access.log<\/p>\n<p>\u53ef\u7ed3\u5408 logrotate \u505a\u5b9a\u671f\u6e05\u7406&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">nano<\/span> \/etc\/logrotate.d\/fastapi<\/p>\n<p>\u5185\u5bb9\u793a\u4f8b&#xff1a;<\/p>\n<p>\/opt\/fastapi_app\/logs\/*.log {<br \/>\n    daily<br \/>\n    rotate 14<br \/>\n    compress<br \/>\n    missingok<br \/>\n    notifempty<br \/>\n    copytruncate<br \/>\n}<\/p>\n<h4>9.2 \u76d1\u63a7\u6307\u6807<\/h4>\n<table>\n<tr>\u6307\u6807\u91c7\u96c6\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>CPU \u5229\u7528\u7387<\/td>\n<td>top \/ htop \/ vmstat<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u4f7f\u7528<\/td>\n<td>free -m<\/td>\n<\/tr>\n<tr>\n<td>\u8fdb\u7a0b\u72b6\u6001<\/td>\n<td>ps aux<\/td>\n<\/tr>\n<tr>\n<td>\u7f51\u7edc\u5ef6\u8fdf<\/td>\n<td>MTR \/ traceroute<\/td>\n<\/tr>\n<tr>\n<td>99% \u54cd\u5e94\u65f6\u95f4<\/td>\n<td>external \u63a5\u53e3\u76d1\u63a7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u5341\u3001\u538b\u529b\u6d4b\u8bd5\u4e0e\u6027\u80fd\u8bc4\u6d4b<\/h3>\n<p>\u4f7f\u7528 wrk \u538b\u6d4b&#xff1a;<\/p>\n<p>wrk -t4 -c500 -d60s http:\/\/example.com\/health<\/p>\n<table>\n<tr>\u5e76\u53d1\u8fde\u63a5\u6570\u5e73\u5747\u54cd\u5e94\u65f6\u95f4(ms)\u541e\u5410 (req\/s)<\/tr>\n<tbody>\n<tr>\n<td>100<\/td>\n<td>18.5<\/td>\n<td>5200<\/td>\n<\/tr>\n<tr>\n<td>300<\/td>\n<td>42.7<\/td>\n<td>4800<\/td>\n<\/tr>\n<tr>\n<td>500<\/td>\n<td>96.3<\/td>\n<td>3500<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ed3\u679c\u53d7\u7f51\u7edc\u73af\u5883\u3001\u670d\u52a1\u5668\u786c\u4ef6\u53ca\u5e94\u7528\u903b\u8f91\u5f71\u54cd&#xff0c;\u4e0a\u8868\u4e3a\u53c2\u8003\u6570\u636e\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e00\u3001\u6545\u969c\u6392\u67e5\u4e0e\u5e38\u89c1\u95ee\u9898<\/h3>\n<h4>11.1 502 Bad Gateway<\/h4>\n<p>\u68c0\u67e5&#xff1a;<\/p>\n<ul>\n<li>Uvicorn\/Gunicorn \u662f\u5426\u8fd0\u884c<\/li>\n<li>Nginx \u914d\u7f6e proxy_pass \u662f\u5426\u6b63\u786e<\/li>\n<li>\u7aef\u53e3\u662f\u5426\u88ab\u9632\u706b\u5899\u963b\u65ad<\/li>\n<\/ul>\n<h4>11.2 \u8d85\u65f6<\/h4>\n<p>Gunicorn timeout \u53ef\u9002\u5f53\u8c03\u5927&#xff0c;\u6216\u4f18\u5316\u4e1a\u52a1\u903b\u8f91\u51cf\u5c11\u963b\u585e\u8c03\u7528\u3002<\/p>\n<hr \/>\n<h3>\u5341\u4e8c\u3001\u540e\u7eed\u4f18\u5316\u5efa\u8bae<\/h3>\n<h4>12.1 \u4f7f\u7528 Supervisor \u7ba1\u7406\u8fdb\u7a0b<\/h4>\n<p><span class=\"token function\">sudo<\/span> <span class=\"token function\">apt<\/span> <span class=\"token function\">install<\/span> supervisor<br \/>\n<span class=\"token function\">sudo<\/span> <span class=\"token function\">nano<\/span> \/etc\/supervisor\/conf.d\/fastapi.conf<\/p>\n<p>\u5185\u5bb9&#xff1a;<\/p>\n<p>[program:fastapi]<br \/>\ncommand&#061;\/opt\/fastapi_app\/venv\/bin\/gunicorn app.main:app -c \/opt\/fastapi_app\/gunicorn_conf.py<br \/>\ndirectory&#061;\/opt\/fastapi_app<br \/>\nautostart&#061;true<br \/>\nautorestart&#061;true<br \/>\nstderr_logfile&#061;\/opt\/fastapi_app\/logs\/fastapi.err.log<br \/>\nstdout_logfile&#061;\/opt\/fastapi_app\/logs\/fastapi.out.log<\/p>\n<p>\u91cd\u8f7d&#xff1a;<\/p>\n<p><span class=\"token function\">sudo<\/span> supervisorctl reread<br \/>\n<span class=\"token function\">sudo<\/span> supervisorctl update<\/p>\n<h4>12.2 HTTPS \u5f3a\u5236 HSTS<\/h4>\n<p>\u5728 Nginx SSL \u914d\u7f6e\u4e2d\u6dfb\u52a0&#xff1a;<\/p>\n<p>add_header Strict-Transport-Security &#034;max-age&#061;31536000; includeSubDomains&#034; always;<\/p>\n<hr \/>\n<h3>\u7ed3\u8bed<\/h3>\n<p>\u901a\u8fc7A5\u6570\u636e\u7684\u6559\u7a0b&#xff0c;\u4f60\u5df2\u7ecf\u5b8c\u6210\u4e86\u4e00\u4e2a\u57fa\u4e8e Ubuntu 22.04 &#043; FastAPI &#043; Uvicorn &#043; Nginx \u7684\u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u90e8\u7f72&#xff0c;\u6db5\u76d6\u67b6\u6784\u8bbe\u8ba1\u3001\u914d\u7f6e\u7ec6\u8282\u3001\u6027\u80fd\u6d4b\u8bd5\u3001\u65e5\u5fd7\u76d1\u63a7\u7b49\u5173\u952e\u5185\u5bb9\u3002\u63a5\u4e0b\u6765\u53ef\u6839\u636e\u4e1a\u52a1\u89c4\u6a21&#xff0c;\u7ed3\u5408 Redis \u7f13\u5b58\u3001PostgreSQL \u6570\u636e\u5e93\u4ee5\u53ca CI\/CD \u81ea\u52a8\u5316\u90e8\u7f72\u8fdb\u4e00\u6b65\u63d0\u5347\u7cfb\u7edf\u7a33\u5b9a\u6027\u4e0e\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u5982\u9700\u66f4\u6df1\u5165\u7684\u6027\u80fd\u8c03\u4f18\u6216\u7ed3\u5408 Kubernetes \/ Docker \u90e8\u7f72\u65b9\u6848&#xff0c;\u4e5f\u53ef\u4ee5\u5728\u6b64\u57fa\u7840\u4e0a\u62d3\u5c55\u8ba8\u8bba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4ece\u57fa\u7840\u73af\u5883\u51c6\u5907\u3001\u90e8\u7f72\u67b6\u6784\u8bbe\u8ba1\u3001\u6027\u80fd\u8c03\u4f18\u3001\u5b89\u5168\u914d\u7f6e\u5230\u76d1\u63a7\u6307\u6807\u91c7\u96c6&#xff0c;\u5168\u6d41\u7a0b\u8bb2\u89e3\u5982\u4f55\u5728 Ubuntu 22.04 \u670d\u52a1\u5668 \u4e0a\u6784\u5efa\u4e00\u4e2a\u53ef\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u7684 FastAPI  Uvicorn  Nginx Python Web \u670d\u52a1\u5e73\u53f0\u3002A5\u6570\u636e\u91cd\u70b9\u805a\u7126\u5b9e\u6218\u7ec6\u8282\u3001\u7cfb\u7edf\u53c2\u6570\u914d\u7f6e\u3001\u6027\u80fd\u8bc4\u6d4b\u4e0e\u95ee\u9898\u6392\u67e5\u65b9\u6cd5&#xff0c;\u9002\u5408\u6709\u4e00\u5b9a Linux \/ \u7f51\u7edc \/ Python \u7ecf\u9a8c\u7684\u5f00\u53d1\u4e0e\u8fd0\u7ef4\u4eba\u5458\u9605\u8bfb\u3002\u4e00\u3001\u76ee\u6807\u67b6\u6784\u4e0e\u9002\u7528\u573a\u666f<br \/>\n\u5728\u751f\u4ea7\u73af\u5883\u4e0b&#xff0c;\u5355\u7eaf\u4f7f\u7528 Uvicorn \u76d1\u542c<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[72,103,43],"topic":[],"class_list":["post-65485","post","type-post","status-publish","format-standard","hentry","category-server","tag-fastapi","tag-ubuntu","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/65485.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6587\u4ece\u57fa\u7840\u73af\u5883\u51c6\u5907\u3001\u90e8\u7f72\u67b6\u6784\u8bbe\u8ba1\u3001\u6027\u80fd\u8c03\u4f18\u3001\u5b89\u5168\u914d\u7f6e\u5230\u76d1\u63a7\u6307\u6807\u91c7\u96c6&#xff0c;\u5168\u6d41\u7a0b\u8bb2\u89e3\u5982\u4f55\u5728 Ubuntu 22.04 \u670d\u52a1\u5668 \u4e0a\u6784\u5efa\u4e00\u4e2a\u53ef\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u7684 FastAPI Uvicorn Nginx Python Web \u670d\u52a1\u5e73\u53f0\u3002A5\u6570\u636e\u91cd\u70b9\u805a\u7126\u5b9e\u6218\u7ec6\u8282\u3001\u7cfb\u7edf\u53c2\u6570\u914d\u7f6e\u3001\u6027\u80fd\u8bc4\u6d4b\u4e0e\u95ee\u9898\u6392\u67e5\u65b9\u6cd5&#xff0c;\u9002\u5408\u6709\u4e00\u5b9a Linux \/ \u7f51\u7edc \/ Python \u7ecf\u9a8c\u7684\u5f00\u53d1\u4e0e\u8fd0\u7ef4\u4eba\u5458\u9605\u8bfb\u3002\u4e00\u3001\u76ee\u6807\u67b6\u6784\u4e0e\u9002\u7528\u573a\u666f \u5728\u751f\u4ea7\u73af\u5883\u4e0b&#xff0c;\u5355\u7eaf\u4f7f\u7528 Uvicorn \u76d1\u542c\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/65485.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-24T18:40:21+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65485.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/65485.html\",\"name\":\"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-24T18:40:21+00:00\",\"dateModified\":\"2026-01-24T18:40:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/65485.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/65485.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/65485.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/65485.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u672c\u6587\u4ece\u57fa\u7840\u73af\u5883\u51c6\u5907\u3001\u90e8\u7f72\u67b6\u6784\u8bbe\u8ba1\u3001\u6027\u80fd\u8c03\u4f18\u3001\u5b89\u5168\u914d\u7f6e\u5230\u76d1\u63a7\u6307\u6807\u91c7\u96c6&#xff0c;\u5168\u6d41\u7a0b\u8bb2\u89e3\u5982\u4f55\u5728 Ubuntu 22.04 \u670d\u52a1\u5668 \u4e0a\u6784\u5efa\u4e00\u4e2a\u53ef\u7528\u4e8e\u751f\u4ea7\u73af\u5883\u7684 FastAPI Uvicorn Nginx Python Web \u670d\u52a1\u5e73\u53f0\u3002A5\u6570\u636e\u91cd\u70b9\u805a\u7126\u5b9e\u6218\u7ec6\u8282\u3001\u7cfb\u7edf\u53c2\u6570\u914d\u7f6e\u3001\u6027\u80fd\u8bc4\u6d4b\u4e0e\u95ee\u9898\u6392\u67e5\u65b9\u6cd5&#xff0c;\u9002\u5408\u6709\u4e00\u5b9a Linux \/ \u7f51\u7edc \/ Python \u7ecf\u9a8c\u7684\u5f00\u53d1\u4e0e\u8fd0\u7ef4\u4eba\u5458\u9605\u8bfb\u3002\u4e00\u3001\u76ee\u6807\u67b6\u6784\u4e0e\u9002\u7528\u573a\u666f \u5728\u751f\u4ea7\u73af\u5883\u4e0b&#xff0c;\u5355\u7eaf\u4f7f\u7528 Uvicorn \u76d1\u542c","og_url":"https:\/\/www.wsisp.com\/helps\/65485.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-24T18:40:21+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/65485.html","url":"https:\/\/www.wsisp.com\/helps\/65485.html","name":"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-24T18:40:21+00:00","dateModified":"2026-01-24T18:40:21+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/65485.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/65485.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/65485.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5982\u4f55\u5728\u670d\u52a1\u5668 Ubuntu 22.04 \u4e0a\u90e8\u7f72 FastAPI + Uvicorn + Nginx \u751f\u4ea7\u7ea7 Python Web \u670d\u52a1\u6307\u5357"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/65485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=65485"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/65485\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=65485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=65485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=65485"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=65485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}