Margo
Margo

Reputation: 368

Django looks for media files in wrong directory with DEBUG = False and Nginx

My 2 Django apps are running with Nginx and Gunicorn on mysite.com and mysite.com/app2.

App1's media files are working just fine. And for the app2 with DEBUG=False I get 404 when trying to download a media file, though the url it makes for the file is correct (it matches the directory on the server).

From nginx error log I found that it is probably looking for it in the media directory of app1.

How can I make app2 look for media in the correct directory?

Nginx log error:

*2020/06/09 13:24:51 [error] 9378#9378: 1 open() "/home/user/app1/media/attach_1/attach.pdf" failed (2: No such file or directory), client: 134.94.7.210, server: mysite.com, request: "GET /media/attach_1/attach.pdf HTTP/1.1", host: "mysite.com", referrer: "mysite.com/app2/"

Nginx conf:

 server {
            listen 80;
            server_name server_domain;
            location = /favicon.ico { access_log off; log_not_found off; }

            location /static/ {
                    root /home/user/app1;
            }
            location /app2/static/ {
                root /home/user;
            }

            location /media/ {
                root /home/user/app1;
             }
            location /app2/media/ {
                root /home/user;
            }

            location / {
                include proxy_params;
                proxy_pass http://unix:/run/app1.sock;
            }
            location /secondapp/ {
                include proxy_params;
                proxy_pass http://unix:/run/app2.sock:/;
            }
        }

app2.settings:

STATIC_URL = '/static/'
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = 'media/'

I also have urlpatterns = [...] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 1

Views: 1117

Answers (2)

Ivan Shatsky
Ivan Shatsky

Reputation: 15470

In addition to @0p3r4t0r answer, you should made the following changes in your nginx.conf:

    location /app2/static/ {
        root /home/user/;
    }
    location /app2/media/ {
        root /home/user/;
    }

And even this would work only if your app2 URI prefix is the same as the name of your app2 folder. Otherwise you'll need an alias directive:

    location /app2/static/ {
        alias /home/user/app2/static/;
    }
    location /app2/media/ {
        alias /home/user/app2/media/;
    }

But if they are the same, the first config is preferable.

Upvotes: 3

0p3r4t0r
0p3r4t0r

Reputation: 683

For app2 shouldn't you change you media and static urls to point to the urls for app2 on your server?

STATIC_URL = '/app2/static/'
...
MEDIA_URL = '/app2/media/'

As it stands they are pointing to the same URLs you're using for app1, which would explain why the URLs are valid, but the wrong directory is being searched for your files.

Upvotes: 1

Related Questions