Home > Mobile >  the server does not distribute django gunicorn nginx static
the server does not distribute django gunicorn nginx static

Time:01-10

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

P.S 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

CodePudding user response:

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

  •  Tags:  
  • Related