代码之家  ›  专栏  ›  技术社区  ›  LaurensVijnck

码头化Django Rest+Angular项目中阻塞的CORS

  •  0
  • LaurensVijnck  · 技术社区  · 7 年前

    今天我决定将现有的django rest+angular应用程序进行Dockerize。尽管CORS请求被阻止,但该网站仍按原样显示。

    访问位于' http://localhost:8000/branches/1 “起源” http://localhost:3000 '已被CORS策略阻止:请求的资源上不存在'access control allow origin'头。

    角度应用程序使用nginx进行固定。我在用 this 教程正在进行中(docker+compose文件如下所示)。

    奇怪的是,我以前有过这个问题,通过使用 Djang Cors标题 包裹。随着 CORS_ORIGIN_ALLOW_ALL = True 这似乎不再解决问题了。

    这是否与应用程序现在在容器中运行的事实有关?我将为项目提供dockerfiles以及下面的docker compose文件。但不确定它们是否相关。

    角度项目的Dockerfile:

    FROM tiangolo/node-frontend:10 as build-stage
    WORKDIR /app
    
    COPY package*.json /app/
    RUN npm install
    
    COPY ./ /app/
    ARG configuration=production
    RUN npm run build -- --output-path=./dist/out --configuration $configuration
    
    # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
    FROM nginx:1.15
    COPY --from=build-stage /app/dist/out/ /usr/share/nginx/html
    
    # Copy the default nginx.conf provided by tiangolo/node-frontend
    COPY --from=build-stage /nginx.conf /etc/nginx/conf.d/default.confenter
    

    Django Rest项目的Dockerfile:

    # Pull base image
    FROM python:3.7
    
    # Set environment varibles
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1
    
    # create root directory
    RUN mkdir /web
    
    # set working directory
    WORKDIR /web
    
    # Coppy current directory contents into the container
    ADD . /web/
    
    # Install any needed packages in requirements.txt
    RUN pip install -r requirements.txt
    

    以及docker-compose.yml文件:

    version: '3'
    
    services:
      db:
        image: mysql:5.7
        restart: always
        container_name: db
        environment:
          - MYSQL_HOST=localhost
          - MYSQL_PORT=3306  # cannot change this port to other number
          - MYSQL_ROOT_HOST=%
          - MYSQL_DATABASE=test
          - MYSQL_USER=belter
          - MYSQL_PASSWORD=belter_2017
          - MYSQL_ROOT_PASSWORD=123456_abc
        ports:
          - '3302:3306'
    
      web:
        build: ./orca/
        restart: always
        command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
        container_name: web
        volumes:
          - ./orca:/code
        ports:
          - '8000:8000'
        depends_on:
          - db
    
      angular:
        build: ./ng-orca-new/
        container_name: angular
        ports:
          - '3000:80'
    

    事先谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   LaurensVijnck    7 年前

    问题是由于nginx阻塞了请求。在该层再次添加头文件,生成以下nginx.conf:

    server {
      listen 80;
      location / 
        add_header "Access-Control-Allow-Origin" "*";
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
    
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files $uri $uri/ /index.html =404;
      }
    }
    
    推荐文章