【場景篇】
為了節省端口的占用,將N個flask應用服務——每個對應一個文件(web.py、django也一樣)合並為一個端口服務來啟用
【尋思篇】
通常的做法:每個文件配置一個xml 或者 ini文件,然后依次啟動uwsgi(uwsgi -x {xml文件名}),形如:
<uwsgi> <wsgi-file>/home/yxgly/code/doraemon/DsBag/GET_DATA/Get_Data_Api.py</wsgi-file> <callable>app</callable> <socket>/tmp/uwsgi1.sock</socket> <stats>/tmp/stats.socket1</stats> <master/> <enable-threads>true</enable-threads> <workers>1</workers> <threads>1</threads> <processes>1</processes> </uwsgi>
uwsgi -x {xml} -d /tmp/uwsgi.log
但這樣會導致啟動的uwsgi太多,不便於管理。
稍高級一點的用法,可以使用“皇帝”(emperor),具體用法:將所有的xml統一放到一個目錄里(例如:放到這個目錄下 mysgi_conf),進行一並啟動:
uwsgi --emperor mysgi_conf/ --logto /tmp/uwsgi.log
這樣啟動了之后,每個文件都有有個與之對應的socket文件
然后在nginx上配置多條location來做好走向,形如:
server {
listen 8888;
server_name _;
location /get_data {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi1.sock;
}
location /get_info {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi2.sock;
}
location /get_class {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi3.sock;
}
……………………………………
access_log logs/access_8888.log main;
}
但這種會出現種種錯誤,若是只配置“location /" 就會正常,但我的“需求”是在一個端口下(8888)做好所有的事情……
找谷哥、度娘……找到了一篇
https://jawher.me/2012/03/16/multiple-python-apps-with-nginx-uwsgi-emperor-upstart/
坑爹的是按照他指導的樣子去做,還是不奏效
與此同時,更讓我困惑的是文中所提到的“uwsgi_modifier1”的用法,我又沒有完全“領會”明白,最后搜到了這樣一段解釋,就算稍微明白了一些。
uwsgi_modifier1 value 30 means:
Standard WSGI request followed by the HTTP request body. The PATH_INFO is automatically modified, removing the SCRIPT_NAME from it.
【解決篇】
在網上搜了很多關於這個問題的解決思路,但是沒有找到一個“合適”的答案(百變不離其中的都是抄來抄去,沒有任何一個奏效的)
於是 決定還是閱讀更權威的官方文檔
http://uwsgi-docs.readthedocs.io/en/latest/Nginx.html (這一篇介紹與nginx交互的頁面,答案就在此)
重要的事情說三遍!
Note: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hack
Note: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hack
Note: ancient uWSGI versions used to support the so called “uwsgi_modifier1 30” approach. Do not do it. it is a really ugly hack
簡言之,不要用“uwsgi_modifier1“了!
再看上下文,發現了個更有“意思”的用法
[uwsgi] socket = 127.0.0.1:3031 ; mount apps mount = /app1=app1.py mount = /app2=app2.py ; rewrite SCRIPT_NAME and PATH_INFO accordingly manage-script-name = true
大家看到這里也知道如何搞定了吧……
對,最后的樣子就是這樣子😄
依葫蘆畫瓢
[uwsgi] mount = /get_data=/home/yxgly/code/doraemon/DsBag/GET_DATA/Get_Data_Api.py mount = /get_info=(此處省略,就不寫了) mount = /get_class=(此處省略,就不寫了) callable = app manage-script-name = true socket = /tmp/uwsgi_ini.sock
這里用的是ini格式的作為配置文件,所以啟動方式稍微有些變化(當然,你也可以用xml來寫)
uwsgi --ini myscg_conf.ini -d /tmp/uwsgi.log
然后只需在nginx層在配置一個‘/‘ 的location即可。
形如:
server {
listen 8888;
server_name _;
location / {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi_ini.sock;
}
access_log /app/Nginx/logs/access_8888.log main;
}
然后就可以使用
http://{ip}:8888/get_data/……
http://{ip}:8888/get_info/……
http://{ip}:8888/get_class/……
來訪問了。
若有更好的建議,歡迎留言,持續改進😄