簡體   English   中英

服務器不分發 django + gunicorn + nginx static

[英]the server does not distribute django + gunicorn + nginx static

在我的 ubuntu 20.04 上,我正在嘗試使用 nginx 和 gunicorn 部署 Django 3.2.8 項目。 問題是服務器找不到 static。 因此,頁面顯示時沒有 css 和圖像。

我嘗試了許多來自互聯網的解決方案,但沒有任何幫助。 提前感謝您的寶貴時間!

起初我啟動這個腳本(start_gunicorn.sh)

#!/bin/bash
source /root/myapps/zhanna_first_web/env/bin/activate
exec gunicorn -c /root/myapps/zhanna_first_web/zhanna/gunicorn_config.py lawyer>

gunicorn_config.py:

command = '/root/myapps/zhanna_first_web/env/bin/gunicorn'
pythonpath = '/root/myapps/zhanna_first_web/zhanna'
bind = '185.46.8.164:8001'
workers = 3
user = 'root'
limit_request_fields = 32000
limit_request_field_size = 0
raw_env = 'DJANGO_SETTINGS_MODULE=lawyer.settings'

/etc/nginx/sites-enabled/default

server {
   listen 80;
   server_name 185.46.8.164;

   location /static {
      autoindex on;
      root /myapps/zhanna_first_app/zhanna/static;
   }

    location / {
        proxy_pass http://185.46.8.164:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        add_header Access-Control-Allow-Origin *;
    }
}

Ubuntu 使用此 output 響應 start_gunicorn 腳本的啟動。

[2022-01-09 09:53:31 +0300] [76583] [INFO] Starting gunicorn 20.1.0
[2022-01-09 09:53:31 +0300] [76583] [INFO] Listening at: http://185.46.8.164:8001 (76583)
[2022-01-09 09:53:31 +0300] [76583] [INFO] Using worker: sync
[2022-01-09 09:53:31 +0300] [76585] [INFO] Booting worker with pid: 76585
[2022-01-09 09:53:31 +0300] [76586] [INFO] Booting worker with pid: 76586
[2022-01-09 09:53:31 +0300] [76587] [INFO] Booting worker with pid: 76587

之后,該站點啟動並在 185.46.8.164:8001 可用。 我可以打開網站的不同頁面,但 static 不可用。

從錯誤日志中,我意識到服務器正在訪問 static 的正確文件夾。 也就是說,他正在它所在的文件夾中尋找它。 但由於某種原因,他在那里看不到它並給出錯誤 404 NOT FOUND

PS我真的嘗試了很多東西。 例如,我將配置中的 ip 從 127.0.01 更改為我服務器的真實 ip 並返回。 我將配置“位置/靜態”更改為“位置/靜態/”並返回。 然后我將路徑從 /path/to 更改為 path/to/static 並返回。 將別名更改為 root 並返回。 互聯網論壇上提供的更多內容

我希望你能幫助我,因為我已經完全沒有想法了

使用位置/static/而不是 /static

server {
            listen 80;
            server_name 185.46.8.164;

        
            location = /favicon.ico { access_log off; log_not_found off; }
            location /static/ {
                root /myapps/zhanna_first_app/zhanna/static;
            }
            location /media/  {
                root /path-to-media-folder;
            }
    
        location / {
        proxy_pass http://185.46.8.164:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        add_header Access-Control-Allow-Origin *;
    }
    }

確保運行 collectstatic 和 restat nginx

原來,錯誤是 nginx 給出了 502 錯誤。 我重新配置了我的文件,502 錯誤消失了。 但現在我遇到了 500 錯誤。

/var/log/nginx/error.log 中的行:

worker_connections are not enough while connecting to upstream...

我的配置:my_app.conf(nginx 配置):

server {
    listen 82;
    server_tokens off;
    server_name 185.46.8.164;

    #root /var/www/
    location / {
        include uwsgi_params;
        uwsgi_pass unix:///run/uwsgi/app/myapp/socket;
        #
        try_files $uri $uri/ =404;
        #
        proxy_pass http://185.46.8.164:82;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        add_header Access-Control-Allow-Origin *;
    }

    location /static/ {
        root /eva/static;
    }

}

myapp.ini(uwsgi.ini 文件):

[uwsgi]
chdir = /root/eva/lawyer
env = DJANGO_SETTINGS_MODULE= lawyer.settings.production
wsgi-file = lawyer/wsgi.py
#module = lawyer.uwsgi:application
workers = 1
max-requests = 5000
#plugins-dir=/usr/lib/uwsgi/plugins/
#plugins = python3
#virtualenv = /root/eva/venv
home = /root/eva/venv
processes = 5
threads = 2
master = true
die-on-term = true
socket = /run/sedova.sock
chmod-socket = 666
vacuum = true
uid = www-data
gui = www-data
  

我在網上找到了兩種方法。 1) 更改 my_app.conf。 但這些變化對我沒有幫助。 2)在/etc/nginx/nginx.conf中,修改worker_connections的值,添加worker_rlimit_nofile參數。 但是太大的值會導致我的 socket() 失敗...錯誤(如下所示)。 而且太小 - 不解決問題。 socket() 失敗(24:對許多打開的文件)...

暫無
暫無

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

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