1

I'm using Django+Gunicorn+Nginx for my web application in a Ubuntu 22.04.4 server. I followed this guide until the end and my web is running in production. However, static files are not working (css, images and js) and I feel like I've tried everything.

My static files only work if I run the django development server with Debug activated on localhost.

My project is located in /home/user/visor/prod/visor_scs/. My static files are in /var/www/myweb.com/static/ and I've also tried placing then in /home/user/visor/prod/visor_scs/static/ with no result (after adjusting my nginx configuration accordingly).

Here are my settings.py related to static files:

INSTALLED_APPS = [
...,
'django.contrib.staticfiles',
]

STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/myweb.com/static/'

Here is my gunicorn service:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=user
Group=www-data
WorkingDirectory=/home/user/visor/prod/visor_scs
ExecStart=/home/user/visor/prod/visor_scs/prod-envisor/bin/gunicorn \
        --access-logfile - \
        --workers 3 \
        --bind unix:/run/gunicorn.sock \
        visor_scs.wsgi:application

[Install]
WantedBy=multi-user.target

Here is my nginx configuration in /var/nginx/sites-available/visor_scs:

server {
        listen 80;
        server_name myweb.com;
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /static/ {
                root /var/www/myweb.com/static/;


        }

        location / {
                include proxy_params;
                proxy_pass http://unix:/run/gunicorn.sock;
        }
}

I've also tried using alias instead of root and placing my static files inside of my project directory. After every change I've restarted nginx and gunicorn services and run collectstatic. I also gave permissions and ownership to the www-data user for the static and project directories.

I've checked the logs for gunicorn and nginx and there's nothing that cath my eye besides a 404 for all my static files.

Lastly, here are the errors in the development console in the browser:

GET
http://myweb.com/static/visor/styles.css

GET
http://myweb.com/static/visor/leaflet_style.css

GET
http://myweb.com/static/visor/img/logo_vertical_mediano.png
[HTTP/1.1 404 Not Found 105ms]

El recurso de “http://myweb.com/static/visor/styles.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
El recurso de “http://myweb.com/static/visor/leaflet_style.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
GET
http://myweb.com/static/visor/styles.css

GET
http://myweb.com/static/visor/leaflet_style.css

El recurso de “http://myweb.com/static/visor/styles.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
El recurso de “http://myweb.com/static/visor/leaflet_style.css” fue bloqueado debido a una discordancia del tipo MIME (“text/html”) (X-Content-Type-Options: nosniff).
visor
GET
http://myweb.com/static/visor/img/apple-touch-icon.png
[HTTP/1.1 404 Not Found 33ms]

GET
http://myweb.com/static/visor/img/favicon-16x16.png
[HTTP/1.1 404 Not Found 24ms]

I feel like I'm banging my head against a wall, I'll supply any relevant info/logs. Thank you for reading if you've reached this point.

2
  • Use root /var/www/myweb.com;
    – AlexD
    Commented Mar 1 at 12:07
  • @AlexD I've also tried that to no avail.
    – pfrud
    Commented Mar 1 at 12:10

1 Answer 1

1

I just had to edit my nginx settings, removing the '=' in there.

  location /static/ {
    root /var/www/myweb.com;
  }

After restarting Nginx my static files are working.

1
  • remind to accept your answer afterwards, else we will be reminded to solve the question until the end of the universe ;)
    – djdomi
    Commented Mar 2 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .