简体   繁体   English

如何设置端口运行静态网站作为nginx docker容器?

[英]How to set port to run static website as nginx docker container?

I'm trying to run a static website using docker and nginx. 我正在尝试使用docker和nginx运行静态网站。 I'm using jwilder/nginx-proxy as a reverse proxy on my ubuntu machine. 我在我的ubuntu机器上使用jwilder / nginx-proxy作为反向代理。 But calling https://www.example.com returns me a 502 error. 但是,调用https://www.example.com会返回502错误。 I think it is a problem with setting the port correctly, but I do not see my mistake. 我认为正确设置端口是一个问题,但我没有看到我的错误。

This is my Dockerfile... 这是我的Dockerfile ......

FROM nginx:alpine
COPY . /usr/share/nginx/html

EXPOSE 3499

...and this is how I configured my gitlab ci: ...这就是我配置gitlab ci的方式:

build:
  stage: build
  image: docker:stable
  script:
    - docker build -t ${DOCKER_CONTAINER}:latest .

production:
  stage: release
  image: docker:stable
  variables:
    GIT_STRATEGY: none
  script:
    - docker run
      --name ${DOCKER_CONTAINER}
      --detach
      --restart=always
      -e VIRTUAL_HOST=www.example.com
      -e LETSENCRYPT_HOST=www.example.com
      -p 3499
      ${DOCKER_CONTAINER}:latest
  environment:
    name: production
    url: https://www.example.com

The problem is that are assuming just using EXPOSE 3499 changes the port on your nginx config. 问题是假设只使用EXPOSE 3499更改nginx配置上的端口。 The nginx is still running on port 80 within the container. nginx仍然在容器内的端口80上运行。

EXPOSE is to say your intentions, that the image does intend to expose 3499 . EXPOSE是说你的意图,图像确实打算暴露3499 But the server inside needs to be configured to make sure the port it listens on is the same. 但是需要配置内部服务器以确保它侦听的端口是相同的。

Since you are using the official docker nginx image, you can read it's documentation below 由于您使用的是官方docker nginx映像,因此您可以在下面阅读它的文档

https://hub.docker.com/_/nginx https://hub.docker.com/_/nginx

Using environment variables in nginx configuration 在nginx配置中使用环境变量

Out-of-the-box, nginx doesn't support environment variables inside most configuration blocks. 开箱即用,nginx不支持大多数配置块中的环境变量。 But envsubst may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts. 但是如果需要在nginx启动之前动态生成nginx配置,则可以使用envsubst作为解决方法。

Here is an example using docker-compose.yml: 以下是使用docker-compose.yml的示例:

web:
  image: nginx
  volumes:
   - ./mysite.template:/etc/nginx/conf.d/mysite.template
  ports:
   - "8080:80"
  environment:
   - NGINX_HOST=foobar.com
   - NGINX_PORT=80
  command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"

The mysite.template file may then contain variable references like this: 然后mysite.template文件可能包含这样的变量引用:

listen ${NGINX_PORT};

So as you see, you need to do some work in the image to make sure your nginx actually listens on 3499 所以如你所见,你需要在图像中做一些工作以确保你的nginx实际上在3499监听

Update: 23rd July 2019 更新:2019年7月23日

In case you don't want to do it using environment variable. 如果您不想使用环境变量。 Then you can overwrite the file using docker-compose 然后,您可以使用docker-compose覆盖该文件

So you will keep a local file default.conf where you will change the contents 因此,您将保留本地文件default.conf ,您将在其中更改内容

server {
    listen       3499;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

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

And mount this file in your docker-compose.yml 并将此文件安装在docker-compose.yml

web:
  image: nginx
  volumes:
   - ./default.conf:/etc/nginx/conf.d/default.conf
  ports:
   - "8080:3499"

And that will make sure that the file is correct. 这将确保文件正确。 You can even copy that in the Dockerfile , if you don't want to override at run-time 如果您不想在运行时覆盖,您甚至可以在Dockerfile复制它

You need to add 你需要添加

-e VIRTUAL_PORT=3499 to your gitlab configuration.

Check Multiple Ports section of documentation of jwilder/nginx-proxy. 检查jwilder / nginx-proxy文档的Multiple Ports部分。 https://hub.docker.com/r/jwilder/nginx-proxy https://hub.docker.com/r/jwilder/nginx-proxy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM