{"id":386,"date":"2023-09-16T10:57:09","date_gmt":"2023-09-16T02:57:09","guid":{"rendered":""},"modified":"2023-09-16T10:57:09","modified_gmt":"2023-09-16T02:57:09","slug":"web\u9879\u76ee\u5206\u5e03\u5f0f\u90e8\u7f72\u6574\u4f53\u6846\u67b6\u65b9\u6848","status":"publish","type":"post","link":"https:\/\/mushiming.com\/386.html","title":{"rendered":"web\u9879\u76ee\u5206\u5e03\u5f0f\u90e8\u7f72\u6574\u4f53\u6846\u67b6\u65b9\u6848"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u9879\u76ee\u5206\u5e03\u5f0f\u90e8\u7f72\u6574\u4f53\u6846\u67b6\u65b9\u6848<\/p>\n

\u4ee5\u4e0b\u6709\u4e24\u79cd\u65b9\u6848\u548c\u4e00\u4e2a\u5907\u9009\u65b9\u6848:\n\t\u65b9\u68481 :\tclient-->Nginx(openresty)-->gunicorn(\u901a\u8fc7wsgi\u542f\u52a8\u6258\u7ba1)--?django(web\u5e94\u7528\u670d\u52a1)\n\t\u65b9\u68482 :\tNginx(openresty,upstream)-->supervisor(monitor)-->gunicorn(\u540cuwsgi)-->django(web\u5e94\u7528\u670d\u52a1)\n\t\n\t\u5907\u9009\u65b9\u6848:\t\u6309\u7167\u539f\u6765\u7684http\u542f\u52a8\u65b9\u5f0f\n\t\tnohup python manage.py runserver 0.0.0.0:9001\n\t&\tNginx(openresty,upstream)-->supervisor(monitor)HTTP--?django(HTTP)\n\n\n\u4f7f\u7528\u5982\u4e0b\u6846\u67b6\u548c\u5de5\u5177\n\t1.\u53cd\u5411\u4ee3\u7406\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u5668: Nginx(openresty)\n\t2.gunicorn(\u4ee3\u66ffuwsgi)\n\t3.django\n\t4.supervisor(\u76d1\u63a7\u5e76\u62c9\u8d77\u5931\u8d25\u670d\u52a1)\n<\/code><\/pre>\n

gunicorn\u5b89\u88c5<\/p>\n

gunicorn\u7b80\u4ecb:\n\tgunicorn\u662f\u4e00\u4e2apython wsgi http server,\u53ea\u652f\u6301Unix\u7cfb\u7edf\u4e0a\u8fd0\u884c,\u6765\u6e90\u4e8eruby\u7684unicorn.Gunicorn\u4f7f\u7528prefork master-worker\u6a21\u578b,\u80fd\u591f\u4e0e\u5404\u79cd\u7684web\u6846\u67b6\u8fdb\u884c\u534f\u4f5c\n\t\n1.\tpip install gunicorn -i https:\/\/pypi.douban.com\/simple\n2.\t\u542f\u52a8gunicorn\u547d\u4ee4:\n\t\u5177\u4f53chdir\u662f\u9879\u76ee\u7684basedir\u76ee\u5f55\n\tnohup gunicorn --chdir \/home\/zhouguangyou\/artproject\/ artproject.wsgi:application  --bind 0.0.0.0:9000  --workers=2 &\n<\/code><\/pre>\n

\u914d\u7f6eNginx\u670d\u52a1\u5668<\/p>\n

\u5728nginx\u670d\u52a1\u5668\u914d\u7f6e\u4e2d\u4fee\u6539\u914d\u7f6e\u76ee\u5f55,\u5c06nginx.conf\u4fee\u6539\u4e3a:<\/p>\n

# user  nobody;\n# user nginx;\nworker_processes  2;        #cat \/proc\/cpuinfo\n# worker_cpu_affinity       0001 0010 0100 1000;\n\nerror_log  logs\/error.log  notice;\n# error_log  logs\/error.log  debug;\n\npid  logs\/nginx.pid;\n\nevents\n{\n    use   epoll;\n    multi_accept  on;\n    accept_mutex_delay  50ms;\n    worker_connections  65535;    #\u6bcf\u4e2aworker\u652f\u6301\u6700\u5927\u7f51\u7edc\u8fde\u63a5\u6570\n}    \n\nhttp\n{\n    include  mime.types;\n    # default_type  application\/octet-stream;\n     default_type   text\/html;\n\n# log_format  main  '$remote_addr - $remote_user [$time_local] \"$request\" '\n#                  '$status $body_bytes_sent \"$http_referer\" '\n#                  '\"$http_user_agent\" \"$http_x_forwarded_for\"'\n#                  '\"$upstream_addr\" \"$upstream_status\" \"$upstream_response_time\" '\n#                  '$request_time -- $http_cookie -- $cookie_pin';\n\n\n# access_log  logs\/access.log  main;\n\nsendfile  on;\ntcp_nopush  on;\n\nkeepalive_timeout  0;\n\ngzip  on;\ngzip_min_length  1k;\ngzip_buffers 48k;\ngzip_http_version  1.1;\ngzip_types  text\/plain application\/x-javascript text\/css  text\/shtml application\/xml;\n\nproxy_intercept_errors  on;\ncharset  utf-8;\n\n######################\ninclude  conf.d\/*.conf;\n######################\n\n}\n<\/code><\/pre>\n

\u521b\u5efa\u76ee\u5f55conf.d, \u5e76\u6dfb\u52a0\u5982\u4e0b\u4fe1\u606f\u5230project.conf\uff1a<\/p>\n

proxy_next_upstream  error;\n\nserver\n{\n    listen 8000;       #nginx\u63d0\u4f9b\u5bf9\u5916\u7684\u7aef\u53e3\u662f8000\uff0c \u901a\u8fc7\u8def\u7531 \/ \u8f6c\u53d1\u5230127.0.0.1:9000\/art\/index \u670d\u52a1\uff0c\u800c9000\u7aef\u53e3\u662f\u4e0a\u8ff0gunicorn\u63d0\u4f9b\u7684\u7aef\u53e3\n    server_name   localhost;\n\n    client_max_body_size  50M;\n\n    #\u914d\u7f6e\u9879\u76ee\u9759\u6001\u8d44\u6e90\u76ee\u5f55\n    location \/static\/ {\n        \n        root \/home\/zhouguangyou\/artproject\/art;\n\t}\n  \n\n    location \/ {\n        proxy_pass   http:\/\/127.0.0.1:9000;\n        proxy_set_header  Host $host;\n        proxy_set_header  X-Real-IP  $remote_addr;\n        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;\n     }\n\n    # error_page  404              \/404.html;\n\n    # redirect server error pages to the static page \/50x.html\n    #\n     error_page  500  502  503  504 \/50x.html;\n     location = \/50x.html {\n         root  html;\n      }\n\n}\n<\/code><\/pre>\n

\u6700\u540e,\u91cd\u542fnginx\u4ee5\u4f7f\u670d\u52a1\u751f\u6548<\/p>\n

\u4ee5\u4e0a\u914d\u7f6e\u57fa\u672c\u5b8c\u6210\u4e86nginx+gunicorn+django \u7684\u914d\u7f6e<\/p>\n

\u63a5\u4e0b\u6765:<\/p>\n

\u5b89\u88c5supervisor\u670d\u52a1<\/p>\n

supervisor\u7b80\u4ecb<\/p>\n

supervisor\u7ba1\u7406\u8fdb\u7a0b,\u662f\u901a\u8fc7fork\/exec\u7684\u65b9\u5f0f\u5c06\u8fd9\u4e9b\u88ab\u7ba1\u7406\u7684\u8fdb\u7a0b\u5f53\u505asupervisor\u7684\u5b50\u8fdb\u7a0b\u6765\u8fdb\u884c\u542f\u52a8,\u56e0\u6b64,\u6211\u4eec\u53ea\u9700\u8981\u5c06\u7ba1\u7406\u8fdb\u7a0b\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u8def\u5f84\u6dfb\u52a0\u5230supervisor\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u5c31\u597d\u4e86.\u6b64\u65f6,\u88ab\u7ba1\u7406\u8fdb\u7a0b\u5c31\u88ab\u89c6\u4e3asupervisor\u7684\u5b50\u8fdb\u7a0b,\u82e5\u8be5\u5b50\u8fdb\u7a0b(\u88ab\u7ba1\u7406\u8fdb\u7a0b)\u53d1\u751f\u5f02\u5e38,\u7236\u8fdb\u7a0b(supervisor)\u53ef\u4ee5\u53ca\u65f6\u83b7\u53d6\u5230\u5b50\u8fdb\u7a0b\u7684\u5f02\u5e38\u4fe1\u606f,\u901a\u8fc7\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u4e86\u5b9e\u73b0\u5bf9\u5b50\u8fdb\u7a0b\u8fdb\u884c\u7ba1\u7406,\u6bd4\u5982\u8bbe\u7f6eautostart=true,\u53ef\u4ee5\u5b9e\u73b0\u5b50\u8fdb\u7a0b\u7684\u5f02\u5e38\u4e2d\u65ad-->\u81ea\u52a8\u91cd\u542f\n<\/code><\/pre>\n

supervisor\u5b89\u88c5<\/p>\n

linux(Ubuntu) \u4e0b\u6267\u884c:sudo apt install supervisor\n<\/code><\/pre>\n

supervisor\u914d\u7f6e<\/p>\n

\u5c06supervisor\u8bbe\u7f6e\u4e3a\u7ba1\u7406\u542f\u52a8\u76d1\u63a7 gunicorn<\/p>\n

\u5f80supervisor.conf \u4e2d\u6dfb\u52a0\u5982\u4e0b\u4fe1\u606f<\/p>\n

[group:artprojects]\nprograms=art-1, art-2  \n\n\n[program:art-1]\ncommand=gunicorn --chdir \/home\/zhouguangyou\/artproject\/ artproject.wsgi:application  --bind 0.0.0.0:9001  --workers=2\ndirectory=\/home\/zhouguangyou\/artproject\nuser=zhouguangyou\nautorestart=true\nredirect_stderr=true\nstdout_logfile=log\/art1.log\nloglevel=info\nstopsignal=INT\n\n\n[program:art-2]\ncommand=gunicorn --chdir \/home\/zhouguangyou\/artproject\/ artproject.wsgi:application  --bind 0.0.0.0:9002 --workers=2\ndirectory=\/home\/zhouguangyou\/artproject\nuser=zhouguangyou\nautorestart=true\nredirect_stderr=true\nstdout_logfile=log\/art2.log\nloglevel=info\nstopsignal=INT\n\n[supervisord]\nnodaemon=false\nlogfile=log\/supervisord.log\npidfile=log\/supervisord.pid\nloglevel=info\n<\/code><\/pre>\n

\u6b64\u5916,\u521b\u5efalog\u6587\u4ef6\u5939\u5b58\u653e\u65e5\u5fd7\u6587\u4ef6<\/p>\n

\u5728nginx\u914d\u7f6e\u6587\u4ef6project.conf\u4e2d\u52a0\u5165\u5982\u4e0b\u4fe1\u606f<\/p>\n

upstream artprojects{\n    #ip_hash;  or  \u8f6e\u8be2\uff08\u9ed8\u8ba4\uff09 or url_hash\n    server 127.0.0.1:9001;\n    server 127.0.0.1:9002;\n }\n\n location \/ {\n       #(2) method2: use proxy_pass upstream to the supervisor who manage the gunicorn\n      \n      proxy_pass  http:\/\/artprojects;\n  }\n<\/code><\/pre>\n

\u6dfb\u52a0\u597d\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u540e<\/p>\n

\uff081\uff09\u542f\u52a8nginx<\/p>\n

\uff082\uff09\u901a\u8fc7supervisor\u542f\u52a8gunicorn<\/p>\n

supervisord -c supervisor.conf<\/p>\n

\u9875\u9762\u901a\u8fc7http:\/\/127.0.0.1:8000\/*\/\u8fdb\u884c\u9875\u9762\u8bbf\u95ee\uff0c\u89c2\u770b\u6548\u679c\u3002<\/p>\n

\u7efc\u5408\u4e0a\u8ff0\uff0cnginx\u505a\u53cd\u5411\u4ee3\u7406\u548c\u8d1f\u8f7d\u5747\u8861\uff0c\u5c06\u8bf7\u6c42upstream\u8f6c\u53d1\u7ed9\u4e00\u4e2aSupervisor\u76d1\u7ba1\u7684Gunicorn\u8fdb\u7a0b\uff0c\u800cGunicorn\u8fdb\u7a0b\u62d6\u7ba1\u4e86Django\u5de5\u7a0b\u4ee3\u7801\u3002\n<\/code><\/pre>\n

\u81f3\u6b64\uff0cNginx + Gunicorn + Supervisor + Django \u7ebf\u4e0a\u73af\u5883\u90e8\u7f72\u90fd\u5df2\u7ecf\u5b8c\u6210\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"web\u9879\u76ee\u5206\u5e03\u5f0f\u90e8\u7f72\u6574\u4f53\u6846\u67b6\u65b9\u6848\u9879\u76ee\u5206\u5e03\u5f0f\u90e8\u7f72\u6574\u4f53\u6846\u67b6\u65b9\u6848\u4ee5\u4e0b\u6709\u4e24\u79cd\u65b9\u6848\u548c\u4e00\u4e2a\u5907\u9009\u65b9\u6848:\t\u65b9\u68481:\tclient>Nginx(openresty...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/386"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}