uWSGI和nginx从Django提供静态服务

时间:2017-01-05 12:42:40

标签: django nginx

我已经设置了一个django项目来为nginx提供静态服务,并且还使用了uwsgi。在debug = True中运行时,我可以在主页上查看演示静态文件,并且管理静态正在加载但是当我设置debug = False时无法加载静态文件,它返回404.我已设置该文件夹的权限为755,但它没有解决问题。

这里是设置文件中的设置

STATIC_URL = '/static/'
MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'kb/media')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'kb/static/'),]

nginx配置文件kb_nginx.conf

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/ubuntu/kb/kb.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name kenyabuzz.nation.news; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/ubuntu/kb/kb/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/kb/kb/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/ubuntu/kb/kb/uwsgi_params; # the uwsgi_params file you installed
    }
}

符号链接为sudo ln -s /home/ubuntu/kb/kb/kb_nginx.conf /etc/nginx/sites-enabled/

使用uwsgi --http :8000 --socket kb.sock --module kb.wsgi --chmod-socket=664

运行它

更新 这是uwsgi的运行时结果:

*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 16432, cores: 1)
[pid: 16432|app: 0|req: 1/1] 197.232.12.165 () {42 vars in 774 bytes} [Fri Jan  6 07:27:49 2017] GET / => generated 152 bytes in 11 msecs (HTTP/1.1 200) 3 headers in 102 bytes (2 switches on core 0)
[pid: 16432|app: 0|req: 2/2] 197.232.12.165 () {42 vars in 781 bytes} [Fri Jan  6 07:27:49 2017] GET /static/img/girl.png => generated 93 bytes in 1 msecs (HTTP/1.1 404) 3 headers in 94 bytes (1 switches on core 0)

0 个答案:

没有答案