简体   繁体   English

Apache Django和静态和媒体文件服务

[英]Apache django and static and media file serving

I have a django project that I finally managed to serve using apache. 我有一个django项目,我终于设法使用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. 我想非常简单,因此尽管我的测试服务器提供/ media和static服务,但没有将它们包含在site.conf文件中。 I wanted first to check if deployment works and then have apache serve static files. 我想先检查部署是否可行,然后让apache提供静态文件。 But, to be honest, Apache is serving files from media folder without me having to do anything. 但是,老实说,Apache无需提供任何服务即可从媒体文件夹提供文件。 My apache conf is 我的apache conf是

<VirtualHost *:80>
    ServerAdmin webmast@rhombus.com
    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 我的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. 编辑:我做了您描述的更改,但是现在我有了一种奇怪的态度:)我没有得到任何媒体或静态投放(403错误),并且首先单击任何链接或地址栏中的错误400,第二次打开网页正常。

error.log 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. 当我访问该网站时,我收到一个错误的请求(400)。 If I click again it opens the site normally, but I still get the same error on my error.log. 如果我再次单击,它将正常打开该站点,但是在error.log上仍然出现相同的错误。

It's because you have specifically added some django media serving URL patterns. 这是因为您专门添加了一些提供URL模式的django媒体。 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; 您正在通过python在MEDIA_URL服务MEDIA_ROOT ; not recommended except during development. 不建议在开发期间使用。

You should wrap that addition in an if settings.DEBUG = True statement. 您应该将该附加内容包装在if settings.DEBUG = True语句中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM