Nginx中的Heroku(Docker)PORT环境变量 [英] Heroku (Docker) PORT environment variable in nginx

查看:119
本文介绍了Nginx中的Heroku(Docker)PORT环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是对Dockerfile进行了一些修改,以便在Heroku上使用Nginx运行它。 Heroku的特别之处在于,所有内容都以非root用户身份运行。其次,某些行为是使用来自Heroku本身的随机端口,您无法对其进行修改。它们提供了env $ PORT,您应将其绑定到nginx。如果Heroku识别到该端口未绑定某些内容,则会停止整个容器。问题是:



如何将nginx绑定到给定的env变量,以便在nginx-site.conf中具有动态端口?
我尝试在 Dockefile 中使用以下内容:



env PORT; nginx.conf 中的$ c>和监听PORT_from_env; nginx-site.conf 中的
nginx-site.conf 中尝试监听80; RUN / bin / sed -i s / list 80 / listen $ {PORT} / /etc/nginx/sites-available/default.conf Dockerfile



<我绝对超能力。有人有想法或可以帮忙吗?






其他信息:




解决方案

通过遵循此示例



第1步: default.conf.template

$中收听 $ PORT b
$ b

  server {
listen $ PORT default_server;

位置/ {
root / usr / share / nginx / html;
index index.html;
}
}

步骤2:添加此指令到您的 Dockerfile

  COPY default.conf.template / etc /nginx/conf.d/default.conf.template 

步骤3: Dockerfile

  CMD / bin / bash -c envsubst'\ $ PORT'< /etc/nginx/conf.d/default.conf.template> /etc/nginx/conf.d/default.conf& ;& nginx -g‘关闭守护进程;’


I just take a few modifications on a Dockerfile to run it with nginx on Heroku. Something special about Heroku is, that everything is running as non-root. Second certain behaviour is the use of a random Port which comes from Heroku itself and you can't be modified. They provide the env $PORT which you should bind to nginx. If Heroku recognizes that something isn't bind to that port it stops the entire container. Question is:

How can I bind nginx to a given env variable in order to have a dynamic port in the nginx-site.conf? I tried to use things like follows in the Dockefile:

env PORT; in nginx.conf and listen PORT_from_env; in the nginx-site.conf Also tried listen 80; in the nginx-site.conf and RUN /bin/sed -i "s/listen 80/listen ${PORT}/" /etc/nginx/sites-available/default.conf in the Dockerfile

I am absolutely above my capabilities. Someone has an idea or can help? It be very helpful!


Further information:

解决方案

I got it working for my app by following this example :

Step 1: listen to $PORT in default.conf.template

server {
  listen $PORT default_server;

  location / {
    root   /usr/share/nginx/html;
    index  index.html;
  }
}

Step 2: add this directive to your Dockerfile

COPY default.conf.template /etc/nginx/conf.d/default.conf.template

Step 3: add this at the end of your Dockerfile

CMD /bin/bash -c "envsubst '\$PORT' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf" && nginx -g 'daemon off;'

这篇关于Nginx中的Heroku(Docker)PORT环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆