简体   繁体   English

Docker-Compose + Django + PostgreSQL + uwsgi + Nginx找不到404

[英]Docker-Compose + Django + PostgreSQL + uwsgi + Nginx getting 404 not found

I have recently made some service for practice. 我最近为练习提供了一些服务。 I used Docker-Compose, Django, PostgreSQL, uwsgi, Nginx. 我使用了Docker-Compose,Django,PostgreSQL,uwsgi,Nginx。 I try to run in my local machine. 我尝试在本地计算机上运行。 Here is what my site like when I access it on my server. 这是我在服务器上访问网站时的样子。 在此处输入图片说明

Here is part of about static my settings.py file in the Django 'django_project' root directory. 这是关于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',
)

Here is my Docker-compose file 这是我的Docker-compose文件

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  

Here is my Dockerfile for Django 这是我的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

Here is my Dockerfile for Nginx 这是我的Nginx Dockerfile

FROM nginx:alpine

RUN rm /etc/nginx/conf.d/default.conf
COPY ./sites-enabled/nginx.conf /etc/nginx/conf.d/default.conf

Here is my nginx.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;
    }
}

Here is my uwsgi.ini 这是我的uwsgi.ini

[uwsgi]
chdir = /var/www/refrigerator/
module = refrigerator_manager.wsgi

socket =:8000

chmod-socket = 666

vacuum = true

master = true

Here is my urls.py 这是我的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'),
]

I think the static file is not connected between the nginx container and the Django container. 我认为静态文件未在nginx容器和Django容器之间连接。 I tried to fix the error for a few days by referring to similar examples of google and StackOverflow, but it is not working well and I need help. 我尝试通过参考Google和StackOverflow的类似示例来修复该错误几天,但它无法正常工作,我需要帮助。 Help! 救命!

In your urls.py there is a line as url(r'^.*', api_404, name='api_404'), , So,whenever you access to your root (ie, www.mydomain.com/ ), the url dispatcher takes you to the api_404 view. 在您的urls.py有一行作为url(r'^.*', api_404, name='api_404'), ,,因此,无论何时您访问root (即www.mydomain.com/调度程序将您带到api_404视图。

I'm not sure what's inside the api_404 view but as the view name indicates, it may raise a Http404 error. 我不确定api_404视图内部的api_404但是正如视图名称所示,它可能会引发Http404错误。

Conclusion 结论
This is not a problem of nginix or docker etc, just try to access a valid api 这不是nginix或docker等的问题,只需尝试访问有效的 api

This code snippet will re-produce the behaviour 此代码段将重现该行为

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')
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM