Skip to content

Commit

Permalink
Add Nginx Component for Service Forwarding (#506)
Browse files Browse the repository at this point in the history
Signed-off-by: letonghan <letong.han@intel.com>
  • Loading branch information
letonghan authored Aug 19, 2024
1 parent 20fc8ca commit 60cc0b0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
76 changes: 76 additions & 0 deletions comps/nginx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Nginx for Microservice Forwarding

[Nginx](https://nginx.org/en/) serves as a versatile tool in the realm of web services, functioning as an HTTP and reverse proxy server, and a generic TCP/UDP proxy server.

In GenAIComps, we utilize nginx to streamline our network services. We provide an nginx Docker container, which is essential for deploying [OPEA](https://github.com/opea-project) microservices, mega services, and managing endpoint and port forwarding for frontend services. Our use of Docker to launch nginx ensures a flexible and reliable service deployment, optimizing our infrastructure to meet diverse operational demands.

## 🚀1. Build Docker Image

```bash
cd docker
docker build -t opea/nginx:latest --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy -f ./Dockerfile .
```

## 🚀2. Environment Settings

To use Nginx for service forwarding, users need to setup environment variables first. The variables set here will be substituted in `nginx.conf.template`.

For example, if you want to use Nginx to forward the frontend, backend services of a [ChatQnA](https://github.com/opea-project/GenAIExamples/tree/main/ChatQnA) example, setup environment variables as below.

```bash
export FRONTEND_SERVICE_IP=${your_frontend_service_ip}
export FRONTEND_SERVICE_PORT=5173
export BACKEND_SERVICE_NAME=chatqna
export BACKEND_SERVICE_IP=${your_backend_service_ip}
export BACKEND_SERVICE_PORT=8888
export NGINX_PORT=${your_nginx_port}
```

Nginx will expose `80` as the default port. You can choose other available ports as `${your_nginx_port}` for Nginx docker.

For other examples, change the variable above following the corresponding READMEs.

If you want to forward other services like `dataprep` using Nginx, add the code below in `nginx.conf.template` and setup the right parameters for it. Notice that the `${dataprep_service_endpoint}` need to be the form of `/v1/xxx/xxx`.

```bash
location ${dataprep_service_endpoint} {
proxy_pass http://${dataprep_service_ip}:${dataprep_service_port};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
```

## 🚀3. Start Nginx Service

### 3.1 Start with CLI (Option 1)

```bash
docker run -d --name opea-nginx -p ${NGINX_PORT}:80 \
-e FRONTEND_SERVICE_IP=${FRONTEND_SERVICE_IP} \
-e FRONTEND_SERVICE_PORT=${FRONTEND_SERVICE_PORT} \
-e BACKEND_SERVICE_NAME=${BACKEND_SERVICE_NAME} \
-e BACKEND_SERVICE_IP=${BACKEND_SERVICE_IP} \
-e BACKEND_SERVICE_PORT=${BACKEND_SERVICE_PORT} \
opea/nginx:latest
```

### 3.2 Start with Docker Compose (Option 2)

```bash
cd docker
docker compose -f docker_compose.yaml up -d
```

## 🚀4. Consume Forwarded Service

To consume the backend service, use the curl command as below (this is a ChatQnA service example):

```bash
curl http://${your_nginx_ip}:${your_nginx_port}/v1/chatqna \
-H "Content-Type: application/json" \
-d '{"messages": "What is Deep Learning?"}'
```

For the frontend service, open the following URL in your browser: `http://${your_nginx_ip}:${your_nginx_port}`.
17 changes: 17 additions & 0 deletions comps/nginx/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


FROM nginx:alpine

RUN apk add --no-cache gettext

COPY nginx.conf.template /etc/nginx/nginx.conf.template

ENV FRONTEND_SERVICE_IP=localhost
ENV FRONTEND_SERVICE_PORT=5173
ENV BACKEND_SERVICE_NAME=chatqna
ENV BACKEND_SERVICE_IP=localhost
ENV BACKEND_SERVICE_PORT=8888

CMD /bin/sh -c "envsubst '${FRONTEND_SERVICE_IP} ${FRONTEND_SERVICE_PORT} ${BACKEND_SERVICE_NAME} ${BACKEND_SERVICE_IP} ${BACKEND_SERVICE_PORT}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
24 changes: 24 additions & 0 deletions comps/nginx/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

services:
opea-nginx-server:
image: opea/nginx:latest
container_name: opea-nginx-server
ports:
- "${NGINX_PORT:-80}:80"
environment:
- no_proxy=${no_proxy}
- https_proxy=${https_proxy}
- http_proxy=${http_proxy}
- FRONTEND_SERVICE_IP=${FRONTEND_SERVICE_IP}
- FRONTEND_SERVICE_PORT=${FRONTEND_SERVICE_PORT}
- BACKEND_SERVICE_NAME=${BACKEND_SERVICE_NAME}
- BACKEND_SERVICE_IP=${BACKEND_SERVICE_IP}
- BACKEND_SERVICE_PORT=${BACKEND_SERVICE_PORT}
ipc: host
restart: always

networks:
default:
driver: bridge
29 changes: 29 additions & 0 deletions comps/nginx/docker/nginx.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0


server {
listen 80;
listen [::]:80;

location /home {
root /usr/share/nginx/html;
index index.html index.htm;
}

location / {
proxy_pass http://${FRONTEND_SERVICE_IP}:${FRONTEND_SERVICE_PORT};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /v1/${BACKEND_SERVICE_NAME} {
proxy_pass http://${BACKEND_SERVICE_IP}:${BACKEND_SERVICE_PORT};
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

0 comments on commit 60cc0b0

Please sign in to comment.