{"id":60932,"date":"2026-01-16T16:25:43","date_gmt":"2026-01-16T08:25:43","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/60932.html"},"modified":"2026-01-16T16:25:43","modified_gmt":"2026-01-16T08:25:43","slug":"%e4%bb%8e-0-%e5%88%b0-1-%e6%8e%8c%e6%8f%a1-gunicorn%ef%bc%9a%e8%ae%a9-python-web-%e5%ba%94%e7%94%a8%e9%a3%9e%e8%b5%b7%e6%9d%a5%e7%9a%84-wsgi-%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/60932.html","title":{"rendered":"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;<\/li>\n<li>\u73af\u5883\u8bf4\u660e<\/li>\n<li>1\u3001Gunicorn \u662f\u4ec0\u4e48&#xff1f;<\/li>\n<li>2\u3001\u5b89\u88c5 &amp; \u6700\u5c0f\u53ef\u8fd0\u884c\u793a\u4f8b<\/li>\n<li>3\u3001\u6838\u5fc3\u6982\u5ff5\u901f\u89c8<\/li>\n<li>4\u3001\u5e76\u53d1\u6a21\u578b\u600e\u4e48\u9009&#xff1f;<\/li>\n<li>5\u3001\u751f\u4ea7\u7ea7\u914d\u7f6e\u6e05\u5355&#xff08;\u53ef\u76f4\u63a5\u6284&#xff09;<\/li>\n<li>6\u3001\u4f18\u96c5\u91cd\u542f &amp; \u96f6\u5b95\u673a\u53d1\u5e03<\/li>\n<li>7\u3001\u5bb9\u5668\u5316\u6700\u4f73\u5b9e\u8df5<\/li>\n<li>8\u3001\u5e38\u89c1\u5751 8 \u5219<\/li>\n<li>\u5c0f\u7ed3 &amp; \u5ef6\u4f38\u9605\u8bfb<\/li>\n<\/ul>\n<hr \/>\n<h2>\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;<\/h2>\n<p>\u672c\u5730\u5f00\u53d1\u65f6&#xff0c;\u4e00\u6761 flask run \u6216 python manage.py runserver \u5c31\u80fd\u8ba9\u5e94\u7528\u6109\u5feb\u5730\u8dd1\u8d77\u6765\u3002\u4f46\u8fd9\u4e24\u53e5\u547d\u4ee4\u80cc\u540e\u90fd\u6302\u7740 \u5355\u7ebf\u7a0b\u3001\u5355\u8fdb\u7a0b\u3001\u81ea\u52a8\u91cd\u8f7d\u3001\u8c03\u8bd5\u94a9\u5b50 \u7684\u201c\u5f00\u53d1\u670d\u52a1\u5668\u201d\u6807\u7b7e&#xff0c;\u4e00\u65e6\u653e\u5230\u751f\u4ea7\u6d41\u91cf\u4e0b&#xff0c;\u5c31\u4f1a\u51fa\u73b0&#xff1a;<\/p>\n<ul>\n<li>\u5e76\u53d1\u5904\u7406\u80fd\u529b\u5f31&#xff08;\u5355\u8fdb\u7a0b\/\u7ebf\u7a0b&#xff09;<\/li>\n<li>\u65e0\u5b89\u5168\u7194\u65ad&#xff08;\u6162\u8bf7\u6c42\u4f1a\u62d6\u57ae\u6574\u4e2a\u670d\u52a1&#xff09;<\/li>\n<li>\u9759\u6001\u6587\u4ef6\u6548\u7387\u4f4e<\/li>\n<li>\u4e0d\u652f\u6301\u70ed\u5347\u7ea7\u3001\u4f18\u96c5\u91cd\u542f<\/li>\n<\/ul>\n<p>\u4e8e\u662f&#xff0c;\u6211\u4eec\u9700\u8981\u4e00\u4e2a \u4e13\u6ce8\u751f\u4ea7\u573a\u666f\u7684 WSGI \u670d\u52a1\u5668\u2014\u2014Gunicorn&#xff08;Green Unicorn&#xff09;\u3002<\/p>\n<hr \/>\n<h2>\u73af\u5883\u8bf4\u660e<\/h2>\n<p>\u64cd\u4f5c\u7cfb\u7edf&#xff1a;Ubuntu 24.04.1 LTS Python \u7248\u672c&#xff1a;3.10.19 Flask \u7248\u672c&#xff1a;3.1.2 Gunicorn \u7248\u672c&#xff1a;23.0.0<\/p>\n<p>Visual Studio Code \u7248\u672c&#xff1a;1.106.3 Visual Studio Code \u63d2\u4ef6&#xff1a;Python\u3001Pylance\u3001Docker \u63d2\u4ef6\u53ef\u53c2\u8003&#xff1a;https:\/\/blog.csdn.net\/achi010\/article\/details\/154604215<\/p>\n<hr \/>\n<h2>1\u3001Gunicorn \u662f\u4ec0\u4e48&#xff1f;<\/h2>\n<ul>\n<li>\u7528 Python \u5199\u7684\u8f7b\u91cf\u7ea7 WSGI HTTP Server<\/li>\n<li>\u8bde\u751f\u4e8e 2010 \u5e74&#xff0c;\u76ee\u524d\u7531 Benoit Chesneau \u7b49\u4eba\u7ef4\u62a4<\/li>\n<li>\u53e3\u53f7&#xff1a;\u201cWSGI HTTP Server for UNIX\u201d&#xff0c;\u5b9a\u4f4d\u5c31\u662f Unix\/Linux \u4e0a\u8dd1 Python Web \u7684\u751f\u4ea7\u5165\u53e3<\/li>\n<li>\u652f\u6301\u591a\u79cd\u5e76\u53d1\u6a21\u578b&#xff08;sync\u3001gevent\u3001eventlet\u3001tornado\u3001meinheld\u3001uvloop\u2026&#xff09;<\/li>\n<li>\u96f6\u914d\u7f6e\u5373\u53ef\u8dd1&#xff0c;\u4e5f\u63d0\u4f9b 100&#043; \u53ef\u8c03\u53c2\u6570&#xff0c;\u53ef\u73a9\u6027\u9ad8<\/li>\n<\/ul>\n<p>\u5b98\u7f51&#xff1a;https:\/\/gunicorn.org\/<\/p>\n<p>\u90e8\u7f72\u76f8\u5173\u6587\u6863&#xff08;Nginx \u76f8\u5173\u914d\u7f6e\u6ce8\u610f\u4e8b\u9879&#xff09;&#xff1a;https:\/\/docs.gunicorn.org\/en\/latest\/deploy.html<\/p>\n<p>GitHub&#xff1a;https:\/\/github.com\/benoitc\/gunicorn<\/p>\n<p>Gunicorn \u2014 Flask Documentation (3.1.x)<\/p>\n<hr \/>\n<h2>2\u3001\u5b89\u88c5 &amp; \u6700\u5c0f\u53ef\u8fd0\u884c\u793a\u4f8b<\/h2>\n<p>&#xff08;1&#xff09;\u5b89\u88c5<\/p>\n<p>python -m venv venv<br \/>\n<span class=\"token builtin class-name\">source<\/span> venv\/bin\/activate<br \/>\npip <span class=\"token function\">install<\/span> gunicorn flask<\/p>\n<p>&#xff08;2&#xff09;\u793a\u4f8b\u5e94\u7528 app.py<\/p>\n<p><span class=\"token keyword\">from<\/span> flask <span class=\"token keyword\">import<\/span> Flask<span class=\"token punctuation\">,<\/span> jsonify<br \/>\n<span class=\"token keyword\">import<\/span> os<\/p>\n<p>app <span class=\"token operator\">&#061;<\/span> Flask<span class=\"token punctuation\">(<\/span>__name__<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;\/&#034;<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">def<\/span> <span class=\"token function\">index<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> jsonify<span class=\"token punctuation\">(<\/span><br \/>\n        pid<span class=\"token operator\">&#061;<\/span>os<span class=\"token punctuation\">.<\/span>getpid<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        msg<span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#034;Hello from Gunicorn!&#034;<\/span><br \/>\n    <span class=\"token punctuation\">)<\/span><\/p>\n<p>&#xff08;3&#xff09;\u542f\u52a8\u6d4b\u8bd5<\/p>\n<p>gunicorn app:app -b <span class=\"token number\">0.0<\/span>.0.0:8000<\/p>\n<p>\u8bbf\u95ee http:\/\/localhost:8000 &#xff0c;\u770b\u5230\u8fd4\u56de\u7684\u8fdb\u7a0b PID&#xff0c;\u8bf4\u660e Gunicorn \u5df2\u7ecf\u5728\u80cc\u540e\u76d1\u542c 8000 \u7aef\u53e3\u3002<\/p>\n<hr \/>\n<h2>3\u3001\u6838\u5fc3\u6982\u5ff5\u901f\u89c8<\/h2>\n<table>\n<tr>\u672f\u8bed\u542b\u4e49<\/tr>\n<tbody>\n<tr>\n<td align=\"left\">Worker<\/td>\n<td align=\"left\">\u5b9e\u9645\u5904\u7406\u8bf7\u6c42\u7684\u8fdb\u7a0b\/\u7ebf\u7a0b<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Master<\/td>\n<td align=\"left\">\u7ba1\u7406 Worker \u751f\u547d\u5468\u671f\u7684\u5b88\u62a4\u8fdb\u7a0b<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Pre-fork<\/td>\n<td align=\"left\">Master \u5148 fork() \u51fa N \u4e2a Worker&#xff0c;\u518d\u5171\u540c accept() \u540c\u4e00\u4e2a Socket<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Sync<\/td>\n<td align=\"left\">\u9ed8\u8ba4\u6a21\u578b&#xff0c;\u6bcf\u4e2a Worker \u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a\u8bf7\u6c42&#xff08;\u9002\u5408 CPU \u5bc6\u96c6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Async<\/td>\n<td align=\"left\">\u57fa\u4e8e gevent\/eventlet&#xff0c;\u4e00\u4e2a Worker \u53ef\u5e76\u53d1\u4e0a\u5343\u4e2a\u7eff\u8272\u7ebf\u7a0b&#xff08;\u9002\u5408 I\/O \u5bc6\u96c6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">graceful timeout<\/td>\n<td align=\"left\">\u7ed9 Worker \u7684\u6700\u5927\u201c\u4f18\u96c5\u201d\u5904\u7406\u65f6\u95f4&#xff0c;\u8d85\u65f6\u5219\u5f3a\u5236 SIGKILL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>4\u3001\u5e76\u53d1\u6a21\u578b\u600e\u4e48\u9009&#xff1f;<\/h2>\n<p>&#xff08;1&#xff09;CPU \u5bc6\u96c6&#xff08;\u56fe\u50cf\u5904\u7406\u3001\u52a0\u5bc6\u3001\u5927\u6570\u636e\u8ba1\u7b97&#xff09;<\/p>\n<p>\u63a8\u8350&#xff1a; sync \u6216 gthread&#xff0c;Worker \u6570 \u2248 CPU \u6838\u6570<\/p>\n<p>gunicorn app:app -w <span class=\"token number\">4<\/span> -k <span class=\"token function\">sync<\/span><br \/>\n<span class=\"token comment\"># \u6216\u8005<\/span><br \/>\ngunicorn app:app -w <span class=\"token number\">4<\/span> -k gthread<\/p>\n<p>&#xff08;2&#xff09;I\/O \u5bc6\u96c6&#xff08;\u4ee3\u7406\u3001API Gateway\u3001\u722c\u866b\u805a\u5408&#xff09;<\/p>\n<p>\u63a8\u8350&#xff1a; gevent<\/p>\n<p>pip <span class=\"token function\">install<\/span> gevent<br \/>\ngunicorn app:app -k gevent -w <span class=\"token number\">2<\/span> &#8211;worker-connections <span class=\"token number\">1000<\/span><\/p>\n<p>&#xff08;3&#xff09;\u6df7\u5408\u573a\u666f&#xff08;\u6709\u70b9\u590d\u6742&#xff0c;\u6682\u4e0d\u8ba8\u8bba&#xff09;<\/p>\n<p>gthread \u515c\u5e95&#xff0c;\u6216\u8005\u628a CPU \u4efb\u52a1\u62c6\u5230 Celery&#xff0c;Web \u5c42\u4fdd\u6301\u8f7b\u91cf<\/p>\n<hr \/>\n<h2>5\u3001\u751f\u4ea7\u7ea7\u914d\u7f6e\u6e05\u5355&#xff08;\u53ef\u76f4\u63a5\u6284&#xff09;<\/h2>\n<p>\u914d\u7f6e\u53c2\u6570 &#8211; \u5b98\u65b9\u8bf4\u660e\u6587\u6863&#xff1a;https:\/\/docs.gunicorn.org\/en\/stable\/settings.html#server-mechanics<\/p>\n<p># gunicorn.conf.py<br \/>\n# \u7ed1\u5b9a\u5730\u5740\u548c\u7aef\u53e3<br \/>\nbind &#061; &#034;0.0.0.0:8000&#034;<\/p>\n<p># \u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf&#xff0c;\u901a\u5e38\u8bbe\u7f6e\u4e3aCPU\u6838\u5fc3\u6570\u76842\u500d\u52a01<br \/>\nworkers &#061; 4<\/p>\n<p># \u5de5\u4f5c\u8fdb\u7a0b\u7c7b\u578b&#xff0c;\u4f7f\u7528gevent\u5f02\u6b65\u6a21\u5f0f\u6216\u540c\u6b65\u6a21\u5f0f&#034;sync&#034;<br \/>\nworker_class &#061; &#034;gevent&#034;<\/p>\n<p># \u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u7684\u6700\u5927\u5e76\u53d1\u8fde\u63a5\u6570<br \/>\nworker_connections &#061; 1000<\/p>\n<p># \u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u5904\u7406\u591a\u5c11\u4e2a\u8bf7\u6c42\u540e\u91cd\u542f&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f<br \/>\nmax_requests &#061; 10000<\/p>\n<p># max_requests\u7684\u6296\u52a8\u503c&#xff0c;\u907f\u514d\u6240\u6709\u5de5\u4f5c\u8fdb\u7a0b\u540c\u65f6\u91cd\u542f<br \/>\nmax_requests_jitter &#061; 500<\/p>\n<p># \u4f18\u96c5\u5173\u95ed\u8d85\u65f6\u65f6\u95f4&#xff08;\u79d2&#xff09;<br \/>\ngraceful_timeout &#061; 30<\/p>\n<p># HTTP keep-alive\u6301\u7eed\u65f6\u95f4&#xff08;\u79d2&#xff09;<br \/>\nkeepalive &#061; 2<\/p>\n<p># \u662f\u5426\u9884\u52a0\u8f7d\u5e94\u7528&#xff0c;\u53ef\u4ee5\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6027\u80fd<br \/>\npreload_app &#061; True<\/p>\n<p># \u8bbf\u95ee\u65e5\u5fd7\u683c\u5f0f\u5b9a\u4e49<br \/>\naccess_logformat &#061; &#039;%(h)s %(l)s %(u)s %(t)s &#034;%(r)s&#034; %(s)s %(b)s &#034;%(f)s&#034; &#034;%(a)s&#034; %(D)s&#039;<\/p>\n<p># \u8bbf\u95ee\u65e5\u5fd7\u8f93\u51fa\u4f4d\u7f6e&#xff0c;&#034;-&#034;\u8868\u793a\u6807\u51c6\u8f93\u51fa<br \/>\naccesslog &#061; &#034;-&#034;<\/p>\n<p># \u9519\u8bef\u65e5\u5fd7\u8f93\u51fa\u4f4d\u7f6e&#xff0c;&#034;-&#034;\u8868\u793a\u6807\u51c6\u8f93\u51fa<br \/>\nerrorlog &#061; &#034;-&#034;<\/p>\n<p># \u65e5\u5fd7\u7ea7\u522b&#xff1a;debug, info, warning, error, critical<br \/>\nloglevel &#061; &#034;info&#034;<\/p>\n<p>\u542f\u52a8&#xff1a;<\/p>\n<p>gunicorn app:app -c gunicorn.conf.py<\/p>\n<hr \/>\n<h2>6\u3001\u4f18\u96c5\u91cd\u542f &amp; \u96f6\u5b95\u673a\u53d1\u5e03<\/h2>\n<p>&#xff08;1&#xff09;\u6536\u5230 HUP \u4fe1\u53f7\u540e&#xff0c;Master \u4f1a\u91cd\u65b0\u8f7d\u5165\u4ee3\u7801&#xff0c;\u65b0 Worker \u9010\u6b65\u66ff\u6362\u65e7 Worker&#xff0c;\u65e7\u8bf7\u6c42\u4e0d\u53d7\u5f71\u54cd&#xff1a;<\/p>\n<p><span class=\"token function\">kill<\/span> -HUP <span class=\"token operator\">&lt;<\/span>master_pid<span class=\"token operator\">&gt;<\/span><\/p>\n<p>&#xff08;2&#xff09;\u5982\u679c\u5728\u5bb9\u5668\u4e2d&#xff0c;\u53ef\u628a PID 1 \u6362\u6210 gunicorn&#xff0c;docker stop \u4f1a\u81ea\u52a8\u53d1 SIGTERM&#xff0c;Gunicorn \u4f1a\u7b49\u5f85 graceful_timeout&#xff0c;\u518d\u9000\u51fa\u3002<\/p>\n<p>&#xff08;3&#xff09;\u84dd\u7eff \/ \u6eda\u52a8\u53d1\u5e03&#xff1a;\u7ed3\u5408 K8s RollingUpdate &#043; readinessProbe&#xff0c;\u5b9e\u73b0 \u96f6\u4e2d\u65ad\u3002<\/p>\n<hr \/>\n<h2>7\u3001\u5bb9\u5668\u5316\u6700\u4f73\u5b9e\u8df5<\/h2>\n<p>&#xff08;1&#xff09;Dockerfile&#xff08;\u591a\u9636\u6bb5\u3001\u975e root\u3001\u5065\u5eb7\u68c0\u67e5&#xff09;<\/p>\n<p># \u4f7f\u7528 Python 3.10.19 slim \u955c\u50cf\u4f5c\u4e3a\u591a\u9636\u6bb5\u6784\u5efa\u7684\u7b2c\u4e00\u9636\u6bb5&#xff0c;\u547d\u540d\u4e3a builder<br \/>\n# \u8fd9\u4e2a\u9636\u6bb5\u4e13\u95e8\u7528\u4e8e\u5b89\u88c5 Python \u4f9d\u8d56\u5305<br \/>\nFROM python:3.10.19-slim AS builder<br \/>\n# \u8bbe\u7f6e pip \u7684\u56fd\u5185\u955c\u50cf\u6e90\u4e3a\u9ed8\u8ba4<br \/>\nENV PIP_INDEX_URL&#061;https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<br \/>\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u4e3a \/app<br \/>\nWORKDIR \/app<br \/>\n# \u5c06\u9879\u76ee\u4e2d\u7684 requirements.txt \u6587\u4ef6\u590d\u5236\u5230\u5bb9\u5668\u7684 \/app \u76ee\u5f55\u4e2d<br \/>\nCOPY requirements.txt .<br \/>\n# \u5728 builder \u9636\u6bb5\u5b89\u88c5\u9879\u76ee\u6240\u9700\u7684 Python \u4f9d\u8d56\u5305\u5230\u7528\u6237\u76ee\u5f55<br \/>\nRUN pip install &#8211;user -r requirements.txt<\/p>\n<p># \u5f00\u59cb\u7b2c\u4e8c\u9636\u6bb5\u6784\u5efa&#xff0c;\u4f7f\u7528\u76f8\u540c\u7248\u672c\u7684 Python \u57fa\u7840\u955c\u50cf<br \/>\nFROM python:3.10.19-slim<br \/>\n# \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf PATH&#xff0c;\u4f7f\u7528\u6237\u5b89\u88c5\u7684\u5305\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528<br \/>\nENV PATH&#061;\/home\/app\/.local\/bin:$PATH<br \/>\n# \u521b\u5efa\u4e00\u4e2a UID \u4e3a 1000 \u7684 app \u7528\u6237\u5e76\u521b\u5efa\u5176\u4e3b\u76ee\u5f55<br \/>\n# \u8fd9\u6837\u907f\u514d\u4ee5\u540e\u4ee5 root \u7528\u6237\u8eab\u4efd\u8fd0\u884c\u5e94\u7528&#xff0c;\u63d0\u9ad8\u5b89\u5168\u6027<br \/>\nRUN useradd -m -u 1000 app<br \/>\n# \u8bbe\u7f6e\u5de5\u4f5c\u76ee\u5f55\u4e3a \/app<br \/>\nWORKDIR \/app<br \/>\n# \u4ece\u7b2c\u4e00\u9636\u6bb5(builder)\u590d\u5236\u5df2\u5b89\u88c5\u7684 Python \u5305\u5230 app \u7528\u6237\u7684\u672c\u5730\u76ee\u5f55<br \/>\nCOPY &#8211;from&#061;builder \/root\/.local \/home\/app\/.local<br \/>\n# \u5c06\u9879\u76ee\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u590d\u5236\u5230\u5bb9\u5668\u7684 \/app \u76ee\u5f55\u4e2d<br \/>\nCOPY . .<br \/>\n# \u5207\u6362\u5230 app \u7528\u6237\u6267\u884c\u540e\u7eed\u6307\u4ee4&#xff0c;\u589e\u5f3a\u5bb9\u5668\u5b89\u5168\u6027<br \/>\nUSER app<br \/>\n# \u58f0\u660e\u5bb9\u5668\u8fd0\u884c\u65f6\u76d1\u542c\u7684\u7aef\u53e3\u4e3a 8000<br \/>\nEXPOSE 8000<br \/>\n# \u914d\u7f6e\u5065\u5eb7\u68c0\u67e5&#xff0c;\u901a\u8fc7 Python \u811a\u672c\u53d1\u9001 HTTP \u8bf7\u6c42\u68c0\u6d4b\u5e94\u7528\u662f\u5426\u6b63\u5e38\u8fd0\u884c<br \/>\nHEALTHCHECK CMD python -c &#034;import requests; requests.get(&#039;http:\/\/localhost:8000\/healthz&#039;, timeout&#061;3)&#034;<br \/>\n# \u5bb9\u5668\u542f\u52a8\u65f6\u6267\u884c\u7684\u547d\u4ee4&#xff1a;\u4f7f\u7528 gunicorn WSGI \u670d\u52a1\u5668\u542f\u52a8 Flask \u5e94\u7528<br \/>\n# app:app \u8868\u793a\u5bfc\u5165 app \u6a21\u5757\u4e2d\u7684 app \u5bf9\u8c61<br \/>\n# -c gunicorn.conf.py \u8868\u793a\u4f7f\u7528 gunicorn.conf.py \u914d\u7f6e\u6587\u4ef6<br \/>\nCMD [&#034;gunicorn&#034;, &#034;app:app&#034;, &#034;-c&#034;, &#034;gunicorn.conf.py&#034;]<\/p>\n<p>&#xff08;2&#xff09;\u6784\u5efa\u955c\u50cf<\/p>\n<p>docker build -t app:0.1 <span class=\"token builtin class-name\">.<\/span><\/p>\n<p>&#xff08;3&#xff09;\u8fd0\u884c\u6d4b\u8bd5<\/p>\n<p>docker run &#8211;name app -p <span class=\"token number\">8000<\/span>:8000 -d app:0.1<\/p>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee&#xff1a;http:\/\/localhost:8000 \u770b\u5230\u8fd4\u56de\u7684\u8fdb\u7a0b PID&#xff0c;\u8bf4\u660e\u5bb9\u5668\u6b63\u5e38\u542f\u52a8<\/p>\n<hr \/>\n<h2>8\u3001\u5e38\u89c1\u5751 8 \u5219<\/h2>\n<li>\n<p>\u5fd8\u8bb0\u8bbe\u7f6e forwarded-allow-ips<\/p>\n<p>\u7528 Nginx \u505a\u53cd\u5411\u4ee3\u7406\u65f6&#xff0c;\u9700\u628a\u771f\u5b9e IP \u4f20\u8fc7\u53bb&#xff1a;<\/p>\n<p>forwarded-allow-ips &#061; &#034;*&#034; \u6216\u5177\u4f53 CIDR<\/p>\n<\/li>\n<li>\n<p>\u9759\u6001\u6587\u4ef6 404<\/p>\n<p>Gunicorn \u53ea\u8d1f\u8d23 \u52a8\u6001 WSGI&#xff0c;\u9759\u6001\u8d44\u6e90\u5e94\u4ea4\u7ed9 Nginx\/Caddy\/S3<\/p>\n<\/li>\n<li>\n<p>Worker \u5185\u5b58\u66b4\u6da8<\/p>\n<p>\u8bbe\u7f6e max_requests &#043; max_requests_jitter&#xff0c;\u8ba9 Worker \u5b9a\u671f\u201c\u91cd\u751f\u201d<\/p>\n<\/li>\n<li>\n<p>HTTPS \u91cd\u5b9a\u5411\u6b7b\u5faa\u73af<\/p>\n<p>\u5728\u4ee3\u7406\u5c42\u52a0 X-Forwarded-Proto: https&#xff0c;\u5e76\u5728 Flask \u52a0 ProxyFix<\/p>\n<\/li>\n<li>\n<p>Docker \u91cc PID 1 \u6536\u4e0d\u5230\u4fe1\u53f7<\/p>\n<p>\u7528 exec gunicorn \u2026 \u542f\u52a8&#xff0c;\u6216\u52a0 tini \u4f5c\u4e3a init<\/p>\n<\/li>\n<li>\n<p>Gevent \u4e0e C \u6269\u5c55\u51b2\u7a81<\/p>\n<p>\u90e8\u5206\u5e93&#xff08;\u5982 psycopg2&#xff09;\u9700\u6539\u7528 \u7eaf Python \u6216\u534f\u7a0b\u53cb\u597d \u7248\u672c&#xff08;psycopg2-binary \u2192 psycopg[binary]&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u65e5\u5fd7\u65f6\u95f4\u6233\u4e0d\u5bf9<\/p>\n<p>\u5bb9\u5668\u5185\u9ed8\u8ba4 UTC&#xff0c;\u53ef\u5728 gunicorn.conf.py \u91cc&#xff1a;<\/p>\n<p>access_log_format &#061; &#039;%(t)s %(h)s &#034;%(r)s&#034; %(s)s %(b)s&#039;<\/p>\n<\/li>\n<li>\n<p>\u9ad8\u5e76\u53d1\u4e0b\u7aef\u53e3\u8017\u5c3d<\/p>\n<p>\u6253\u5f00 net.ipv4.tcp_tw_reuse &#061; 1&#xff0c;\u6216\u8ba9\u5ba2\u6237\u7aef\u8d70\u8fde\u63a5\u6c60<\/p>\n<\/li>\n<hr \/>\n<h2>\u5c0f\u7ed3 &amp; \u5ef6\u4f38\u9605\u8bfb<\/h2>\n<ul>\n<li>Gunicorn \u4e0d\u662f\u94f6\u5f39&#xff0c;\u800c\u662f WSGI \u7684\u6700\u540e\u4e00\u516c\u91cc\u3002\u5408\u7406\u9009\u62e9\u5e76\u53d1\u6a21\u578b &#043; \u53c2\u6570\u8c03\u4f18&#xff0c;\u624d\u80fd\u53d1\u6325\u591a\u6838\u5a01\u529b\u3002<\/li>\n<li>\u6e90\u7801\u4ec5 7k \u884c&#xff0c;\u6ce8\u91ca\u53cb\u597d&#xff0c;\u63a8\u8350\u9605\u8bfb gunicorn\/workers\/ \u4e0b\u5404\u79cd\u6a21\u578b\u7684\u5b9e\u73b0\u5dee\u5f02\u3002<\/li>\n<li>\u8fdb\u4e00\u6b65\u5b66\u4e60 ASGI \u76f8\u5173\u77e5\u8bc6<\/li>\n<\/ul>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u76ee\u5f55\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;\u73af\u5883\u8bf4\u660e1\u3001Gunicorn \u662f\u4ec0\u4e48&#xff1f;2\u3001\u5b89\u88c5 &amp; \u6700\u5c0f\u53ef\u8fd0\u884c\u793a\u4f8b3\u3001\u6838\u5fc3\u6982\u5ff5\u901f\u89c84\u3001\u5e76\u53d1\u6a21\u578b\u600e\u4e48\u9009&#xff1f;5\u3001\u751f\u4ea7\u7ea7\u914d\u7f6e\u6e05\u5355&#xff08;\u53ef\u76f4\u63a5\u6284&#xff09;6\u3001\u4f18\u96c5\u91cd\u542f &amp; \u96f6\u5b95\u673a\u53d1\u5e037\u3001\u5bb9\u5668\u5316\u6700\u4f73\u5b9e\u8df58\u3001\u5e38\u89c1\u5751 8 \u5219\u5c0f\u7ed3 &amp; \u5ef6\u4f38\u9605\u8bfb\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;<br \/>\n\u672c\u5730\u5f00\u53d1\u65f6&#xff0c;\u4e00\u6761 f<\/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":[6431,6433,6432,153,1032,81,2500],"topic":[],"class_list":["post-60932","post","type-post","status-publish","format-standard","hentry","category-server","tag-wsgi","tag-6433","tag-6432","tag-flask","tag-gunicorn","tag-python","tag-2500"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \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\/60932.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u76ee\u5f55\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;\u73af\u5883\u8bf4\u660e1\u3001Gunicorn \u662f\u4ec0\u4e48&#xff1f;2\u3001\u5b89\u88c5 &amp; \u6700\u5c0f\u53ef\u8fd0\u884c\u793a\u4f8b3\u3001\u6838\u5fc3\u6982\u5ff5\u901f\u89c84\u3001\u5e76\u53d1\u6a21\u578b\u600e\u4e48\u9009&#xff1f;5\u3001\u751f\u4ea7\u7ea7\u914d\u7f6e\u6e05\u5355&#xff08;\u53ef\u76f4\u63a5\u6284&#xff09;6\u3001\u4f18\u96c5\u91cd\u542f &amp; \u96f6\u5b95\u673a\u53d1\u5e037\u3001\u5bb9\u5668\u5316\u6700\u4f73\u5b9e\u8df58\u3001\u5e38\u89c1\u5751 8 \u5219\u5c0f\u7ed3 &amp; \u5ef6\u4f38\u9605\u8bfb\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f; \u672c\u5730\u5f00\u53d1\u65f6&#xff0c;\u4e00\u6761 f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/60932.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-16T08:25:43+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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60932.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/60932.html\",\"name\":\"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-16T08:25:43+00:00\",\"dateModified\":\"2026-01-16T08:25:43+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/60932.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/60932.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/60932.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668\"}]},{\"@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":"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \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\/60932.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u76ee\u5f55\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f;\u73af\u5883\u8bf4\u660e1\u3001Gunicorn \u662f\u4ec0\u4e48&#xff1f;2\u3001\u5b89\u88c5 &amp; \u6700\u5c0f\u53ef\u8fd0\u884c\u793a\u4f8b3\u3001\u6838\u5fc3\u6982\u5ff5\u901f\u89c84\u3001\u5e76\u53d1\u6a21\u578b\u600e\u4e48\u9009&#xff1f;5\u3001\u751f\u4ea7\u7ea7\u914d\u7f6e\u6e05\u5355&#xff08;\u53ef\u76f4\u63a5\u6284&#xff09;6\u3001\u4f18\u96c5\u91cd\u542f &amp; \u96f6\u5b95\u673a\u53d1\u5e037\u3001\u5bb9\u5668\u5316\u6700\u4f73\u5b9e\u8df58\u3001\u5e38\u89c1\u5751 8 \u5219\u5c0f\u7ed3 &amp; \u5ef6\u4f38\u9605\u8bfb\u524d\u8a00&#xff1a;\u4e3a\u4ec0\u4e48\u201c\u88f8\u8dd1\u201d Flask\/Django \u4e0d\u591f&#xff1f; \u672c\u5730\u5f00\u53d1\u65f6&#xff0c;\u4e00\u6761 f","og_url":"https:\/\/www.wsisp.com\/helps\/60932.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-16T08:25:43+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/60932.html","url":"https:\/\/www.wsisp.com\/helps\/60932.html","name":"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-16T08:25:43+00:00","dateModified":"2026-01-16T08:25:43+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/60932.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/60932.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/60932.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece 0 \u5230 1 \u638c\u63e1 Gunicorn\uff1a\u8ba9 Python Web \u5e94\u7528\u98de\u8d77\u6765\u7684 WSGI \u670d\u52a1\u5668"}]},{"@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\/60932","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=60932"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/60932\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=60932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=60932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=60932"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=60932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}