arthem
arthem

Reputation: 151

Flask + Gunicorn + NGINX Static files?

Having some trouble serving static files (CSS + JS) with NGINX and Flask. Current setup has the Flask website running in one container and NGINX running in another. Static files are stored at /static within the Flask app directory.

How can I allow NGINX Container to access the CSS and JS files?

Flask Dockerfile: FROM python:3.8-slim

WORKDIR /usr/src/app

RUN pip install pipenv
COPY Pipfile* ./
RUN pipenv lock --requirements > requirements.txt
RUN pip install -r requirements.txt
RUN pip install gunicorn

COPY . .

NGINX Dockerfile: FROM nginx:1.15.8

RUN rm /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/
RUN rm /etc/nginx/conf.d/default.conf
COPY project.conf /etc/nginx/conf.d/

NGINX Project.conf

server {

    listen 80;
    server_name gunicorn_nginx;

    location / {
        proxy_pass http://Website:8000;

        # Do not change this
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /static {
        rewrite ^/static(.*) /$1 break;
        root /Website/; 
    }
}

I have tried looking through some tutorials online but they don't seem to cover this aspect. Thanks in advance.

EDIT: It seems that by removing

location /static {
    rewrite ^/static(.*) /$1 break;
    root /Website/; 
}

from the project.config file Gunicorn is able to serve static files?

Upvotes: 1

Views: 1043

Answers (1)

Aleksander Sadowski
Aleksander Sadowski

Reputation: 41

I also had the same problem and removing the static location with alias solved the problem of not loading

I still dont understand why.

Upvotes: 1

Related Questions