简体   繁体   English

docker 和 jwilder/nginx-proxy http/https 问题

[英]docker and jwilder/nginx-proxy http/https issue

I'm using docker on osx via boot2docker .我通过boot2docker在 osx 上使用boot2docker

I have 2 hosts: site1.loc.test.com and site2.loc.test.com pointed to ip address of docker host.我有 2 个主机: site1.loc.test.comsite2.loc.test.com指向site1.loc.test.com主机的 IP 地址。

Both should be available via 80 and 443 ports.两者都应该通过80443端口可用。

So I'm using jwilder/nginx-proxy for reverse proxy purposes.所以我将jwilder/nginx-proxy用于反向代理。

But in fact when I'm running all of them via docker-compose every time I try to open via 80 port I get redirect to 443 (301 Moved Permanently) .但实际上,当我每次尝试通过80端口打开时都通过docker-compose运行所有这些时,我会重定向到443 (301 Moved Permanently)

May be I've missed something in jwilder/nginx-proxy configuration?可能是我在jwilder/nginx-proxy配置中遗漏了什么?

docker-compose.yml docker-compose.yml

proxy:
  image: jwilder/nginx-proxy
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
    - certs:/etc/nginx/certs
  ports:
    - "80:80"
    - "443:443"

site1:
  image: httpd:2.4
  volumes:
    - site1:/usr/local/apache2/htdocs
  environment:
    VIRTUAL_HOST: site1.loc.test.com
  expose:
    - "80"

site2:
  image: httpd:2.4
  volumes:
    - site2:/usr/local/apache2/htdocs
  environment:
    VIRTUAL_HOST: site2.loc.test.com
  expose:
    - "80"

Just to keep this topic up to date, the jwilder/nginx-proxy meanwhile introduced a flag for that: HTTPS_METHOD=noredirect ;为了保持这个主题的最新状态,jwilder/nginx-proxy 同时引入了一个标志: HTTPS_METHOD=noredirect To be set as environment variable.要设置为环境变量。

Further reading on github在 github 上进一步阅读

I think your configuration should be correct, but it seems that this is the intended behaviour of jwilder/nginx-proxy .我认为您的配置应该是正确的,但似乎这是jwilder/nginx-proxy的预期行为。 See these lines in the file nginx.tmpl : https://github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl#L89-L94请参阅文件nginx.tmpl中的这些行: https : //github.com/jwilder/nginx-proxy/blob/master/nginx.tmpl#L89-L94

It seems that if a certificate is found, you will always be redirected to https.似乎如果找到证书,您将始终被重定向到 https。


EDIT: I found the confirmation in the documentation编辑:我在文档中找到了确认

The behavior for the proxy when port 80 and 443 are exposed is as follows:当端口 80 和 443 暴露时,代理的行为如下:

  • If a container has a usable cert, port 80 will redirect to 443 for that container so that HTTPS is always preferred when available.如果容器具有可用的证书,则端口 80 将重定向到该容器的 443,以便在可用时始终首选 HTTPS。

You can still use a custom configuration .您仍然可以使用自定义配置 You could also try to override the file nginx.tmpl in a new Dockefile .您也可以尝试以覆盖该文件nginx.tmpl在新Dockefile。

To serve traffic in both SSL and non-SSL modes without redirecting to SSL, you can include the environment variable HTTPS_METHOD=noredirect (the default is HTTPS_METHOD=redirect).要在 SSL 和非 SSL 模式下提供流量而不重定向到 SSL,您可以包含环境变量 HTTPS_METHOD=noredirect(默认为 HTTPS_METHOD=redirect)。

HTTPS_METHOD must be specified on each container for which you want to override the default behavior.必须在要覆盖其默认行为的每个容器上指定 HTTPS_METHOD。

Here is an example Docker Compose file:这是一个示例 Docker Compose 文件:

version: '3'

services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./config/certs:/etc/nginx/certs
    environment:
      DEFAULT_HOST: my.example.com

  app:
    build:
      context: .
      dockerfile: ./Dockerfile
    environment:
      HTTPS_METHOD: noredirect
      VIRTUAL_HOST: my.example.com

Note: As in this example, environment variable HTTPS_METHOD must be set on the app container, not the nginx-proxy container.注意:在本例中,环境变量HTTPS_METHOD必须设置在app容器上,而不是nginx-proxy容器上。

Ref: How SSL Support Works section for the jwilder/nginx-proxy Docker image.参考: jwilder/nginx-proxy Docker 映像的How SSL Support Works部分。

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

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