{"id":657,"date":"2018-09-22T12:59:57","date_gmt":"2018-09-22T04:59:57","guid":{"rendered":"http:\/\/cmesoft.com\/?p=657"},"modified":"2018-09-22T13:02:05","modified_gmt":"2018-09-22T05:02:05","slug":"nginx-uwsgi-%e9%85%8d%e7%bd%ae-flask-%e5%bc%80%e5%8f%91%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"http:\/\/cmesoft.com\/?p=657","title":{"rendered":"nginx + uwsgi \u914d\u7f6e flask \u5f00\u53d1\u6846\u67b6"},"content":{"rendered":"<h2>\u4ecb\u7ecd<\/h2>\n<p>\u7c7b\u4f3c\u4e8e\uff0cnginx \u7ed3\u5408 php-fpm \u53ef\u4ee5\u652f\u6301\u8df3\u8f6c\u5230 php \u7a0b\u5e8f\u4e00\u6837\uff0cnginx \u540e\u7aef\u4e5f\u53ef\u4ee5\u6302 uwsgi \u6765\u7528 python \u7a0b\u5e8f\u4f5c\u4e3aserver\u7aef\u3002<\/p>\n<h2>\u53c2\u8003\u6587\u6863<\/h2>\n<p>\u300a\u4e00\u4e2aUWSGI\u7684\u4f8b\u5b50\u300b:https:\/\/blog.csdn.net\/crazyhacking\/article\/details\/18617873<br \/>\n\u4e3b\u8981\u662f\u5173\u4e8eflask + python \u7684\u4ee3\u7801\u5f00\u53d1\u4f8b\u5b50\u3002<br \/>\n\u300auWSGI+Nginx+Flask\u5728Linux\u4e0b\u7684\u90e8\u7f72\u300bhttps:\/\/www.cnblogs.com\/zhangjpn\/p\/6876412.html?utm_source=itdadao&amp;utm_medium=referral<br \/>\n\u4e3b\u8981\u662f\u5173\u4e8e\u90e8\u7f72<\/p>\n<h2>\u8f6f\u4ef6\u5b89\u88c5<\/h2>\n<pre><code>pip install uwsgi\nyum install python-flask\nyum install nginx\n<\/code><\/pre>\n<h2>\u914d\u7f6e<\/h2>\n<h3>nginx \u914d\u7f6e<\/h3>\n<h4>\u4f7f\u7528 http\/http-socket \u534f\u8bae<\/h4>\n<pre><code>server {\n            listen       80 default_server;\n            server_name  _;\n\n        location ~ \/pptserver { \n            proxy_pass http:\/\/127.0.0.1:8888;\n            }\n\n    error_page 404 \/404.html;\n        location = \/40x.html {\n    }\n\n    error_page 500 502 503 504 \/50x.html;\n        location = \/50x.html {\n    }\n\n}\n<\/code><\/pre>\n<h3>\u4f7f\u7528unix socket \u534f\u8bae(\u672c\u5730\u6587\u4ef6)<\/h3>\n<pre><code>server {\n            listen       80 default_server;\n            server_name  _;\n\n        location ~ \/pptserver { \n        include uwsgi_params;\n        uwsgi_pass unix:\/\/tmp\/ppt.sock;\n            }\n\n    error_page 404 \/404.html;\n        location = \/40x.html {\n    }\n\n    error_page 500 502 503 504 \/50x.html;\n        location = \/50x.html {\n    }\n\n}\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u8981\u786e\u4fdd sock \u6587\u4ef6\u53ef\u4ee5\u88ab\u8bbf\u95ee\uff0c\u4e00\u822c\u8bbe\u7f6e\u4e3a 777 \u5373\u53ef\u3002<\/p>\n<h3>uwsgi \u914d\u7f6e<\/h3>\n<pre><code>cat \/data\/install\/conf\/uwsgi.conf \n[uwsgi]\n## For directlly http access\nhttp-socket=127.0.0.1:8888\n\n### For nginx proxy\n#socket=\/tmp\/ppt.sock\n\nwsgi-file=\/data\/install\/bin\/pptserver.py\n#plugins = python\ncallable = app\n#chdir = \/data\/install\/bin\ntouch-reload=\/data\/install\/bin\/\nprocesses = 2\nthreads = 2\nstats = 127.0.0.1:9191\npost-buffering = 8192\nbuffer-size = 65535\nsocket-timeout = 10\nuid = apache\ngid = apache\nmaster = true\n#protocol = uwsgi\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4f7f\u7528 http\u6216\u8005socket\u534f\u8bae\u65f6\uff0c\u53ea\u9700\u8981\u5207\u6362\u4e0b(socket\u6216\u8005http)\u5373\u53ef\u3002<\/p>\n<h2>\u8fd0\u884c\u7a0b\u5e8f<\/h2>\n<h3>\u542f\u52a8 nginx<\/h3>\n<pre><code>service nginx restart\n<\/code><\/pre>\n<h3>\u542f\u52a8 uwsgi<\/h3>\n<pre><code>\/usr\/bin\/uwsgi --ini \/data\/install\/conf\/uwsgi.conf\n<\/code><\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<pre><code># curl \"localhost\/pptserver?uid=3434&amp;name=get_jingle\" -d \"name=post_jigang\"\n{\"get_name\":\"get_jingle\",\"post_name\":\"post_jigang\"}\n<\/code><\/pre>\n<h2>\u9644\u4ef6<\/h2>\n<h3>pptserver.py<\/h3>\n<pre><code># cat \/data\/install\/bin\/pptserver.py \nfrom flask import Flask,render_template, jsonify\nfrom flask import request\n\napp = Flask(__name__)\n\n@app.route('\/pptserver', methods=['GET', 'POST'])\n#def index():\ndef index():\n    requester = request.remote_addr\n        #logging.info('Data request from: %s' % requester)\n        url = request.url\n        uid = url.split('uid=')[-1]\n    get_name=request.args.get(\"name\")\n    post_name=request.form.get(\"name\")\n    #return \"Test message, your input uid=%s, method=%s, get_name=%s, post_name=%s  from %s\\n\" % (uid, request.method, get_name, post_name, requester)\n    return jsonify(get_name=get_name,post_name=post_name)\nif __name__ == '__main__':\n    app.run()\n<\/code><\/pre>\n<h3>git \u5de5\u7a0b<\/h3>\n<p>\u53c2\u8003\u6211\u7684 git \u793a\u4f8b\u5de5\u7a0b<\/p>\n<p>https:\/\/github.com\/duanjigang1983\/study<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ecb\u7ecd \u7c7b\u4f3c\u4e8e\uff0cnginx \u7ed3\u5408 php-fpm \u53ef\u4ee5\u652f\u6301\u8df3\u8f6c\u5230 php \u7a0b\u5e8f\u4e00\u6837\uff0cnginx \u540e\u7aef\u4e5f\u53ef\u4ee5\u6302 u&hellip;&nbsp;<a href=\"http:\/\/cmesoft.com\/?p=657\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">nginx + uwsgi \u914d\u7f6e flask \u5f00\u53d1\u6846\u67b6<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[41],"tags":[],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cmesoft.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=657"}],"version-history":[{"count":2,"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/cmesoft.com\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions\/659"}],"wp:attachment":[{"href":"http:\/\/cmesoft.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cmesoft.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cmesoft.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}