簡體   English   中英

nginx 在 18.04 LTS 上運行 django 時 uwsgi 出現 502 錯誤

[英]nginx gives 502 error with uwsgi for running django on ubuntu 18.04 LTS

注意:我是 django 及其部署的新手。

根據本指南中提到的步驟,通過 uwsgi 和 nginx 部署 django - 除了帝王附庸配置並且沒有任何虛擬環境。

旁注:該站點使用python3 manage.py 0.0.0.0:8800

但是,nginx 似乎在套接字中面臨權限問題,並在瀏覽器中出現 502 bad gateway 錯誤。

nginx 錯誤日志顯示以下錯誤:

2020/07/08 21:05:40 [crit] 3943#3943: *3 connect() 到 unix:///home/ubuntu/deploymenttst/MySite/MySite.sock 在連接到上游時失敗(13:權限被拒絕) ,客戶端:192.168.12.12,服務器:192.168.12.12,請求:“GET / HTTP/1.1”,上游:“uwsgi://unix:///home/ubuntu/deploymenttst/MySite/MySite.sock:”,主機:“192.168.12.12:8400”

配置如下:

  1. 在項目的 settings.py 文件中,配置設置為(除了默認的 wsgi):

     DEBUG = False ALLOWED_HOSTS = [ "192.168.12.12" ] STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  2. /etc/nginx/sites-available/MySite_nginx.conf 中的/etc/nginx/sites-available/MySite_nginx.conf文件具有以下配置條目:

     # MySite_nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:///home/ubuntu/deploymenttst/MySite/MySite.sock; # for a file socket #server 127.0.0.1:8008; } # configuration of the server server { # the port your site will be served on listen 8400; # the domain name it will serve for server_name 192.168.12.12; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # # Django media location /media { alias /home/ubuntu/deploymenttst/MySite/media; # your Django project's media files - amend as required } location /static { #alias /home/ubuntu/deploymenttst/MySite/main/static; # your Django project's static files - amend as required #alias /home/ubuntu/deploymenttst/MySite/register/static; # your Django project's static files - amend as required alias /home/ubuntu/deploymenttst/MySite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /home/ubuntu/deploymenttst/MySite/uwsgi_params; # the uwsgi_params file you installed } }

    它已被符號鏈接到 /etc/nginx/sites-enabled/MySite_nginx.conf。

  3. uwsgi_params 文件在項目目錄中創建,包含以下條目:

     uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REQUEST_SCHEME $scheme; uwsgi_param HTTPS $https if_not_empty; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name;
  4. MySite_uwsgi.ini 文件里面的內容如下:

     [uwsgi] # Django-related settings # the base directory (full path) chdir = /home/ubuntu/deploymenttst/MySite # Django's wsgi file module = MySite.wsgi # the virtualenv (full path) #home = /home/ubuntu/deploymenttst/MySite/ # process-related settings # master master = true # maximum number of worker processes processes = 20 # the socket (use the full path to be safe socket = /home/ubuntu/deploymenttst/MySite/MySite.sock #... with appropriate permissions - may be needed chmod-socket = 666 uid = www-data gid = www-data # clear environment on exit vacuum = true
  5. Static 文件已使用python3 manage.py collectstatic到項目目錄內的 static 目錄中

  6. 在其中一個終端 windows 中,使用以下命令成功啟動了 uwsgi 進程: uwsgi --ini MySite_uwsgi.ini

  7. nginx 已在每次配置更改時停止並啟動並重新啟動多次,否則也是如此。

  8. MySite 項目目錄的 uid:gid 已設置為 www-data:www:data 使用sudo chown -R www-data:www:data *

為什么我仍然收到錯誤的網關 502 錯誤,其中由於權限問題無法聯系上游 django 應用程序?

通過將項目放置到 /tmp 目錄解決了這個問題。

從 www-data 用戶運行的 nginx 無法訪問內部目錄 MySite,因此盡管已分配給用戶 www-data,但套接字或放置在那里的文件。

現在,我的另一個問題是關於 nginx 權限問題的原因,盡管向 www-data 提供了目錄的 uid 和 gid,但可能是什么問題?

注意:我的用戶名為ubuntu是 sudoer。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM