这是我使用nginx配置反向代理的方法。有运行nodeJS应用程序的docker容器(app1,app2,...)
有了这个,我通过localhost:8080
指向docker容器app1 nodeJS应用程序,并使用localhost:8081
指向app2。
但是我想在不使用端口的情况下通过子域名调用应用程序,我不知道怎么做到这一点。我也觉得我搞砸了港口......
app1.localhost
应指向app1
,app2.localhost
应指向app2
。
nginx.conf
http {
sendfile on;
upstream docker-app1 {
server app1:80;
}
upstream docker-app2 {
server app2:80;
}
server {
listen 8080;
server_name app1.localhost;
location / {
proxy_pass http://docker-app1;
proxy_redirect off;
proxy_buffering off;
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-Host $server_name;
#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
server {
listen 8081;
server_name app2.localhost;
location / {
proxy_pass http://docker-app2;
proxy_redirect off;
proxy_buffering off;
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-Host $server_name;
#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
}
搬运工-compose.yml
version: '3.3'
services:
nginx:
container_name: 'nginx'
image: 'nginx:1.13.5'
restart: 'always'
ports:
- '80:80'
- '8080:8080'
- '8081:8081'
- '443:443'
volumes:
- './nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro'
app1:
container_name: app1
build: ./app1
restart: always
ports:
- '3001:80'
app2:
container_name: app2
build: ./app1
restart: always
ports:
- '4200:80'
更新:由于Sergiu为反向代理提供了一个很好的链接,我使用新配置更新了帖子;问题仍然相同
答案 0 :(得分:3)
使用jwilder/nginx-proxy
代替自定义nginx应该足够了:
version: '3.3'
services:
nginx:
image: jwilder/nginx-proxy
ports:
- "80:80"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
app1:
container_name: app1
build: ./app1
restart: always
environment:
VIRTUAL_HOST: app1.localhost
ports:
- 80
app2:
container_name: app2
build: ./app1
restart: always
environment:
VIRTUAL_HOST: app2.localhost
ports:
- 80
然后将其放入您机器的/etc/host
:
127.0.0.1 app1.localhost
127.0.0.1 app2.localhost
用作:
curl -i app1.localhost
jwilder/nginx-proxy
将从每个容器(通过docket API)读取VIRTUAL_HOST
环境变量,然后相应地设置其配置文件。
答案 1 :(得分:0)
构建和运行在版本3.x中不起作用
为节点应用程序创建docker镜像。
用于创建节点应用程序图像的Dockerfile
<强> Dockerfile 强>
FROM node:boron
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package.json .
# For npm@5 or later, copy package-lock.json as well
# COPY package.json package-lock.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
相应地重新启动EXPOSE端口
创建泊坞窗图像
docker build -t app1。
docker build -t app2。
创建主机
sudo echo“127.0.0.1 app1.localhost”&gt;&gt;的/ etc /主机
sudo echo“127.0.0.1 app2.localhost”&gt;&gt;的/ etc /主机
<强> nginx.conf 强>
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain text/css text/javascript
application/javascript application/json
application/xml;
index index.html index.htm;
server {
listen 80;
server_name server_name app1.localhost;;
location / {
proxy_pass http://172.31.42.174:8081/;
# Replace 172.31.42.174 with localhost if you are running reverse proxy in local server user private ip/public ip
# Add other properties as required
}
}
server {
listen 80;
server_name server_name app2.localhost;;
location / {
proxy_pass http://172.31.42.174:8081/;
# Replace 172.31.42.174 with localhost if you are running reverse proxy in local server user private ip/public ip
# Add other properties as required
}
}
}
<强>搬运工-compose.yml 强>
version: '3'
services:
nginx:
image: 'nginx:1.13.5'
ports:
- '80:80'
volumes:
- nginx-conf:/etc/nginx/nginx.conf:ro
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
app1:
image: app1
ports:
- 8081:8081
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
app2:
image: app2
ports:
- 8081:8081
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
volumes:
nginx-conf:
driver: local
driver_opts:
o: bind
type: none
device: /path/of/nginx/conf/nginx.conf
将nfginx.conf的完整路径提供给卷,因为nginx依赖于conf文件,我们需要在主节点上运行nginx。
生成强>
docker stack deploy -c docker-compose nodeapps
状态强>
docker stack ls
docker service ls