Django, nginx, and gunicorn team up but fail to load or serve any static files

As I prepare to make my page public, I encountered an issue after setting up nginx + gunicorn. The page loads fine, but the CSS is not loading despite no errors being shown in the nginx log file.

Here is a snippet from my nginx.conf:

server {
    listen 8000;
    server_name 127.0.0.1;

    access_log /<direct_path>/logs/nginx-access.log;     
    error_log /<direct_path>/logs/nginx-error.log;      

    location / {
        proxy_pass http://127.0.0.1:9000;         
    }

    location /static {
        alias /<project path>/chemicalizer;  
    }
}

My gunicorn configuration (gunicorn.conf.py) located in the same directory as manage.py:

bind = "127.0.0.1:9000"                   
errorlog = '/<direct_path>/logs/gunicorn-error.log' 
accesslog = '/<direct_path>/logs/gunicorn-access.log'
loglevel = 'debug'
workers = 4     

Information extracted from my project's settings.py file:

import os, djcelery
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

djcelery.setup_loader()
BROKER_URL = 'amqp://guest:guest@localhost:5672'

SECRET_KEY = censored...

DEBUG = False
ALLOWED_HOSTS = ['0.0.0.0', '127.0.0.1', 'localhost']

INSTALLED_APPS = ( ... )   

MIDDLEWARE_CLASSES = ( ... )   

ROOT_URLCONF = 'chemicalizer.urls'

TEMPLATES = [ ... ]

I would greatly appreciate any help on this matter. Let me know if there are other files needed for reference.

Update: Further investigation reveals that while my css file is being loaded by nginx, the HTML content sent by gunicorn seems incomplete. Checking with Chrome's "developer tools," the network tab shows successful loading of the CSS file, but in the sources tab, the CSS file appears empty.

Moreover, the gunicorn-access.log does not show a GET command for the css file. This behavior differs when running a Debug server. Any insights into why this occurs?

Answer №1

It appears that you've directed Nginx to your main project directory instead of the directory where your static files are stored.

location /static {
    alias /<project path>/chemicalizer;  # <- ensure nginx is serving the static content
}

Instead, use:

location /static {
    alias /<project path>/chemicalizer/static;  # <- ensure nginx serves the static content
}

Answer №2

After numerous attempts, I finally stumbled upon a solution to my dilemma. It turns out that a minor error in my html code was occasionally triggering a GET request. However, the main issue causing the CSS file not to load was due to an incorrect mime type. I resolved this by creating a new file named mime.types within the /etc/nginx/ directory with the following content:

types {
    text/css            css;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be the reason my dropdown menu is not appearing on hover?

Currently, I am in the process of developing a menu using angularJS and google app script within a dialog box. I have been referring to this sample code for guidance. Instead of pasting all my code here, I can summarize what I have come up with: var a ...

Unable to set DIV to 'inline-block' or none based on checkbox selection

Despite reviewing multiple examples, I am still struggling to make this DIV visible and hidden by clicking a checkbox. Can someone please review my JavaScript code? <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Conten ...

A streamlined approach to extracting images from websites using Django and Python

As a Django/Python beginner, I am currently working on a project where users can input a URL, and the site will scrape images above a certain size along with the page title. The process involves using Selenium to extract content from the destination page, ...

Equalizing the space between table headings and content

I am struggling with aligning the heading and entries in my table properly. https://i.stack.imgur.com/IYtY1.jpg My goal is to ensure that the heading and entries are perfectly aligned with each other. This is how my Table.jsx looks like: <div classNa ...

The 'utf8' codec encountered an issue decoding byte 0xe4 in timezone due to an invalid continuation byte

While working on the UserProfile model, I've implemented the following code: def repeated_times(self, test, date): return self.user.user_test_results.filter(taken_date__month=date.month, djangotest=test).count() However, I encountered an error: ...

There is a space separating the slider image and the navigation bar

Having a small space between the slider image and the navigation bar is causing some issues. I've tried various solutions like minifying the code, adjusting margins, and setting line heights to zero but nothing seems to be working. One question I have ...

Mastering the art of finding the perfect position

I'm trying to keep the menu on top, but when I hover over one menu, the others should not overlap. How can I achieve this? Thanks for any help! Before: https://i.sstatic.net/esBw6.png After: https://i.sstatic.net/slW1n.png Expected: https://i.s ...

Using jQuery sortable with the overflow property set to hidden to sort items between two lists

I need help with a jQuery sortable feature where I have two lists and can move items between them: $( '#productsList, #orderList' ) .sortable({connectWith: '.containerDiv'}) .disableSelection(); My issue arises when I try to implement ...

CSS: Continuous animation loop does not smoothly transition between initial and final keyframes

It seems like I have come across an issue with a CSS animation that I can't find a solution for. The animation is supposed to loop infinitely, and while it is working, there is a problem. // CSS @keyframes myanim { 0% { transform: translate(0, 0) ...

Encountering the error message "The remote host abruptly terminated the connection"

https://i.sstatic.net/ansC8.png https://i.sstatic.net/u1GZw.png After working on my practice website for over a year without any major problems, I suddenly encountered an issue that I haven't seen before. Despite not making any intentional changes t ...

Instructions on implementing a floating label on a select2 dropdown menu

I am currently utilizing the select2 plugin for my select dropdowns on a webpage. I have multiple select dropdown boxes and I need to implement a floating label specifically for the selected dropdown. Despite my efforts and searches, I have not been able ...

flexbox wrapping text styling

Can text be wrapped in flex within a fixed width container? For example: img username added you as a friend Instead of: user added img name you as a friend I am able to achieve this if the image and 'a' were separat ...

Is there a way to eliminate the background color from crispy-forms in Django?

In my Django project, I am utilizing crispy forms along with the bootstrap5 template pack. My goal is to modify the visual button behavior and remove the background color associated with it. Currently, I have set CRISPY_TEMPLATE_PACK = "bootstrap5" and CR ...

Unable to Toggle Bootstrap 5 Dropdown

Take a look at my code below. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewpor ...

Is there a way to modify the appearance of a text box to suit individual preferences?

Is it possible to alter the shape of a text box, creating a rectangle with a portion removed from one side? I have included an example image for reference. https://i.sstatic.net/oNyum.png I came across clip-path: polygon in CSS, but it seems to only accep ...

The conclusion of jQuery's nested animation

Functioning, But Page Transitions Inconsistent What I Believe to be the Correct Approach, But Event Triggering Issue Currently, I am experimenting with page transitions and utilizing bind() to detect the end of the CSS event transition. When a class is ad ...

Turn off automatic spelling correction and predictive text in a content-editable section

I'm currently working on a cross-browser application using Script#. I've incorporated a contenteditable div where users can input text. However, I am facing an issue with the auto correct/auto completion feature altering the user's text. Co ...

Adjusting the height of an Angular CDK virtual scroll viewport based on dynamic needs

Currently, I am developing an Angular table with the cdk Virtual Scroll feature. Is there a method to adjust the scroll viewport's height dynamically? While the standard style property works, I am encountering difficulties setting the value using ngSt ...

Leveraging the power of Bootstrap 4 to place the footer beneath all remaining content on the

I am currently working on a React application that consists of a header, container, and footer. To add animation to the route loading process, I had to apply position: absolute to the container section of the page. Within the container, there is a row wit ...

Is the sound persistently playing on every single page?

Can an mp3 continue playing from where it left off when you switch to another page on the same site? I'm feeling lost and not sure how to tackle this. Still learning HTML, CSS, etc. Any thoughts or tips would be greatly appreciated. Thank you! ...