首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django,Apache2和Nginx :很长的加载时间

Django,Apache2和Nginx :很长的加载时间
EN

Stack Overflow用户
提问于 2017-01-23 22:43:24
回答 1查看 109关注 0票数 1

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

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

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

然后我安装了Nginx (我以前从未使用过它),这就是我遇到问题的地方。事实上,当我想访问我的网站时,我会像以前一样得到html。但是加载仍在继续,静态文件未被恢复。我在Mozilla终端上没有看到任何错误。

以下是“站点可用”配置:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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配置:

代码语言:javascript
复制
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设置:

代码语言:javascript
复制
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错误日志或访问日志中没有任何内容。

编辑:最后,我只使用,它可以工作。我在apache conf中添加了以下内容:

代码语言:javascript
复制
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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-24 04:32:22

我建议您在apache文件中添加静态部分(如果需要,也可以添加媒体部分),如https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files中的apache.conf示例。

你是否主持了多个网站?如果没有,请不要使用可用的站点;只需关闭虚拟主机,并且不启用启用站点目录中的任何内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41817004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档