CORS 在 dockerized Django REST + Angular 项目中被阻止
CORS blocked in dockerized Django REST + Angular project
我今天决定 docker 化现有的 Django REST + Angular 应用程序。该网站正常显示,但 CORS 请求被阻止。
Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
angular 应用程序使用 Nginx docker 化。我正在使用 this 教程进行中(docker + 撰写文件如下所列)。
奇怪的是,我以前遇到过这个问题,并通过使用 djang-cors-headers 包解决了它。连同 CORS_ORIGIN_ALLOW_ALL = True
,这似乎不再能解决问题。
这是否与应用程序现在在容器中运行有关?我正在为项目提供 docker 文件以及下面的 docker-compose 文件。不过不确定它们是否相关。
Angular 项目的 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'
提前致谢!
问题是由于 nginx 阻止了请求。在该层再次添加 headers,产生以下 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;
}
}
我今天决定 docker 化现有的 Django REST + Angular 应用程序。该网站正常显示,但 CORS 请求被阻止。
Access to XMLHttpRequest at 'http://localhost:8000/branches/1' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
angular 应用程序使用 Nginx docker 化。我正在使用 this 教程进行中(docker + 撰写文件如下所列)。
奇怪的是,我以前遇到过这个问题,并通过使用 djang-cors-headers 包解决了它。连同 CORS_ORIGIN_ALLOW_ALL = True
,这似乎不再能解决问题。
这是否与应用程序现在在容器中运行有关?我正在为项目提供 docker 文件以及下面的 docker-compose 文件。不过不确定它们是否相关。
Angular 项目的 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'
提前致谢!
问题是由于 nginx 阻止了请求。在该层再次添加 headers,产生以下 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;
}
}