【发布时间】:2018-03-10 10:34:24
【问题描述】:
我在我的 kubernetes 集群中部署了一个 nginx pod 来提供静态文件。为了在不同的环境中设置特定的标头,我遵循了official nginx docker image docs 中的说明,它使用envsubst 在运行 nginx 之前从模板生成配置文件。
这是我的 nginx 模板(nginx.conf.template):
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
server {
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /usr/share/nginx/html;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location / {
add_header x-myapp-env $MYAPP_ENV;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#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 /usr/share/nginx/html;
}
}
server {
listen 80;
server_name _;
return 301 https://$host$request_uri;
}
}
我使用 Kubernetes 的default command override 功能在启动 nginx 之前初步生成 nginx conf 文件。这是配置的相关部分:
command: ["/bin/sh"]
args: ["-c", "envsubst < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'" ]
Kubernetes 成功部署了 pod,但是当我发出请求时,我的浏览器中出现 ERR_TOO_MANY_REDIRECTS 错误。
奇怪的是,当我使用与上述几乎相同的 nginx.conf(但没有 add_header 指令)运行命令覆盖而不运行命令覆盖时,它工作正常。
(所有要提供的 SSL 证书和文件都会在构建时愉快地复制到容器中,因此那里应该没有问题)
任何帮助表示赞赏。
【问题讨论】:
-
nginx 日志中有什么内容?
标签: docker nginx kubernetes