【问题标题】:django nginx static files 404django nginx静态文件404
【发布时间】:2014-06-07 05:27:09
【问题描述】:

这是我的设置:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'

这是我的 nginx 配置:

server {
    server_name 77.241.197.95;

    access_log off;

    location /static/ {
        alias /home/django-projects/tshirtnation/staticfiles/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

我运行了python manage.py collectstatic,它复制了所有静态文件。我用 gunicorn_django --bind:my-ip:8001 运行我的服务器,除了静态文件之外,一切似乎都在工作。

编辑: 我跑了

sudo tail /var/log/nginx/error.log

似乎没有找不到静态文件的错误:/

【问题讨论】:

  • 首先,将你的gunicorn_django命令改为gunicorn_django --bind=127.0.0.1:8001,因为当你使用外部ip运行它时,它会接受来自outside的连接。
  • @OmidRaha 现在它根本不起作用:/
  • 您是否在 setting.py 文件中设置了 debug=False?
  • 该 IP 由 Apache 服务器提供服务;你的 nginx 前面有 Apache 反向代理吗?如果不是,我希望在启动 nginx 时出现“地址已在使用”错误。

标签: python django nginx django-deployment


【解决方案1】:

我遇到了这个问题,并通过将这些行添加到项目 urls.py 文件中来解决它:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = [
   ...
] += staticfiles_urlpatterns()

还有另一个原因,这有点问题,它在/etc/nginx/nginx.conf的nginx主配置文件中。确保此文件包含以下行:

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

【讨论】:

    【解决方案2】:

    settings.py:

    ALLOWED_HOSTS = ['*']
    
    STATIC_URL = '/static/'
    STATIC_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/static/'
    
    MEDIA_ROOT = '/home/calosh/PycharmProjects/Proyecto_AES/media/'
    MEDIA_URL = '/media/'
    

    在 nginx 配置中(/etc/nginx/sites-enabled/default)

    server {
        server_name localhost;
    
        access_log off;
    
        location /static/ {
            alias /home/calosh/PycharmProjects/Proyecto_AES/static/;
        }
    
        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;
                add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
    }
    

    然后重启nginx服务器:

    sudo service nginx restart
    

    然后运行 ​​gunicorn:

    gunicorn PFT.wsgi
    

    在 localhost 或整个本地网络(端口 80)上提供应用程序。

    http://127.0.0.1/
    

    【讨论】:

    • 你也可以补充一些解释吗?
    【解决方案3】:

    尝试在 nginx 设置中删除斜线,然后是静态,例如它应该是 "/static" 而不是 "/static/",如果您的设置正常,请尝试在本地计算机上重新加载服务器并尝试在远程计算机上重新启动。我也遇到过类似的情况,但重启机器后,问题就解决了。

    【讨论】:

      【解决方案4】:

      在 settings.py 中试试这个:

      import os
      ROOT_PATH = os.path.dirname(__file__)
      BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
      
      STATIC_URL = '/static/'
      STATIC_ROOT = os.path.join(ROOT_PATH,'static/')
      STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
      

      和 nginx 中的配置文件(或您的服务器设置):

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

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,并且能够通过从 /static/ 位置删除尾随 / 来修复我的 nginx 配置。

        location /static {  # "/static" NOT "/static/"
            # ...
        }
        

        【讨论】:

          【解决方案6】:

          尝试将^~ 前缀修饰符添加到您的静态位置以跳过检查正则表达式:

          location ^~ /static/ {
              alias /home/django-projects/tshirtnation/staticfiles/;
          }
          

          【讨论】:

            【解决方案7】:

            将 STATIC_URL 与域一起使用。这很重要!

            【讨论】:

              【解决方案8】:

              在你的 settings.py 中,输入:

              STATICFILES_FINDERS = (
              'django.contrib.staticfiles.finders.FileSystemFinder',
              'django.contrib.staticfiles.finders.AppDirectoriesFinder'
              )
              STATIC_ROOT = "/home/django-projects/tshirtnation/staticfiles/"
              STATIC_URL = '/static/'
              

              你不需要这个:

              STATICFILES_DIRS = ...
              

              【讨论】:

                【解决方案9】:

                我的看起来像这样,而且它有效:

                STATICFILES_DIRS = (
                    os.path.join(os.path.dirname(__file__), '..', 'static'),
                )
                

                而且在nginx的配置中,我的位置/static/在位置/上面这样,

                location /static {
                    //
                }
                
                # Finally, send all non-media requests to the Django server.
                location / {
                    //
                }
                

                还有一件事,我不知道这是否重要,但我确实在服务器中有一个“监听”{}。我不确定它是否有帮助。

                【讨论】:

                  【解决方案10】:

                  您的问题是始终使用 location / 块,即使在 location /static/ 之后也是如此,所以一切都将是 proxy_passed。
                  这里的解决方案是使用一些try_files 魔法。

                  server {
                      server_name 77.241.197.95;
                  
                      access_log off;
                  
                      location / {
                          try_files $uri @django;
                      }
                  
                      location /static/ {
                          alias /home/django-projects/tshirtnation/staticfiles/;
                          try_files $uri =404;
                          # here we use =404 because there is no need to pass it to gunicorn.
                      }
                  
                      location @djago {
                          proxy_pass http://127.0.0.1:8001;
                          proxy_set_header X-Forwarded-Host $server_name;
                          proxy_set_header X-Real-IP $remote_addr;
                          add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
                      }
                  }
                  

                  【讨论】:

                  • Nginx 匹配最具体的位置,所以这不是必需的。
                  【解决方案11】:

                  检查这些东西

                  1 静态older是否可以被nginx访问,我是说文件夹权限。

                  2 或者这样做

                  替换这个:

                  STATIC_ROOT = '/home/django-projects/tshirtnation/staticfiles'
                  

                  有了这个

                  STATIC_ROOT = ''

                  并在设置中添加它

                  STATICFILES_DIRS = (
                       '/home/django-projects/tshirtnation/staticfiles/',
                  )
                  

                  不要忘记重新加载 nginx 服务器。

                  希望这行得通。

                  【讨论】:

                    【解决方案12】:

                    我认为浏览器会尝试在以下位置找到您的静态:

                    http://127.0.0.1:8001/static/
                    

                    虽然 nginx 默认工作在 80 端口上。

                    您需要在 nginx 配置中定义 8001 端口或在 80 端口上运行 django 服务器。

                    【讨论】:

                      猜你喜欢
                      • 2017-06-05
                      • 2021-01-12
                      • 2016-09-24
                      • 2015-11-30
                      • 2021-08-08
                      • 2021-08-12
                      • 2012-09-30
                      • 2020-10-17
                      • 1970-01-01
                      相关资源
                      最近更新 更多