【问题标题】:Nginx only partially serves static files for django appNginx 只为 django 应用程序提供部分静态文件
【发布时间】:2014-12-02 11:18:42
【问题描述】:

首先,我想声明我已阅读并尝试了我在该主题上找到的所有 SO 问题的解决方案。
没有任何帮助,而且我的问题与平常的问题完全不同。

我在我的开发计算机上安装了 django 项目(在 runserver 和 gunicorn 上),这意味着提供静态文件。
值得一提的是,我最近由于硬件问题将我的开发环境迁移到了新系统(这里的要点是我最近安装了这个项目)。

当我尝试在另一个系统上安装它时(我的安装脚本已为其编写的 ubuntu 13.10 的全新安装)我遇到了一个奇怪的问题,即 并非所有静态文件都由nginx.
'Not all' 表示提供了一些 css 文件,一些没有提供; js也一样。

是的,所有这些文件都在同一个静态目录中,是的,它们实际上都在那里。 此外,这可能不是配置问题,因为在这两种情况下(我的开发机器一切都运行良好,而这个测试机器没有)存在 完全相同 配置。 文件的特权也是一样的。 nginx 的日志中没有任何内容。

它一定是一些非常愚蠢但充满异国情调的东西,或者我不知道......巫毒? 我的意思是,如果 nginx 不提供任何服务,我会管理 - 这显然是一些错误 - 但是为什么一个文件被提供而另一个来自完全相同的地方却没有? 同一个设置怎么可能为一个系统上的所有文件提供服务,而在另一个系统上只提供很少的文件?

我没有在此处包含任何配置等,因为如上所述,我认为它们在这里不是问题;但是,如果有人觉得需要向自己保证我渴望发布它,只需说一句话。

在此先感谢大家,我请求帮助,我绝望了,并不为此感到羞耻。
已经三天了,截止日期不会到任何地方......

我的/etc/nginx/nginx.conf(基本默认一个):

user www-data;                                                                  
worker_processes 4;                                                             
pid /run/nginx.pid;                                                             

events {                                                                        
    worker_connections 768;                                                 
    # multi_accept on;                                                      
}                                                                               

http {                                                                          

    ##                                                                      
    # Basic Settings                                                        
    ##                                                                      

    sendfile on;                                                            
    tcp_nopush on;                                                          
    tcp_nodelay on;                                                         
    keepalive_timeout 65;                                                   
    types_hash_max_size 2048;                                               
    # server_tokens off;                                                    

    # server_names_hash_bucket_size 64;                                     
    # server_name_in_redirect off;                                          

    include /etc/nginx/mime.types;                                          
    default_type application/octet-stream;                                  

    ##                                                                      
    # Logging Settings                                                      
    ##                                                                      

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

    ##                                                                      
    # Gzip Settings                                                         
    ##                                                                      

    gzip on;                                                                
    gzip_disable "msie6";                                                   

    # gzip_vary on;                                                         
    # gzip_proxied any;                                                     
    # gzip_comp_level 6;                                                    
    # gzip_buffers 16 8k;                                                   
    # gzip_http_version 1.1;                                                
    # gzip_types text/plain text/css application/json application/x-javascript text/xmlapplication/xml applicatio

    ##                                                                      
    # nginx-naxsi config                                                    
    ##                                                                      
    # Uncomment it if you installed nginx-naxsi                             
    ##                                                                      

    #include /etc/nginx/naxsi_core.rules;   

    ##                                                                      
    # nginx-passenger config                                                
    ##                                                                      
    # Uncomment it if you installed nginx-passenger                         
    ##                                                                      

    #passenger_root /usr;                                                   
    #passenger_ruby /usr/bin/ruby;                                          

    ##                                                                      
    # Virtual Host Configs                                                  
    ##                                                                      

    include /etc/nginx/conf.d/*.conf;                                       
    include /etc/nginx/sites-enabled/*;                                     
}                                                                               


#mail {                                                                         
#       # See sample authentication script at:                                  
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript             
#                                                                               
#       # auth_http localhost/auth.php;                                         
#       # pop3_capabilities "TOP" "USER";                                       
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";                              
#                                                                               
#       server {                                                                
#               listen     localhost:110;                                       
#               protocol   pop3;                                                
#               proxy      on;                                                  
#       }                                                                       
#                                                                               
#       server {                                                                
#               listen     localhost:143;                                       
#               protocol   imap;                                                
#               proxy      on;                                                  
#       }                                                                       
#}         

启用站点的站点配置:

server {                                                                        
listen 80;                                                                  
server_name webapp.org;                                                     

access_log /var/log/nginx_access.log;                                       
error_log /var/log/nginx_error.log;                                         

location /static {                                                          
    root /home/myuser/app;                                                 
}                                                                           

location /sse {                                                             
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            
    proxy_set_header Host $http_host;                                       
    proxy_redirect off;                                                     
    proxy_buffering off;                                                    
    proxy_next_upstream error;                                              
    proxy_read_timeout 600;                                                 

    proxy_pass http://127.0.0.1:8888;                                       
}                                                                           

location /pacs {                                                            
    proxy_pass http://127.0.0.1:8080/;                                      
}                                                                           

location / {                                                                
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            
    proxy_set_header Host $http_host;                                       
    proxy_redirect off;                                                     

    proxy_pass http://127.0.0.1:8888;                                       
}                                                                           
}                                 

【问题讨论】:

  • 两个问题。 1)“未送达”是什么意思?当被要求提供该文件时,nginx 会返回什么? 2)“nginx的日志中什么都没有”是什么意思?什么都没有?
  • 是的,发布你的 nginx 配置和日志(如果有的话)肯定会帮助这里的人们弄清楚可能发生的事情。
  • 1) 它返回 404 2) 什么都没有...
  • 您不能在日志中得到“什么都没有”...请参阅nginx.com/resources/admin-guide/logging-and-monitoring 以启用 nginx 的访问和错误日​​志。
  • 好吧,显然我可以:/(或者我有什么问题,我发布了配置)

标签: django nginx static-files


【解决方案1】:

您正在直接访问 Django 开发服务器,该服务器正在为您的一些静态文件提供服务。如果没有将它们签入到版本控制中,您可能会将其中一些留在后面,我不知道...

无论如何,你必须访问Nginx,即在地址栏中输入:

http://your.server.com

而不是

http://your.server.com:8888

这样你应该开始在 Nginx 访问日志中看到一些东西。

另外,请记住运行 collectstatic: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django-admin-collectstatic

【讨论】:

  • 非常感谢您的耐心和帮助!
猜你喜欢
  • 2016-12-23
  • 2023-04-07
  • 1970-01-01
  • 2016-01-28
  • 2021-12-02
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多