这里不允许使用nginx“env”指令

weixin_38054276 2019-09-12 11:52:39
我将nginx与docker-compose一起使用,并希望docker-compose将SERVER_NAME作为环境变量传递给nginx.我创建了以下nginx配置文件: myconf.conf: # Environment variables are used to dynamically set SERVER_NAME # from docker-defined environment variable. This logic is stolen from: # # https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html # https://github.com/wantedly/nginx-image-server/blob/master/files/nginx.conf env SERVER_NAME; upstream django { server workflows-django:8000; } server { listen 80; perl_set $server_name_from_env 'sub { return $ENV{"SERVER_NAME"}; }'; server_name $server_name_from_env; return 301 https://$server_name$request_uri; } server { listen 443 ssl; perl_set $server_name_from_env 'sub { return $ENV{"SERVER_NAME"}; }'; server_name $server_name_from_env; ssl_certificate /etc/ssl/private/bostongene.crt; ssl_certificate_key /etc/ssl/private/bostongene.key; charset utf-8; client_max_body_size 75M; location /media { alias /srv/workflows/media; } location /static { alias /srv/workflows/static; } location / { # We can talk to upstream django either via uwsgi or just http proxy # uwsgi: uwsgi_pass django; include /etc/nginx/uwsgi_params; # http proxy: #proxy_set_header Host $host; #proxy_pass http://django } } 出于某种原因,nginx正在诅咒那个env指令: 2016/08/26 13:02:39 [emerg] 1#0: "env" directive is not allowed here in /etc/nginx/sites-enabled/default:7 这是我的Dockerfile,您可以在其中看到此配置用作nginx默认站点: Dockerfile: FROM debian:latest RUN apt-get update && apt-get install -y nginx \ ca-certificates \ gettext-base COPY myconf.conf /etc/nginx/sites-available/default COPY myconf.crt /etc/ssl/private/ COPY myconf.key /etc/ssl/private/ # forward request and error logs to docker log collector RUN ln -sf /dev/stdout /var/log/nginx/access.log \ && ln -sf /dev/stderr /var/log/nginx/error.log EXPOSE 80 443 CMD ["/usr/sbin/nginx", "-g", "daemon off;"] 为什么? 答案:让我解释一下,详细问题是什么. myconf.conf文件用作/ etc / nginx / sites-available / default,这不是root nginx配置.根nginx配置是isnginx.conf,它在http上下文中包含/ etc / nginx / sites-available / default: http { ... ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 所以我的env语句实际上是在http上下文中,这是不正确的 – 它应该只写在根上下文中.
...全文
220 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38062424 2019-09-12
  • 打赏
  • 举报
回复
https://bot.ngx.cc/logs/%23nginx/2015/01-January/%23nginx.01-31.log(谷歌搜索“nginx bug”env指令不允许在这里“”)给了我答案. 正如server directive1的存在所证明的那样,这不是您的主要配置文件,而是要包含的块,最有可能在http context中.而env仅在主要上下文中有效. 1如链接所示,有几种类型的服务器指令,其中没有一种在主上下文中有效

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧