pitchdiesel
pitchdiesel

Reputation: 309

Static files 404 not found error with ssl - Django + uWSGI + nginx

I have a nginx server with my site enabled which is listening on 443 and forwarding traffic to my django app server which is using uWSGI. I can get to the admin page and log in but not static files are served. I've ran python3 manage.py collectstatic. Everything worked before adding SSL with letsencrypt. Getting this in the nginx access logs:

"GET /static/admin/css/base.css HTTP/1.0" 404

Here's my reverse proxy nginx site config:

upstream staging_app_server {
    server 52.52.52.52;
}

server {
    listen 80;
    server_name staging.site.com;
    rewrite ^/(.*)  https://staging.site.com/$1 permanent;
}

server {

    listen 443;
    server_name staging.site.com;

    include snippets/ssl-staging.site.com.conf;
    include snippets/ssl-params.conf;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;

    access_log            /var/log/nginx/staging.access.log;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;

      proxy_pass          http://staging_app_server;
      proxy_redirect      http://staging_app_server https://staging_app_server;
    }

    location ~ /.well-known {
                allow all;
    }

  }

And the app server nginx config:

server {
    listen 80;
    server_name staging.site.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/ubuntu/api/staticfiles;
    }

    location /media/ {
        alias /home/ubuntu/api/media;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/run/uwsgi/api.sock;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param       Host $host;
        uwsgi_param       X-Real-IP $remote_addr;
        uwsgi_param       X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param       X-Forwarded-Host $server_name;
        uwsgi_param       X-Forwarded-Proto $scheme;
    }
}

What am I missing?

Upvotes: 1

Views: 1568

Answers (2)

pitchdiesel
pitchdiesel

Reputation: 309

This worked:

location /static {
    autoindex on;
    alias  /home/ubuntu/api/staticfiles;
}

Thanks to @richard-smith for pointing me in the right direction!

Upvotes: 0

Richard Smith
Richard Smith

Reputation: 49742

Your alias statements are wrong. Either both the value of the location and the value of the alias should end in / or neither end in /.

Also, use root where the alias value ends with the location value. See this document for details.

For example:

location /static/ {
    alias /home/ubuntu/api/staticfiles/;
}
location /media/ {
    root /home/ubuntu/api;
}

Upvotes: 1

Related Questions