nginx:将nginx配置为基于Docker的微服务的反向代理时,在上游"server abcd"中找不到[emerg]主机



我们有一组 dockerized 微服务,它们已经在具有私有内部 IP 的 VM 上运行。我配置了一个 dockerized nginx 以在这些微服务前面设置反向代理,但我得到 nginx:在上游"ms1"错误中找不到 [emerg] 主机。

下面是 nginx.conf 文件

#user  nobody;
#Defines which Linux system user will own and run the Nginx server
worker_processes  4;
#Referes to single threaded process. Generally set to be equal to the number of CPUs or cores.
#error_log  logs/error.log; #error_log  logs/error.log  notice;
#Specifies the file where server logs.
#pid        logs/nginx.pid;
#nginx will write its master process ID(PID).
events {
worker_connections  1024;
# worker_processes and worker_connections allows you to calculate maxclients value:
# max_clients = worker_processes * worker_connections
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
# If serving locally stored static files, sendfile is essential to speed up the server,
# But if using as reverse proxy one can deactivate it
#tcp_nopush     on;
# works opposite to tcp_nodelay. Instead of optimizing delays, it optimizes the amount of data sent at once.
#keepalive_timeout  0;
keepalive_timeout  65;
# timeout during which a keep-alive client connection will stay open.
gzip  on;
# tells the server to use on-the-fly gzip compression.
server {
listen       8080;
server_name your-domain.com www.your-domain.com;
#root configuration for static files.
root   /usr/share/nginx/html;
index  index.html index.htm;
error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
upstream consumer-portal {
server consumer-portal:9006;
}
upstream download-zip-service {
server download-zip-service:9012;
}
server {
listen          8080;
server_name     www.example.com;
location /consumer-portal/ {
proxy_pass http://consumer-portal:9006/;
}
location /download-zip-service/ {
proxy_pass http://download-zip-service:9012/;
}
}
}

以下是我的Dockerfile的内容:

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

以下是docker-compose.yml文件的内容

version: '3'
services:
nginx:
restart: always
build: ./conf/
volumes:
- ./mysite.template:/etc/nginx/conf.d/mysite.template
ports:
- "8080:8080"
networks:
- cloud
networks:
cloud:
driver: bridge

我使用网络的名称作为云,因为容器消费者门户和下载zip服务在云网络上运行。

我在运行命令时出现以下错误:

docker-compose build
docker-compose up
nginx_1  | 2018/07/02 20:48:52 [emerg] 1#1: host not found in upstream "consumer-portal:9006" in /etc/nginx/nginx.conf:50
nginx_1  | nginx: [emerg] host not found in upstream "consumer-portal:9006" in /etc/nginx/nginx.conf:50

任何指导不胜感激!

根据这篇文章的建议,新的 conf 文件:

#user  nobody;
#Defines which Linux system user will own and run the Nginx server
worker_processes  4;
#Referes to single threaded process. Generally set to be equal to the number of CPUs or cores.
#error_log  logs/error.log; #error_log  logs/error.log  notice;
#Specifies the file where server logs.
#pid        logs/nginx.pid;
#nginx will write its master process ID(PID).
events {
worker_connections  1024;
# worker_processes and worker_connections allows you to calculate maxclients value:
# max_clients = worker_processes * worker_connections
}

http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
# If serving locally stored static files, sendfile is essential to speed up the server,
# But if using as reverse proxy one can deactivate it
#tcp_nopush     on;
# works opposite to tcp_nodelay. Instead of optimizing delays, it optimizes the amount of data sent at once.
#keepalive_timeout  0;
keepalive_timeout  65;
# timeout during which a keep-alive client connection will stay open.
gzip  on;
# tells the server to use on-the-fly gzip compression.
server {
listen       8080;
server_name your-domain.com www.your-domain.com;
#root configuration for static files.
root   /usr/share/nginx/html;
index  index.html index.htm;
error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
# upstream consumer-portal {
#     server consumer-portal;
# }
# upstream download-zip-service {
#     server download-zip-service;
# }
server {
listen          8080;
server_name     www.example.com;
location /consumer-portal/ {
proxy_redirect http://consumer-portal:9006;
}
location /download-zip-service/ {
proxy_redirect http://download-zip-service:9012;
}
}
}

如果要使用主机名指定上游服务器,则需要指定解析器

最新更新