An open API service indexing awesome lists of open source software.

https://github.com/fh332393900/docker-compose-node

📦 docker-compose实现node服务代理到二级路径
https://github.com/fh332393900/docker-compose-node

docker docker-compose node

Last synced: about 2 months ago
JSON representation

📦 docker-compose实现node服务代理到二级路径

Awesome Lists containing this project

README

          

📦 *docker-compose* 实现 node 服务代理到二级路径

使用 `docker-compose` 运行容器,同时部署 node 版与 nginx 版

## 使用方法
``` bash
$ docker-compose up --build
```

### 前言(背景)

我们在服务器运行了一个 Node 服务端口在 `3000`,我们要访问服务的接口就只能在 http://xxx:3000 进行访问
但是我们想直接在 `80` 端口进行访问,例如 http://xxx/api 来调用接口要怎么做呢?

### 实现

我们可以使用 nginx 来配置把端口代理到二级路径实现

```js
server {
listen 80;
server_name localhost;

root /usr/share/nginx/html;
index index.html index.htm;

location /api {
add_header X-Config A;
proxy_pass http://47.108.210.87:3000;
}
}
```

docker-componse 的配置

```yaml
version: "3"
services:
node-app:
build:
context: .
dockerfile: node.Dockerfile
ports:
- 3000:3000
nginx-app:
build:
context: .
dockerfile: nginx.Dockerfile
ports:
- 80:80
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf
- .:/usr/share/nginx/html
```

说明:
我们的 node 服务是运行在 3000 端口的,在 80 端口是我们的前端页面,同时添加代理的配置