我们在Docker的常见域下有几个rails应用程序,我们使用nginx将请求定向到特定应用程序.
our_dev_server.com/foo # proxies to foo app
our_dev_server.com/bar # proxies to bar
Run Code Online (Sandbox Code Playgroud)
Config看起来像这样:
upstream foo {
server foo:3000;
}
upstream bar {
server bar:3000;
}
# and about 10 more...
server {
listen *:80 default_server;
server_name our_dev_server.com;
location /foo {
# this is specific to asset management in rails dev
rewrite ^/foo/assets(/.*)$ /assets/$1 break;
rewrite ^/foo(/.*)$ /foo/$1 break;
proxy_pass http://foo;
}
location /bar {
rewrite ^/bar/assets(/.*)$ /assets/$1 break;
rewrite ^/bar(/.*)$ /bar/$1 break;
proxy_pass http://bar;
}
# and about 10 more... …Run Code Online (Sandbox Code Playgroud) 所以,我想nginx在请求时解析后端的主机名。我希望HTTP 502 Bad Gateway在后端服务关闭时得到响应,并且在它启动时得到服务响应。
我使用nginx:1.15-alpine图像nginx,这是我在它的配置中所拥有的:
server {
resolver kube-dns.kube-system.svc.cluster.local valid=5s;
server_name mysystem.com;
listen 80;
client_max_body_size 20M;
location = /nginx_status {
stub_status on;
access_log off;
}
# Services configuration
location ~ /my-service/ {
set $service_endpoint http://my-service.namespace:8080;
proxy_pass $service_endpoint$request_uri;
include includes/defaults-inc.conf;
include includes/proxy-inc.conf;
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我向 nginx 发出请求时,我收到 502 Bad Gateway 响应。Nginx 的日志说找不到名称:
2018/06/28 19:49:18 [error] 7#7: *1 my-service.namespace could not be resolved (3: Host not found), client: 10.44.0.1, server: mysystem.com, request: "GET /my-service/version …Run Code Online (Sandbox Code Playgroud)