2011-08-05 22 views
5

Tôi gặp lỗi 404 khi truy cập các tệp tĩnh được phân phát qua HTTPS nhưng các tệp tĩnh hoạt động tốt thông qua HTTP.Làm thế nào để phân phối các tệp tĩnh Django thông qua HTTPS?

Để rõ ràng, tôi có thể truy cập vào một trang cụ thể theo cả hai cách, ví dụ: http://domain.com/pagehttps://domain.com/page nhưng trong trường hợp HTTPS, hình ảnh sẽ không tải được.

Bên cạnh đó, việc tiếp cận một hình ảnh trực tiếp http://domain.com/static/image.png công trình nhưng https://domain.com/static/image.png lợi nhuận 404.

Tôi đang chạy Ubuntu 10.04 với Django 1.3 sử dụng mod_wsgi trên apache2.

Dưới đây là các tập tin có liên quan (wsgi và prod.conf và secure_prod.conf và settings.py):

django.wsgi

import os 
import sys 
import site 

sys.stdout = sys.stderr # Allows use of print statements 

PROJECT_ROOT = '/home/code/domain/src/domain-project/' 
site_packages = '/home/code/domain/lib/python2.6/site-packages' 

site.addsitedir(os.path.abspath(site_packages)) 
sys.path.insert(0, PROJECT_ROOT) 
sys.path.insert(1, os.path.join(PROJECT_ROOT, "domain")) 
sys.path.insert(2, site_packages) 
os.environ['DJANGO_SETTINGS_MODULE'] = 'domain.settings' 
os.environ['PYTHON_EGG_CACHE'] = '/home/administrator/.python-eggs' 
os.environ["CELERY_LOADER"] = "django" 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

# Load a monitor to automatically reload apache when files change 
import domain.monitor 
domain.monitor.start(interval=1.0) 

production.conf

<VirtualHost *:80> 

    # Admin email, Server Name (domain name) and any aliases 
    ServerAdmin [email protected] 
    ServerName domain.com 
    ServerAlias *.domain.com 

    DocumentRoot /home/code/domain/src/domain-project/domain 
    LogLevel warn 
    WSGIDaemonProcess domain-production processes=5 maximum-requests=500 threads=100 
    WSGIProcessGroup domain-production 
    WSGIScriptAlias//home/code/domain/src/domain-project/apache/production.wsgi 

    SetEnv PYTHON_EGG_CACHE /home/apache/.python_eggs 

    Alias /admin/media /home/code/domain/lib/python2.6/site-packages/django/contrib/admin/media 
    Alias /site_media /home/code/domain/src/domain-project/static 
    Alias /static /home/code/domain/src/domain-project/static 
    Alias /robots.txt /home/code/domain/src/domain-project/static/robots.txt 
    Alias /favicon.ico /home/code/domain/src/domain-project/static/favicon.ico 

    <Location /admin/media> 
    SetHandler None 
    Order allow,deny 
    Allow from all 
    </Location> 

    <Location /site_media> 
    SetHandler None 
    Order allow,deny 
    Allow from all 
    </Location> 

    <LocationMatch "\.(jpg|gif|png|mp4)$"> 
    SetHandler None 
    </LocationMatch> 

    <LocationMatch "^/(robots\.txt|favicon\.ico|crossdomain\.xml)$"> 
    SetHandler none 
    </LocationMatch> 

    ErrorLog /var/log/apache2/domain/production_error.log 
    LogLevel info 
    CustomLog /var/log/apache2/domain/production_access.log combined 

</VirtualHost> 

secure_production .conf

<VirtualHost *:443> 

    ServerAdmin [email protected] 
    ServerName domain.com 
    ServerAlias *.domain.com 

    DocumentRoot /home/code/domain/src/domain-project/domain 
    LogLevel warn 
    WSGIDaemonProcess domain-production processes=5 maximum-requests=500 threads=100 
    WSGIProcessGroup domain_production_secure 
    WSGIScriptAlias//home/code/domain/src/domain-project/apache/production.wsgi 

    SSLEngine on 
    SSLOptions +StrictRequire 

    <Directory /> 
     SSLRequireSSL 
    </Directory> 

    SSLProtocol -all +TLSv1 +SSLv3 
    SSLCipherSuite HIGH:MEDIUM:!aNULL:+SHA1:+MD5:+HIGH:+MEDIUM 

    SSLCertificateFile /home/code/domain/src/domain-project/apache/key/domain.COM.crt 
    SSLCertificateKeyFile /home/code/domain/src/domain-project/apache/key/domain.com.key 
    SSLCertificateChainFile /home/code/domain/src/domain-project/apache/key/Apache_Plesk_Install.txt 
    SSLVerifyClient none 
    SSLProxyEngine off 

    <IfModule mime.c> 
     AddType application/x-x509-ca-cert  .crt 
     AddType application/x-pkcs7-crl   .crl 
    </IfModule> 


    SetEnv PYTHON_EGG_CACHE /home/apache/.python_eggs 


    Alias /admin/media /home/code/domain/lib/python2.6/site-packages/django/contrib/admin/media 
    Alias /site_media /home/code/domain/src/domain-project/static 
    Alias /static /home/code/domain/src/domain-project/static 
    Alias /robots.txt /home/code/domain/src/domain-project/static/robots.txt 
    Alias /favicon.ico /home/code/domain/src/domain-project/static/favicon.ico 


    <Location /admin/media> 
     SetHandler None 
     Order allow,deny 
     Allow from all 
    </Location> 

    <Location /site_media> 
     SetHandler None 
     Order allow,deny 
     Allow from all 
    </Location> 

    <LocationMatch "\.(jpg|gif|png|mp4)$"> 
     SetHandler None 
    </LocationMatch> 

    <LocationMatch "^/(robots\.txt|favicon\.ico|crossdomain\.xml)$"> 
     SetHandler none 
    </LocationMatch> 

    ErrorLog /var/log/apache2/domain/production_secure_error.log 
    LogLevel info 
    CustomLog /var/log/apache2/domain/production_secure_access.log combined 

</VirtualHost> 

settings.py

# Django settings for domain project. 
import os 

DEBUG = False 
TEMPLATE_DEBUG = DEBUG 

# create a relative path to anything on the project from the PROJECT PATH 
SETTINGS_PATH = os.path.dirname(os.path.abspath(__file__)) 
PROJECT_PATH = os.path.join(*os.path.split(SETTINGS_PATH)[:-1]) 
rel = lambda * args: os.path.join(PROJECT_PATH, *args) 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 
STATIC_ROOT = rel('..', 'static') 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth', 
    'django.core.context_processors.debug', 
    'django.core.context_processors.i18n', 
    'django.core.context_processors.request', 
    'django.core.context_processors.static', 
) 


MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
) 

ROOT_URLCONF = 'domain.urls' 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
    rel('..', 'templates'), 
) 

DJANGO_APPS= [ 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
] 

THIRDPARTY_APPS = [ 
    'djcelery', 
    'djkombu', 
    #'sentry', 
    #'sentry.client', 
    #'south', 
] 

domain_APPS= [] 

INSTALLED_APPS = DJANGO_APPS + THIRDPARTY_APPS + domain_APPS 
+0

Bạn có nhận được django 404 hoặc apacho 404 không? Nếu bạn nhận được một apache 404, sau đó conf sản xuất an toàn của bạn có lẽ là thủ phạm. Nếu bạn nhận được một django 404, sau đó kết hợp sẽ có vẻ là sai. – leech

Trả lời

4

Bạn cần phải bí danh tất cả các tệp phương tiện và quản trị tĩnh của mình.

Tại thời điểm này bạn dường như phục vụ django trên cổng 80 và 443, nhưng các trang web phương tiện truyền thông chỉ trên cổng 80. Chỉ cần sao chép các quy tắc bí danh và các phần vị trí vào secure_production.conf

+0

SO Tôi đã có cùng một vấn đề như Jordan và nó vấp tôi trong hai đến ba giờ. Tôi tiếp tục thử https://mysite.com và không thấy tệp tĩnh. Cuối cùng, khi tôi xóa Chuyển hướng tự động thành https và sau đó kiểm tra http..tất cả các tệp tĩnh đã hoạt động với http. Vì vậy, nhờ giải pháp của bạn ..Tôi chỉ cần thêm Alias ​​/ static/path/to/my/static vào trang mặc định-ssl trên Ubuntu và nó bắt đầu hoạt động! – harijay

2

của bạn 443 máy chủ ảo không thể được sử dụng bởi vì nếu nó đã được mod_wsgi sẽ khiếu nại vì một vài lý do. Lý do đầu tiên là 'domain-production' được sử dụng nhiều lần cho WSGIDaemonProcess mà mod_wsgi sẽ không cho phép như tên phải là duy nhất trên toàn bộ cá thể Apache. Thứ hai, WSGIProcessGroup trong tham chiếu 443 'domain_production_secure' mà không có chỉ thị nhóm WSGIDaemonProcess.

Bạn cần phải xác minh tệp nào đang thực sự được đọc. Bạn có thể làm điều này bằng cách giới thiệu một lỗi cú pháp vào các tệp và xem liệu Apache có phàn nàn khi bạn khởi động nó hay thực hiện một configtest.

Nếu bạn đang sửa đổi nội dung bạn đang đăng, vui lòng không thực hiện điều đó, vì bất kỳ thay đổi nào bạn thực hiện làm thay đổi ý nghĩa sẽ khiến khó gỡ lỗi hơn.

BTW, cấu hình của bạn đã kế thừa những thứ nhất định không cần thiết cho mod_wsgi và chỉ cần thiết cho mod_python. Bạn nên quay lại và sửa lại tài liệu mod_wsgi và làm sạch những thứ này. Trong SetEnv cụ thể cho bộ nhớ cache trứng Python không hoạt động cho mod_wsgi và SetHandler None là không cần thiết với mod_wsgi. Việc sử dụng các chỉ thị Vị trí xung quanh các chỉ thị kiểm soát truy cập Apache cũng là điều xấu. Áp dụng chúng vào thư mục vật lý bằng cách sử dụng chỉ thị Thư mục thay thế.

Các vấn đề liên quan