Ubuntu服务器,Django 1.11.4,gunicorn,nginx找不到css文件

时间:2017-08-09 13:01:36

标签: django nginx static gunicorn

静态css有问题。我已经收集了静电。

如果运行

,Css工作正常

./ manage.py runserver 0.0.0.0:8000

但如果运行

则无法找到

gunicorn --bind 0.0.0.0:8000 hotel_main.wsgi:application

与Dajango admin相同的情况

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = "/opt/static/"
STATICFILES_DIRS = [
        ('static', os.path.join(BASE_DIR, 'hotel_site/static'),),
        ('static', os.path.join(BASE_DIR, 'static'),),
]

Urls.py

from django.conf.urls import include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
    url(r'^', include('hotel_site.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Nginx文件

upstream 78.155.199.17 {
    server localhost:8000 fail_timeout=0;
}
server {
        listen 80;
        server_name 78.155.199.17;
        return 301 78.155.199.17$request_uri;
}
server {
        listen 80;
        server_name 78.155.199.17;
        location /static/ {
                root /opt/;
        }
        location /media/ {
                root /root/;
        }
        location / {
                proxy_set_header X-Forwarded-For 
                $proxy_add_x_forwarded_for;
                proxy_redirect off;
                proxy_pass 78.155.199.17;
        }
}

项目树

opt/
└─ static/
   ├─admin/
   ├─ckeditor/
   └─static/
     └─foundation/

root/
└─ ramn_hotel/

1 个答案:

答案 0 :(得分:0)

最近做了here。它按原样运作。