From 0dae5def6bcf45464c7da191c9bc752476880ce8 Mon Sep 17 00:00:00 2001 From: Andrey Zhavoronkov <41117609+azhavoro@users.noreply.github.com> Date: Fri, 28 Feb 2020 23:01:29 +0300 Subject: [PATCH] Az/cvat proxy (#1177) * added nginx proxy * removed unnecessary port configuration & build arg * updated installation guide --- Dockerfile.ui | 14 ++-------- cvat-ui/package.json | 2 +- cvat-ui/src/cvat-core.ts | 10 ++----- cvat/apps/documentation/installation.md | 11 +++----- cvat_proxy/conf.d/cvat.conf.template | 37 +++++++++++++++++++++++++ cvat_proxy/nginx.conf | 16 +++++++++++ docker-compose.yml | 30 +++++++++++--------- 7 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 cvat_proxy/conf.d/cvat.conf.template create mode 100644 cvat_proxy/nginx.conf diff --git a/Dockerfile.ui b/Dockerfile.ui index f31b89da1ae4..b0bfc23c18c6 100644 --- a/Dockerfile.ui +++ b/Dockerfile.ui @@ -1,12 +1,9 @@ -FROM ubuntu:18.04 AS cvat-ui +FROM node:lts-alpine AS cvat-ui ARG http_proxy ARG https_proxy ARG no_proxy ARG socks_proxy -ARG REACT_APP_API_PORT -ARG REACT_APP_API_PROTOCOL -ARG REACT_APP_API_HOST ENV TERM=xterm \ http_proxy=${http_proxy} \ @@ -17,13 +14,6 @@ ENV TERM=xterm \ ENV LANG='C.UTF-8' \ LC_ALL='C.UTF-8' -# Install necessary apt packages -RUN apt update && apt install -yq nodejs npm curl && \ - npm install -g n && n 10.16.3 - -# Create output directories -RUN mkdir /tmp/cvat-ui /tmp/cvat-core /tmp/cvat-canvas - # Install dependencies COPY cvat-core/package*.json /tmp/cvat-core/ COPY cvat-canvas/package*.json /tmp/cvat-canvas/ @@ -47,7 +37,7 @@ COPY cvat-canvas/ /tmp/cvat-canvas/ COPY cvat-ui/ /tmp/cvat-ui/ RUN npm run build -FROM nginx +FROM nginx:stable-alpine # Replace default.conf configuration to remove unnecessary rules COPY cvat-ui/react_nginx.conf /etc/nginx/conf.d/default.conf COPY --from=cvat-ui /tmp/cvat-ui/dist /usr/share/nginx/html/ diff --git a/cvat-ui/package.json b/cvat-ui/package.json index c2c08d2c32d3..9c25aa8f6d98 100644 --- a/cvat-ui/package.json +++ b/cvat-ui/package.json @@ -5,7 +5,7 @@ "main": "src/index.tsx", "scripts": { "build": "webpack --config ./webpack.config.js", - "start": "webpack-dev-server --config ./webpack.config.js --mode=development", + "start": "REACT_APP_API_URL=http://localhost:7000 webpack-dev-server --config ./webpack.config.js --mode=development", "type-check": "tsc --noEmit", "type-check:watch": "npm run type-check -- --watch", "lint": "eslint './src/**/*.{ts,tsx}'", diff --git a/cvat-ui/src/cvat-core.ts b/cvat-ui/src/cvat-core.ts index d63488485611..a777235e113b 100644 --- a/cvat-ui/src/cvat-core.ts +++ b/cvat-ui/src/cvat-core.ts @@ -6,14 +6,8 @@ import _cvat from '../../cvat-core/src/api'; const cvat: any = _cvat; -const protocol = typeof (process.env.REACT_APP_API_PROTOCOL) === 'undefined' - ? 'http' : process.env.REACT_APP_API_PROTOCOL; -const host = typeof (process.env.REACT_APP_API_HOST) === 'undefined' - ? 'localhost' : process.env.REACT_APP_API_HOST; -const port = typeof (process.env.REACT_APP_API_PORT) === 'undefined' - ? '7000' : process.env.REACT_APP_API_PORT; - -cvat.config.backendAPI = `${protocol}://${host}:${port}/api/v1`; +cvat.config.backendAPI = typeof (process.env.REACT_APP_API_URL) === 'undefined' + ? '/api/v1' : `${process.env.REACT_APP_API_URL}/api/v1`; export default function getCore(): any { return cvat; diff --git a/cvat/apps/documentation/installation.md b/cvat/apps/documentation/installation.md index 9931d7438076..1286fe813c2e 100644 --- a/cvat/apps/documentation/installation.md +++ b/cvat/apps/documentation/installation.md @@ -262,9 +262,8 @@ docker-compose down ### Advanced settings -If you want to access you instance of CVAT outside of your localhost you should -specify the [ALLOWED_HOSTS](https://docs.djangoproject.com/en/2.0/ref/settings/#allowed-hosts) -environment variable. The best way to do that is to create +If you want to access your instance of CVAT outside of your localhost you should +specify the `CVAT_HOST` environment variable. The best way to do that is to create [docker-compose.override.yml](https://docs.docker.com/compose/extends/) and put all your extra settings here. @@ -272,11 +271,9 @@ all your extra settings here. version: "2.3" services: - cvat: + cvat_proxy: environment: - ALLOWED_HOSTS: .example.com - ports: - - "80:8080" + CVAT_HOST: .example.com ``` Please don't forget include this file to docker-compose commands using the `-f` diff --git a/cvat_proxy/conf.d/cvat.conf.template b/cvat_proxy/conf.d/cvat.conf.template new file mode 100644 index 000000000000..c208417426dd --- /dev/null +++ b/cvat_proxy/conf.d/cvat.conf.template @@ -0,0 +1,37 @@ +server { + listen 80; + server_name _ default; + return 404; +} + +server { + listen 80; + server_name ${CVAT_HOST}; + + location ~* /api/.*|git/.*|tensorflow/.*|auto_annotation/.*|analytics/.*|static/.*|admin|admin/.*|documentation/.*|dextr/.*|reid/.* { + proxy_pass http://cvat:8080; + proxy_pass_header X-CSRFToken; + proxy_set_header Host $http_host; + proxy_pass_header Set-Cookie; + } + + location / { + # workaround for match location by arguments + error_page 418 = @annotation_ui; + + if ( $query_string ~ "^id=\d+.*" ) { return 418; } + + proxy_pass http://cvat_ui; + proxy_pass_header X-CSRFToken; + proxy_set_header Host $http_host; + proxy_pass_header Set-Cookie; + } + + # old annotation ui, will be removed in the future. + location @annotation_ui { + proxy_pass http://cvat:8080; + proxy_pass_header X-CSRFToken; + proxy_set_header Host $http_host; + proxy_pass_header Set-Cookie; + } +} diff --git a/cvat_proxy/nginx.conf b/cvat_proxy/nginx.conf new file mode 100644 index 000000000000..289b0f6995f2 --- /dev/null +++ b/cvat_proxy/nginx.conf @@ -0,0 +1,16 @@ +worker_processes 2; + + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + sendfile on; + keepalive_timeout 65; + + include /etc/nginx/conf.d/*.conf; + client_max_body_size 0; +} diff --git a/docker-compose.yml b/docker-compose.yml index 053a21cd2882..3bb1eb701515 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,5 @@ # -# Copyright (C) 2018 Intel Corporation +# Copyright (C) 2018-2020 Intel Corporation # # SPDX-License-Identifier: MIT # @@ -37,8 +37,6 @@ services: depends_on: - cvat_redis - cvat_db - ports: - - "8080:8080" build: context: . args: @@ -54,11 +52,7 @@ services: OPENVINO_TOOLKIT: "no" environment: DJANGO_MODWSGI_EXTRA_ARGS: "" - UI_SCHEME: http - UI_HOST: localhost - UI_PORT: 7080 - - + ALLOWED_HOSTS: '*' volumes: - cvat_data:/home/django/data - cvat_keys:/home/django/keys @@ -67,7 +61,6 @@ services: cvat_ui: container_name: cvat_ui - image: nginx restart: always build: context: . @@ -76,9 +69,6 @@ services: https_proxy: no_proxy: socks_proxy: - REACT_APP_API_PROTOCOL: http - REACT_APP_API_HOST: localhost - REACT_APP_API_PORT: 8080 dockerfile: Dockerfile.ui networks: @@ -87,8 +77,22 @@ services: - ui depends_on: - cvat + + cvat_proxy: + container_name: cvat_proxy + image: nginx:stable-alpine + restart: always + depends_on: + - cvat + - cvat_ui + environment: + CVAT_HOST: localhost ports: - - "7080:80" + - "8080:80" + volumes: + - ./cvat_proxy/nginx.conf:/etc/nginx/nginx.conf:ro + - ./cvat_proxy/conf.d/cvat.conf.template:/etc/nginx/conf.d/cvat.conf.template:ro + command: /bin/sh -c "envsubst '$$CVAT_HOST' < /etc/nginx/conf.d/cvat.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" volumes: cvat_db: