{"id":991844,"date":"2024-12-27T08:32:27","date_gmt":"2024-12-27T00:32:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/991844.html"},"modified":"2024-12-27T08:32:29","modified_gmt":"2024-12-27T00:32:29","slug":"nginx%e4%b8%8b%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%aepython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/991844.html","title":{"rendered":"nginx\u4e0b\u5982\u4f55\u914d\u7f6epython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065943\/bc7fab19-4e89-480a-8231-c71712efbe5a.webp\" alt=\"nginx\u4e0b\u5982\u4f55\u914d\u7f6epython\" \/><\/p>\n<p><p> \u5728Nginx\u4e0b\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u6d89\u53ca\u4f7f\u7528WSGI\uff08Web Server Gateway Interface\uff09\u670d\u52a1\u5668\uff0c\u5982Gunicorn\u6216uWSGI\uff0c\u4f5c\u4e3a\u4e2d\u95f4\u5c42\u5c06Nginx\u4e0ePython\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u3002<strong>\u914d\u7f6eNginx\u548cPython\u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5305\u62ec\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u3001\u914d\u7f6eNginx\u3001\u542f\u52a8WSGI\u670d\u52a1\u5668\u3001\u6d4b\u8bd5\u548c\u4f18\u5316\u6027\u80fd<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684WSGI\u670d\u52a1\u5668\u548c\u914d\u7f6eNginx\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4eec\u76f4\u63a5\u5f71\u54cd\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5728Nginx\u4e0b\u914d\u7f6ePython\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001Nginx\u548cPython\u73af\u5883\u51c6\u5907<\/p>\n<\/p>\n<p><p>\u5728\u914d\u7f6e\u4e4b\u524d\uff0c\u786e\u4fdd\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86Nginx\u548cPython\u3002\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u5728Ubuntu\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt update<\/p>\n<p>sudo apt install nginx<\/p>\n<p>sudo apt install python3 python3-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6bd5\u540e\uff0c\u786e\u8ba4\u5b89\u88c5\u6b63\u786e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">nginx -v<\/p>\n<p>python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5WSGI\u670d\u52a1\u5668<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u9009\u62e9\u9002\u5408\u7684WSGI\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u5e38\u7528\u7684WSGI\u670d\u52a1\u5668\u5305\u62ecGunicorn\u548cuWSGI\u3002Gunicorn\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u90e8\u7f72\uff0c\u800cuWSGI\u63d0\u4f9b\u66f4\u591a\u914d\u7f6e\u9009\u9879\uff0c\u9002\u5408\u9700\u8981\u7cbe\u7ec6\u63a7\u5236\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5WSGI\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7pip\u5b89\u88c5WSGI\u670d\u52a1\u5668\u3002\u4f8b\u5982\uff0c\u5b89\u88c5Gunicorn\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gunicorn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5uWSGI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install uwsgi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaPython\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\u4f5c\u4e3a\u4f8b\u5b50\u3002\u9996\u5148\uff0c\u5b89\u88c5Flask\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>app.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def hello_world():<\/p>\n<p>    return &#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5Python\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7Flask\u7684\u5185\u7f6e\u670d\u52a1\u5668\u6d4b\u8bd5\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export FLASK_APP=app.py<\/p>\n<p>flask run<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6d4f\u89c8\u5668\u6253\u5f00<code>http:\/\/127.0.0.1:5000<\/code>\uff0c\u5e94\u770b\u5230\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u914d\u7f6eWSGI\u670d\u52a1\u5668<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u542f\u52a8WSGI\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Gunicorn\u542f\u52a8Flask\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">gunicorn --bind 0.0.0.0:8000 app:app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4f7f\u7528uWSGI\uff0c\u521b\u5efa\u4e00\u4e2a<code>uwsgi.ini<\/code>\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[uwsgi]<\/p>\n<p>module = app:app<\/p>\n<p>master = true<\/p>\n<p>processes = 5<\/p>\n<p>socket = 0.0.0.0:8000<\/p>\n<p>vacuum = true<\/p>\n<p>die-on-term = true<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u542f\u52a8uWSGI\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">uwsgi --ini uwsgi.ini<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5WSGI\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee<code>http:\/\/127.0.0.1:8000<\/code>\uff0c\u786e\u8ba4\u5e94\u7528\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u914d\u7f6eNginx<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaNginx\u914d\u7f6e\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728<code>\/etc\/nginx\/sites-av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable\/<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5982<code>myapp<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-nginx\">server {<\/p>\n<p>    listen 80;<\/p>\n<p>    server_name your_domain_or_IP;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass http:\/\/127.0.0.1:8000;<\/p>\n<p>        proxy_set_header Host $host;<\/p>\n<p>        proxy_set_header X-Real-IP $remote_addr;<\/p>\n<p>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n<p>        proxy_set_header X-Forwarded-Proto $scheme;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u7528Nginx\u914d\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b26\u53f7\u94fe\u63a5\uff0c\u5c06\u914d\u7f6e\u6587\u4ef6\u94fe\u63a5\u5230<code>sites-enabled<\/code>\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u68c0\u67e5Nginx\u914d\u7f6e\u662f\u5426\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo nginx -t<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u91cd\u65b0\u52a0\u8f7dNginx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl reload nginx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6d4b\u8bd5Nginx\u4e0ePython\u5e94\u7528\u8fde\u63a5<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee<code>http:\/\/your_domain_or_IP<\/code>\uff0c\u786e\u8ba4\u5e94\u7528\u6b63\u5e38\u663e\u793a\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f18\u5316\u4e0e\u5b89\u5168<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u914d\u7f6eNginx\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u6839\u636e\u9700\u8981\u8c03\u6574Nginx\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u5982worker_processes\u3001worker_connections\u7b49\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528SSL\/TLS<\/strong><\/p>\n<\/p>\n<p><p>\u8003\u8651\u4f7f\u7528Let\u2019s Encrypt\u6216\u5176\u4ed6CA\u63d0\u4f9b\u7684SSL\u8bc1\u4e66\u6765\u52a0\u5bc6\u6d41\u91cf\uff0c\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u5b89\u5168\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u9632\u706b\u5899<\/strong><\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u670d\u52a1\u5668\u7684\u9632\u706b\u5899\u914d\u7f6e\u6b63\u786e\uff0c\u4ec5\u5f00\u653e\u5fc5\u8981\u7684\u7aef\u53e3\uff08\u598280\u548c443\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Nginx\u4e0b\u6210\u529f\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u5176\u80fd\u591f\u7a33\u5b9a\u3001\u9ad8\u6548\u5730\u8fd0\u884c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u670d\u52a1\u5668\u73af\u5883\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u8c03\u6574\u914d\u7f6e\u548c\u4f18\u5316\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Nginx\u4e2d\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Nginx\u4e2d\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u9700\u8981\u4f7f\u7528WSGI\u670d\u52a1\u5668\uff0c\u5982Gunicorn\u6216uWSGI\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u5e94\u7528\u7a0b\u5e8f\u5df2\u7ecf\u51c6\u5907\u597d\uff0c\u5e76\u80fd\u591f\u5728WSGI\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\u3002\u63a5\u4e0b\u6765\uff0c\u5b89\u88c5Nginx\u548cWSGI\u670d\u52a1\u5668\u3002\u914d\u7f6eNginx\u4ee5\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230WSGI\u670d\u52a1\u5668\u65f6\uff0c\u9700\u8981\u8bbe\u7f6e\u53cd\u5411\u4ee3\u7406\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u7f16\u8f91Nginx\u914d\u7f6e\u6587\u4ef6\uff0c\u5b9a\u4e49\u670d\u52a1\u5668\u5757\uff0c\u5e76\u6307\u5b9aWSGI\u670d\u52a1\u5668\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002<\/p>\n<p><strong>\u5728Nginx\u4e2d\u914d\u7f6ePython\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u5b89\u5168\u6027\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u914d\u7f6eNginx\u4ee5\u8fd0\u884cPython\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u786e\u4fdd\u91c7\u53d6\u9002\u5f53\u7684\u5b89\u5168\u63aa\u65bd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u4f7f\u7528HTTPS\u6765\u52a0\u5bc6\u4f20\u8f93\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6SSL\u8bc1\u4e66\u6765\u5b9e\u73b0\u3002\u540c\u65f6\uff0c\u9650\u5236\u5bf9\u654f\u611f\u6587\u4ef6\u548c\u76ee\u5f55\u7684\u8bbf\u95ee\uff0c\u786e\u4fdd\u53ea\u5141\u8bb8\u5fc5\u8981\u7684\u8bf7\u6c42\u901a\u8fc7\u3002\u5b9a\u671f\u66f4\u65b0Nginx\u548cPython\u5e93\uff0c\u4fee\u8865\u6f5c\u5728\u7684\u5b89\u5168\u6f0f\u6d1e\uff0c\u80fd\u591f\u6709\u6548\u5730\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u9047\u5230Nginx\u4e0ePython\u5e94\u7528\u7a0b\u5e8f\u4e4b\u95f4\u7684\u8fde\u63a5\u95ee\u9898\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u8fde\u63a5\u95ee\u9898\u53ef\u80fd\u7531\u591a\u79cd\u56e0\u7d20\u5f15\u8d77\uff0c\u5305\u62ec\u9632\u706b\u5899\u8bbe\u7f6e\u3001Nginx\u914d\u7f6e\u9519\u8bef\u6216WSGI\u670d\u52a1\u5668\u672a\u6b63\u786e\u542f\u52a8\u3002\u68c0\u67e5Nginx\u7684\u9519\u8bef\u65e5\u5fd7\u548c\u8bbf\u95ee\u65e5\u5fd7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u95ee\u9898\u7684\u6839\u6e90\u3002\u786e\u4fddWSGI\u670d\u52a1\u5668\u6b63\u5728\u8fd0\u884c\u5e76\u76d1\u542c\u6b63\u786e\u7684\u7aef\u53e3\uff0c\u540c\u65f6\u9a8c\u8bc1Nginx\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684proxy_pass\u6307\u4ee4\u662f\u5426\u6307\u5411\u4e86\u6b63\u786e\u7684\u5730\u5740\u548c\u7aef\u53e3\u3002\u5982\u679c\u95ee\u9898\u4f9d\u7136\u5b58\u5728\uff0c\u8003\u8651\u589e\u52a0Nginx\u7684\u8fde\u63a5\u548c\u8d85\u65f6\u8bbe\u7f6e\uff0c\u4ee5\u63d0\u9ad8\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Nginx\u4e0b\u914d\u7f6ePython\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u6d89\u53ca\u4f7f\u7528WSGI\uff08Web Server Gateway Interf [&hellip;]","protected":false},"author":3,"featured_media":991858,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991844"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=991844"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991844\/revisions"}],"predecessor-version":[{"id":991860,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991844\/revisions\/991860"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/991858"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=991844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=991844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=991844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}