Django、Apache2 和 Nginx:加载时间长

Django, Apache2 and Nginx : Long loading time

我使用 Django 作为我网站的技术。假设我使用的域名是 example.com.

我使用 Apache2 作为我的主要 Web 服务器,使用 Nginx 来提供静态文件。

我首先安装了Apache并进行了测试。它运行完美(没有 css 和其他静态文件,只有 html)。

然后我安装了Nginx(我今天之前从来没有用过),这就是我遇到问题的地方。事实上,当我想访问我的网站时,我得到的 html 和以前一样好。但是加载继续并且静态文件没有恢复。 我在 Mozilla 终端中没有看到任何错误。

这里是"Site-available"配置:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin contact@example.com
    DocumentRoot /var/www/my_site

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

apache.conf :

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

#<Directory /srv/>
#   Options Indexes FollowSymLinks
#   AllowOverride None
#   Require all granted
#</Directory>


# Configuration for Django application
WSGIScriptAlias / /var/www/my_site/site/wsgi.py
WSGIPythonPath /var/www/my_site

<Directory var/www/my_site/my_site>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>   

# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives.  See also the AllowOverride
# directive.
#
AccessFileName .htaccess

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

# FQDN
ServerName example.com

我的 Nginx 配置:

server {   
     listen   8000;   
     server_name  localhost;

     access_log  /var/log/nginx/aa8000.access.log;    
     error_log  /var/log/nginx/aa8000.error.log;    

       location / {   
           index  index.html index.htm;    
       }    

     location ^/static/ {    
        autoindex on;    
        root   /var/www/my_site/allstatic/;    
     }    
 }

并完成静态文件的 Django 设置:

STATIC_URL = 'http://example.com:8000/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    '/var/www/my_site/static/',
]
STATIC_ROOT = '/var/www/my_site/allstatic'

Nginx 错误日志甚至访问日志中都没有。

Edit :最后我只使用 apache 并且它有效。 我在 apache conf 中添加了以下内容:

Alias /favicon.ico /var/www/my_site/allstatic/favicon/favicon.ico

Alias /media/ /var/www/my_site/media/
Alias /static/ /var/www/my_site/allstatic/

<Directory /var/www/my_site/allstatic>
Require all granted
</Directory>

<Directory /var/www/my_site/media>
Require all granted
</Directory>

我建议您将静态(如果需要,媒体)部分添加到您的 apache 文件中,就像您 apache.conf

https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files 处的示例一样

您托管的网站不止一个吗?如果不是,请不要使用 sites-available;只需关闭虚拟主机并且不要在 sites-enabled 目录中启用任何内容。