在開發公司CMDB過程當中,前端使用augular4,後端使用django+restframework。前端
前端angular代碼打包直接經過nginx靜態資源訪問的方式提供服務,後端api經過apache2的wsgi方式啓動,經過nginx代理解決跨域向前端提供服務。python
在本機開發的時候就很簡單,nginx同時轉發angular啓動的web服務,django啓動的api服務,api訪問也很正常。nginx
搭建集成測試環境時,後端使用virtualenv打包,經過apache2 conf啓動,相關配置以下:web
nginx:apache
root /usr/share/cmdb/web location /api { proxy_pass http://127.0.0.1:8000; } location /docs { proxy_pass http://127.0.0.1:8000; } location /static { proxy_pass http://127.0.0.1:8000; }
apache2django
<VirtualHost *:8000> WSGIScriptAlias / /usr/share/cmdb/venv/cmdb-api/cmdb/wsgi.py WSGIDaemonProcess cmdb python-path=/usr/share/cmdb/venv/lib/python2.7/site-packages WSGIProcessGroup cmdb WSGIApplicationGroup %{GLOBAL} Alias /static/ /usr/share/cmdb/venv/lib/python2.7/site-packages/rest_framework/static/ <Directory /usr/share/cmdb/venv/lib/python2.7/site-packages/rest_framework/static/> Require all granted </Directory> </VirtualHost>
api調用是須要登陸後在header中加入Authorization,但搭建好的測試環境除了登陸api能調用外,其他的api所有報401,提示信息爲"invalid token header"。後端
經過print打印發現傳入的Authorization值爲空。api
開始懷疑nginx沒有將Authorization頭轉發,經過postman直接請求api,報相同的問題。跨域
排除nginx問題,又懷疑是virtualenv依賴不全致使的,而後停掉apache2,激活virtualenv時經過python manager.py runserver調用api,發現沒有問題。python2.7
最終剩下apache2的問題,這時候看了openstack keystone的配置,發現有個WSGIPassAuthorization On,看着很像。
在conf中加上此配置,Authorization請求頭順利轉發。api調用正常。
最終的apache配置以下
<VirtualHost *:8000> WSGIScriptAlias / /usr/share/cmdb/venv/cmdb-api/cmdb/wsgi.py WSGIDaemonProcess cmdb python-path=/usr/share/cmdb/venv/lib/python2.7/site-packages WSGIProcessGroup cmdb WSGIApplicationGroup %{GLOBAL} WSGIPassAuthorization On Alias /static/ /usr/share/cmdb/venv/lib/python2.7/site-packages/rest_framework/static/ <Directory /usr/share/cmdb/venv/lib/python2.7/site-packages/rest_framework/static/> Require all granted </Directory> </VirtualHost>