【问题标题】:Gunicorn on lighttpd - changing base URL for static files from localhost:8080lighttpd 上的 Gunicorn - 从 localhost:8080 更改静态文件的基本 URL
【发布时间】:2015-01-15 08:15:15
【问题描述】:

现在我正在处理 Django 项目,这是我第一次通过 gunicorn 在服务器 (Lighttpd) 上运行 Django 应用程序。问题是 - 我像这样使用 gunicorn 运行应用程序:

$PYTHON $MANAGEPATH run_gunicorn -D -b 127.0.0.1:18002 -p $GUNICORN_PIDPATH --log-file $GUNICORN_LOGPATH

Django settings.py 的一部分:

from __future__ import absolute_import

import os
from socket import gethostname

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
HOSTNAME = gethostname()

def make_path(path):
    return os.path.join(BASE_DIR, path)

# 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: "/var/www/example.com/static/"
STATIC_ROOT = make_path('static_root')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

在 /etc/lighttpd/vhost.d/my_conf.conf 我对非静态文件有很好的重定向:

$SERVER["socket"] == ":82" {
    server.document-root = "/home/mefioo/public_html/generator/static"
    $HTTP["url"] !~ "\.(js|css|gif|jpg|png|ico|txt|swf|html|htm|svg|xlsx)$" {
        proxy.server = ("" => ( "localhost" => (
        "host" => "127.0.0.1",
        "port" => 18002,
        "fix-redirects" => 1
        ) ) )
    }
}

想法 - .js 或 .css 等静态文件位于

/home/mefioo/public_html/generator/static

(它是 Django 应用程序中 static_root 目录的代理)并且应该可以从 url 中获得,例如

my_domain.com:82/my_file.js

应用程序的其余部分,所有网址如下:

my_domain.com:82/url

应该寻找这样的 django 应用程序:

127.0.0.1:18002/url

因为 lighttpd conf。

问题是 - 当我尝试访问我的静态文件时,我得到了 404,因为应用程序正在寻找

localhost:8080/my_file.js

而不是

my_domain.com:82/my_file.js

我不明白为什么,尤其是当我有该应用程序的两个实例时,一个在我自己的服务器上(第二台 PC,不工作),一个在外部 VPS 上(按预期工作)。我是以错误的方式运行 gunicorn,还是在 lighttpd 设置中遗漏了什么?

【问题讨论】:

    标签: django lighttpd gunicorn static-files


    【解决方案1】:

    我也是 django/gunicorn 的新手,但这是我配置应用程序的方式:

    settings.py 中的设置

    STATIC_ROOT = "/path to static files"
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (
       os.path.join(BASE_DIR, 'static'),
    )
    

    我没有使用 lighttpd 而是使用 nginx,这是配置:

    server{
            server_name example.com www.example.com;
    
            access_log off;
    
    location /static {
            autoindex on;
            alias /path_to_static_files;
        }
            location / {
                    proxy_pass http://127.0.0.1:8000;
                    proxy_set_header X-Forwarded-Host $server_name;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Host $host;
                    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
            }
        }
    

    之后,我们必须收集配置目录中的静态文件。这是通过:python3 manage.py collectstatic

    所有文件都应该复制到正确的目录中,lightppd 或 nginx 可以为它们提供服务,而 gunicorn 执行 django/python - 部分

    【讨论】:

    • 我当然做了python manage.py collectstatic。它们都被复制到适当的目录中。如果我手动粘贴my_domain.com:82/my_file.js 文件就在那里,问题是,我的应用程序正试图在其他地方找到它...
    猜你喜欢
    • 2017-07-19
    • 2018-12-28
    • 2018-11-29
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 2012-12-06
    相关资源
    最近更新 更多