我最近为练习提供了一些服务。我使用了Docker-Compose,Django,PostgreSQL,uwsgi,Nginx。我尝试在本地计算机上运行。 这是我在服务器上访问网站时的样子。
这是关于Django'django_project'根目录中我的settings.py文件静态的一部分。
# STATIC FILE CONFIGURATION
STATIC_DIR = SRC_DIR.path('static')
STATIC_ROOT = str(SRC_DIR('staticfiles'))
STATIC_URL = '/static/'
STATICFILES_DIRS = [str(SRC_DIR('static'))]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
这是我的Docker组成文件
version: '2'
services:
nginx:
restart: always
build: ./nginx
ports:
- "80:80"
volumes_from:
- refrigerator
links:
- refrigerator:refrigerator
depends_on:
- refrigerator
refrigeratordb:
image: "postgres:9.6.1"
container_name: refrigeratordb
expose:
- "5432"
environment:
POSTGRES_PASSWORD: "something"
POSTGRES_USER: "something"
POSTGRES_DB: "something"
refrigerator:
build: .
container_name: refrigeratormanager
depends_on:
- refrigeratordb
links:
- refrigeratordb
volumes:
- .:/var/www/refrigerator
- /usr/src/app/static
command: uwsgi --ini ./refrigerator_manager_uwsgi.ini
stdin_open: true
tty: true
这是我的Django Dockerfile
FROM centos/python-36-centos7:latest
USER root
ADD . /var/www/refrigerator/
WORKDIR /var/www/refrigerator
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 8000
这是我的Nginx Dockerfile
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY ./sites-enabled/nginx.conf /etc/nginx/conf.d/default.conf
这是我的nginx.conf
server {
listen 80;
server_name sodwkdrhdjemals.com www.sodwkdrhdjemals.com;
charset utf-8;
location /static {
alias /static;
}
location / {
include uwsgi_params; # the uwsgi_params file you installed
uwsgi_pass refrigerator:8000;
}
location = /favicon.ico {
return 404;
log_not_found off;
access_log off;
}
}
这是我的uwsgi.ini
[uwsgi]
chdir = /var/www/refrigerator/
module = refrigerator_manager.wsgi
socket =:8000
chmod-socket = 666
vacuum = true
master = true
这是我的urls.py
urlpatterns = [
url(settings.ADMIN_URL, admin.site.urls),
url(r'^rest-swagger/', schema_view),
url(r'^vegetables/?$', VegetablesListCreateAPIView.as_view(),
name='query_for_vegetables_list'),
url(r'^vegetables/(?P<vegetable_id>[0-9]+)/?$',
VegetableRetrieveUpdateDeleteView.as_view(),
name='retrieve_update_delete_vegetable_item'),
url(r'^forks/?$', ForksListCreateAPIView.as_view(),
name='query_for_forks_list'),
url(r'^forks/(?P<fork_id>[0-9]+)/?$',
ForkRetrieveUpdateDeleteView.as_view(),
name='retrieve_update_delete_fork_item'),
url(r'^.*', api_404, name='api_404'),
]
我认为静态文件未在nginx容器和Django容器之间连接。我尝试通过参考Google和StackOverflow的类似示例来修复该错误几天,但它无法正常工作,我需要帮助。救命!
答案 0 :(得分:0)
在您的urls.py
中,一行为url(r'^.*', api_404, name='api_404'),
,因此,每当您访问root
(即www.mydomain.com/
)时,URL调度程序都会将您带到api_404
视图。
我不确定api_404
视图中的内容,但是正如视图名称所示,它可能会引发Http404
错误。
结论
这不是nginix或docker等的问题,只需尝试访问有效 api
此代码段将重现行为
from rest_framework.decorators import api_view
from django.http import Http404
@api_view()
def api_404(request):
raise Http404
urlpatterns = [
url(r'^.*', api_404, name='api_404')
]