Nginx   发布时间:2022-05-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我如何使用nginx和gunicorn为Django应用程序提供静态文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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/tex@R_739_5023@Site/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/tex@R_739_5023@Site/access.log;
error_log /home/lilo/tex@R_739_5023@Site/error.log;

LOCATIOn /static {
    root /home/lilo/tex@R_739_5023@Site/imageSite;
}

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

我的gunicorn_conf文件:

bind = "0.0.0.0:8888"
logfile = "/home/lilo/tex@R_739_5023@Site/gunicorn.log"
workers = 3

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


以下是生成的HTML的样子:


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

事实证明我修复了自己的问题…误解了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/tex@R_739_5023@Site/access.log;
error_log /home/lilo/tex@R_739_5023@Site/error.log;

LOCATIOn /static {
    root /home/lilo/tex@R_739_5023@Site/imageSite;
}

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

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

大佬总结

以上是大佬教程为你收集整理的我如何使用nginx和gunicorn为Django应用程序提供静态文件?全部内容,希望文章能够帮你解决我如何使用nginx和gunicorn为Django应用程序提供静态文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:现在