9

现在,我正在尝试遵循本教程:

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

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

# 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.
)

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

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;
}
}

我的 gunicorn_conf 文件:

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

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

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

下面是生成的 HTML 的样子:

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

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

4

1 回答 1

20

原来我解决了自己的问题......误解了 Nginx 的工作原理。: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 
}
}

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

于 2012-07-12T00:48:53.983 回答