django部署在nginx,在配置unicorn后,无法访问页面。

提示502 Bad Gateway,错误网关。

查看错误日志 sudo journalctl -u gunicorn

May 18 13:45:27 shweb01 gunicorn[5691]: [2020-05-18 05:45:27 +0000] [5714] [ERROR] Exception in worker process
May 18 13:45:27 shweb01 gunicorn[5691]: Traceback (most recent call last):
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, i
May 18 13:45:27 shweb01 gunicorn[5691]:     worker.init_process()
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 1
May 18 13:45:27 shweb01 gunicorn[5691]:     self.load_wsgi()
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/workers/base.py", line 1
May 18 13:45:27 shweb01 gunicorn[5691]:     self.wsgi = self.app.wsgi()
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, i
May 18 13:45:27 shweb01 gunicorn[5691]:     self.callable = self.load()
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49
May 18 13:45:27 shweb01 gunicorn[5691]:     return self.load_wsgiapp()
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39
May 18 13:45:27 shweb01 gunicorn[5691]:     return util.import_app(self.app_uri)
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/home/djangoadmin/pyapps/venv/lib/python3.6/site-packages/gunicorn/util.py", line 358, in i
May 18 13:45:27 shweb01 gunicorn[5691]:     mod = importlib.import_module(module)
May 18 13:45:27 shweb01 gunicorn[5691]:   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
May 18 13:45:27 shweb01 gunicorn[5691]:     return _bootstrap._gcd_import(name[level:], package, level)
May 18 13:45:27 shweb01 gunicorn[5691]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
May 18 13:45:27 shweb01 gunicorn[5691]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
May 18 13:45:27 shweb01 gunicorn[5691]:   File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
May 18 13:45:27 shweb01 gunicorn[5691]: ModuleNotFoundError: No module named 'ChanStock.wsgi.application'; 'ChanStock.wsgi' is not a packag
May 18 13:45:27 shweb01 gunicorn[5691]: [2020-05-18 05:45:27 +0000] [5714] [INFO] Worker exiting (pid: 5714)
May 18 13:45:28 shweb01 gunicorn[5691]: [2020-05-18 13:45:28 +0800] [5691] [INFO] Shutting down: Master
May 18 13:45:28 shweb01 gunicorn[5691]: [2020-05-18 13:45:28 +0800] [5691] [INFO] Reason: Worker failed to boot.
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
May 18 13:45:28 shweb01 systemd[1]: Stopped gunicorn daemon.
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Start request repeated too quickly.
May 18 13:45:28 shweb01 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
May 18 13:45:28 shweb01 systemd[1]: Failed to start gunicorn daemon.

ModuleNotFoundError: No module named 'ChanStock.wsgi.application'; 'ChanStock.wsgi' is not a packag

看到这句提示是无法找到wsgi这个配置,找了很多网上的解决方法。有提示换端口的,有提示修改文件到root路径下。

实际原因是配错了,: 写成了.

sudo vi /etc/systemd/system/gunicorn.service

gunicorn.service setup

修改为冒号后,重新加载配置就ok了。

sudo systemctl daemon-reload
sudo systemctl restart gunicorn.service

参考:

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04#prerequisites-and-goals)

https://www.digitalocean.com/community/questions/setting-up-gunicorn-for-django-project-203-exec


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!



0 条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注