Apostolos
Apostolos

Reputation: 8101

Apache django and static and media file serving

I have a django project that I finally managed to serve using apache. I wanted to be very simple so although my test server was serving /media and static, I didn't include them in my site.conf file. I wanted first to check if deployment works and then have apache serve static files. But, to be honest, Apache is serving files from media folder without me having to do anything. My apache conf is

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName myrhombus.com
    ServerAlias www.myrhombus.com
    WSGIScriptAlias / /srv/www/rhombus2/rhombus/wsgi.py
    <Directory /srv/www/rhombus2/rhombus>
    <Files wsgi.py>
            Require all granted
    </Files>
    </Directory>

    Alias /static/ /srv/www/rhombus2/static/
    Alias /media/ /srv/www/rhombus2/media/

    <Directory /srv/www/rhombus2/static>
        Require all granted
    </Directory>

    <Directory /srv/www/rhombus2/media>
        Require all granted
    </Directory>


</VirtualHost>

As you can see no media or static alias.

My urls.py

urlpatterns = patterns('',
    # Examples:
    #differnet  urls here etc...
    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

How is this possible?

EDIT: I did the changes you describe bellow, but now I get a wierd attitude :) I don't get any media or static served (403 Error ) and first click in any link or address bar gives me a 400 Error, second opens web page normally.

error.log

[Tue May 20 10:12:56.049081 2014] [authz_core:error] [pid 1360:tid 140612925908736] [client 127.0.0.1:48360] AH01630: client denied by server configuration: /serv, referer: http://www.myrhombus.com/accounts/login/

And I get a Bad Request(400) when visiting the site. If I click again it opens the site normally, but I still get the same error on my error.log.

Upvotes: 2

Views: 5110

Answers (1)

It's because you have specifically added some django media serving URL patterns. You are right to be concerned!

+static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

You are serving MEDIA_ROOT at MEDIA_URL through python; not recommended except during development.

You should wrap that addition in an if settings.DEBUG = True statement.

if settings.DEBUG:
   urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

Upvotes: 1

Related Questions