2012-07-11 3 views
4

En ce moment, je suis en train de suivre ce tutoriel:Comment puis-je server des fichiers statiques avec nginx et gunicorn pour une application Django?

http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn

Les charges du site modèle correctement, mais les images ne se charge pas. Voici une partie de mon dossier de config.py pour mon application:

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/" 
MEDIA_ROOT = '' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash. 
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 
MEDIA_URL = '' 

# 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 = '/home/lilo/textImageSite/imageSite/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. 
) 

Mon fichier de configuration nginx (situé dans/etc/nginx/sites activés):

server { 
listen 80; 
server_name xxx.xx.xx.xx; #the actual IP of the server; it has a public IP address 

access_log /home/lilo/textImageSite/access.log; 
error_log /home/lilo/textImageSite/error.log; 

location /static { 
    root /home/lilo/textImageSite/imageSite; 
} 

location/{ 
    proxy_pass http://127.0.0.1:8888; 
} 
} 

Mon fichier gunicorn_conf:

bind = "0.0.0.0:8888" 
logfile = "/home/lilo/textImageSite/gunicorn.log" 
workers = 3 

Et maintenant dans mon modèle, voilà comment je suis accès à l'image:

<img src="{{STATIC_URL}}{{ choice.image_location}}" /> <br /> 

Voici ce que le code HTML généré ressemble:

<img src="/static/street_sign2.jpg" /> <br /> 

Désolé pour le mur de texte, mais je ne peux pas comprendre ce qui ne va pas avec ma configuration ...

+0

Oh oui, j'essaie d'obtenir cette application sur xxx.xx.xxx.xx : 8888/imageSite (adresse IP cachée pour confidentialité: D). Le seul problème en ce moment est que l'application ne peut pas trouver le fichier d'image statique sur le serveur, donc il montre juste une image brisée – Raymond

+0

Qu'est-ce qui se trouve dans le journal des erreurs? – VBart

+0

Les fichiers statiques ne seront pas servis tant que 'DEBUG = False' http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access- échec – Montaro

Répondre

14

je fixe sur Transforme mon propre problème ... mal compris comment Nginx a travaillé. : D

server { 
listen 1234; //port that Nginx listens on 
server_name xxx.xx.xx.xx; #the actual IP of the server; it has a public IP address 

access_log /home/lilo/textImageSite/access.log; 
error_log /home/lilo/textImageSite/error.log; 

location /static { 
    root /home/lilo/textImageSite/imageSite; 
} 

location/{ 
    proxy_pass http://127.0.0.1:8888; //the port that Gunicorn uses 
} 
} 

Donc, dans mon cas, si j'ai mon exemple gunicorn fonctionnant sur le port 8888, puis aller à xxx.xxx.xx.x: 8888/textImageSite chargerait la page, mais sans aucun contenu statique. Si j'y accède en utilisant xxx.xxx.xx.x: 1234, la page chargera le contenu statique (images, feuilles de style CSS, etc.). C'est la première fois que j'utilise Gunicorn et Nginx (et aussi pour la première fois une application Django) donc j'espère que cela aidera quelqu'un qui est confus :)

+0

Cela m'a aidé à comprendre nginx. Merci! Après avoir joué avec apache2 + mod_wsgi, nginx + gunicorn est tellement plus facile à configurer. – Paul

+0

Merci. Mon problème était dans le symbole '/' derrière l'adresse. – kotrfa

Questions connexes