简体   繁体   中英

the server does not distribute django + gunicorn + nginx static

On my ubuntu 20.04 I am trying to deploy a Django 3.2.8 project using nginx and gunicorn. The problem is that the server cannot find the static. Because of this, the pages are displayed without css and images.

I tried many solutions from the internet but nothing helped me. Thank you in advance for your time!

at first im start this script (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 responds to the launch of the start_gunicorn script with this output.

[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

After that, the site is launched and is available at 185.46.8.164:8001. I can open different pages of the site, but static is not available.

From the error log, I realized that the server is accessing the correct folder for static. That is, he is looking for it in the folder where it is. But for some reason he does not see it there and gives ERROR 404 NOT FOUND

PS I've really tried a lot of things. For example, I changed the ip in the configs from 127.0.01 to the real ip of my server and back. I changed the configs "location /static" to "location /static/" and back. Then I changed the paths from /path/to to path/to/static and back. Changed alias to root and back. And much more that is offered on forums on the Internet

I hope that you can help me, because I have completely run out of ideas

use location /static/ instead of /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 *;
    }
    }

Make sure to run collectstatic and restat nginx

It turned out that the error was that nginx was giving a 502 error. I reconfigured my files and the 502 error went away. But now I have met the 500 error.

line from /var/log/nginx/error.log:

worker_connections are not enough while connecting to upstream...

my configs: my_app.conf (nginx config):

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 file):

[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
  

I found two ways on the Internet. 1) Change my_app.conf. But the changes didn't help me. 2) In /etc/nginx/nginx.conf, change the value of worker_connections and add the worker_rlimit_nofile parameter. But too large values cause me the socket() failed... error (shown below). And too small - do not solve the problem. socket() failed (24: To many open files)...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM