1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Nginx, Django, Docker-Compose CORS Policy Problem

Discussão em 'Angular' iniciado por Pacs1212, Agosto 12, 2025.

  1. Pacs1212

    Pacs1212 Guest

    I'm trying to build my first docker-compose for a project at my university. We have an angualt frontend running on a naginx server. A manager to connect front- and backend and some backend stuff. The connection from the manager to the backend is no problem. The manager runs on a django server and the backend on flask. The problem is the connection between the nginx server and the manager on django. Every time i try to do an REST request the error show up:

    Access to XMLHttpRequest at 'manager:8000/training-data' from origin 'http://localhost:8080' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https#


    I set the header in the nginx.conf, and allowed cors in the django server as you can see in the code snippet below:

    nginx.conf:

    user nginx;
    worker_processes 1;

    error_log /var/log/nginx/error.log warn;
    pid /var/run/nginx.pid;


    events {
    worker_connections 1024;
    }


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

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 65;

    #gzip on;

    server {
    listen 80;
    server_name localhost;

    location / {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Accept,Content-Type';
    }
    }

    include /etc/nginx/conf.d/*.conf;
    }


    Django setting.py:

    from corsheaders.defaults import default_headers
    ALLOWED_HOSTS = ['127.0.0.1','manager','0.0.0.0','localhost']


    INSTALLED_APPS = [
    .
    .
    .
    .
    'corsheaders',
    'py-backend'
    ]

    MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.BrokenLinkEmailsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware'
    ]

    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ALLOW_CREDENTIALS = True

    CORS_ALLOW_HEADERS = default_headers + (
    'Access-Control-Allow-Origin',
    )


    In the angualr web app I defiend the apiUrl = "http://manager", baucaus thats the service name in the docker-compose file.

    I don't know if maybe something in my Dockerfiles or docker-compose are wrong so here they are:

    docker-compse.yml

    version: '3'

    services:
    couchdb:
    image: couchdb:2.3.1
    volumes:
    - ./database/data:/home/couchdb/data
    ports:
    - 5984:5984

    manager:
    build: ./python/py-backend
    volumes:
    - ./python/py-backend:/usr/src/app
    command: ./dockerScript/wait-for-it.sh couchdb:5984 -t 15 -- python manage.py runserver 0.0.0.0:8000
    restart: on-failure
    ports:
    - 8000:8000
    depends_on:
    - couchdb

    spacyserver:
    build: ./python/frameworks/SpacyServer
    volumes:
    - ./python/frameworks/SpacyServer:/usr/src/app
    ports:
    - 5000:5000
    command: python SpacyServer.py
    restart: on-failure
    depends_on:
    - couchdb
    - manager

    nginxserver:
    build: ./angular
    volumes:
    - ./angular/deep-tex-rex/dist/deep-tex-rex:/usr/share/nginx/html
    ports:
    - 8080:80
    restart: on-failure


    nginx/angualr Dockerfile

    FROM nginx
    COPY ./deep-tex-rex/dist/deep-tex-rex /usr/share/nginx/html
    COPY ./deep-tex-rex/src/assets/server-config/nginx.conf /etc/nginx/nginx.conf
    WORKDIR /usr/share/nginx/html


    manager/Django Dockerfile

    FROM python:3.7.6
    COPY . /usr/src/app
    WORKDIR /usr/src/app
    RUN pip install -r requirements.txt


    nginx/angualr requirements.txt

    django==3.0.2
    django-cors-headers==3.2.1
    keras==2.3.1
    pycouchdb==1.14
    pyexcel==0.5.15
    pandas==0.25.3
    spacy==2.2.3


    I hope someone can help me I'm getting really frustrated at the moment. I spended now so many houres but I can't fix the problem.

    Continue reading...

Compartilhe esta Página