我如何使用nginx和gunicorn为Django应用程序提供静态文件?

前端之家收集整理的这篇文章主要介绍了我如何使用nginx和gunicorn为Django应用程序提供静态文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在,我正在尝试按照本教程:

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

模板站点正确加载,但图像不加载.这是我的应用程序的config.py文件的一部分:

  1. # Absolute filesystem path to the directory that will hold user-uploaded files.
  2. # Example: "/home/media/media.lawrence.com/media/"
  3. MEDIA_ROOT = ''
  4. # URL that handles the media served from MEDIA_ROOT. Make sure to use a
  5. # trailing slash.
  6. # Examples: "http://media.lawrence.com/media/","http://example.com/media/"
  7. MEDIA_URL = ''
  8. # Absolute path to the directory static files should be collected to.
  9. # Don't put anything in this directory yourself; store your static files
  10. # in apps' "static/" subdirectories and in STATICFILES_DIRS.
  11. # Example: "/home/media/media.lawrence.com/static/"
  12. STATIC_ROOT = '/home/lilo/textImageSite/imageSite/static/'
  13. # URL prefix for static files.
  14. # Example: "http://media.lawrence.com/static/"
  15. STATIC_URL = '/static/'
  16. # Additional locations of static files
  17. STATICFILES_DIRS = (
  18. # Put strings here,like "/home/html/static" or "C:/www/django/static".
  19. # Always use forward slashes,even on Windows.
  20. # Don't forget to use absolute paths,not relative paths.
  21. )

我的Nginx配置文件(位于/ etc / Nginx / sites-enabled):

  1. server {
  2. listen 80;
  3. server_name xxx.xx.xx.xx; #the actual IP of the server; it has a public IP address
  4. access_log /home/lilo/textImageSite/access.log;
  5. error_log /home/lilo/textImageSite/error.log;
  6. location /static {
  7. root /home/lilo/textImageSite/imageSite;
  8. }
  9. location / {
  10. proxy_pass http://127.0.0.1:8888;
  11. }
  12. }

我的gunicorn_conf文件

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

现在在我的模板中,这就是我访问图像的方式:

以下是生成的HTML的样子:

对不起文字墙,但我无法弄清楚我的设置有什么问题……

事实证明我修复了自己的问题…误解了Nginx是如何工作的. :d

  1. server {
  2. listen 1234; //port that Nginx listens on
  3. server_name xxx.xx.xx.xx; #the actual IP of the server; it has a public IP address
  4. access_log /home/lilo/textImageSite/access.log;
  5. error_log /home/lilo/textImageSite/error.log;
  6. location /static {
  7. root /home/lilo/textImageSite/imageSite;
  8. }
  9. location / {
  10. proxy_pass http://127.0.0.1:8888; //the port that Gunicorn uses
  11. }
  12. }

所以在我的情况下,如果我在端口8888上运行我的Gunicorn实例,那么转到xxx.xxx.xx.x:8888 / textImageSite将加载页面,但没有任何静态内容.如果我使用xxx.xxx.xx.x:1234访问它,那么页面将加载静态内容(图像,css样式表等).这是我第一次使用Gunicorn和Nginx(并且第一次写一个Django应用程序)所以希望这会帮助那些困惑的人:)

猜你在找的Nginx相关文章