Skip to content

Commit

Permalink
add nginx comps for service forwarding
Browse files Browse the repository at this point in the history
Signed-off-by: letonghan <[email protected]>
  • Loading branch information
letonghan committed Aug 16, 2024
1 parent 9580298 commit 21e9817
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
65 changes: 65 additions & 0 deletions comps/nginx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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
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 servcie 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
```

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

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

```bash
docker run -d --name opea-nginx -p ${your_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
```

## 🚀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}`.
15 changes: 15 additions & 0 deletions comps/nginx/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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 SERVICE_NAME=chatqna
ENV SERVICE_IP=localhost
ENV 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;'"
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 21e9817

Please sign in to comment.