【问题标题】:docker compose nginx proxy not redirectingdocker compose nginx代理不重定向
【发布时间】:2018-09-11 08:56:41
【问题描述】:

我有一个 Ubuntu 服务器,并且在一个 docker 容器中构建了一个 nginx 代理,在另一个容器中构建了一个 nginx 网络服务器,它们都在同一个网络中并且可以互相看到,但是代理不会重定向到网络服务器我在访问我的网站时总是收到此错误:

proxy      | 2018/09/05 15:30:27 [alert] 8#8: 1024 worker_connections are not enough
proxy      | 2018/09/05 15:30:27 [error] 8#8: *4086 upstream prematurely closed connection while reading response header from upstream, client: 172.18.0.1, server: , request: "GET / HTTP/1.0", upstream: "http://XX.XX.XX.XX:80/", host: "test.com"

这是我的文件:

docker-compose.yml

version: '3'
networks:
    webnet:

services:
  proxy:
    build: ./proxy
    container_name: proxy
    networks:
      - webnet
    ports:
      - 80:80

  website:
    container_name: website
    build: ./nginx
    volumes:
      - ./config/default.conf:/etc/nginx/conf.d/
    networks:
      - webnet

Dockerfile 代理

FROM nginx
RUN rm /etc/nginx/nginx.conf

COPY proxy.conf /etc/nginx/nginx.conf

## proxy.conf

events {
  worker_connections 1024;
}

http {
        server {
                listen 80;
                server_name website;

                location / {
                        proxy_pass http://website;
                }
        }
}
```

Dockerfile nginx 网络服务器

FROM nginx

RUN rm /usr/share/nginx/html/*
COPY test.com /usr/share/nginx/html

## nginx webserver default.conf
server {
    server_name test.com www.test.com;
    root /usr/share/nginx/html;
    index index.php;
}

【问题讨论】:

    标签: docker nginx proxy docker-compose


    【解决方案1】:

    我刚刚意识到,我确实将我的代理容器重定向到它自己,现在修复它^^

    Dockerfile 代理

    server {
      listen 80;
      server_name proxy;
    
      location / {
          proxy_pass http://website/
      }
    }
    

    Dockerfile nginx 网络服务器

    server {
        server_name website;
        root /usr/share/nginx/html;
        index index.php;
    }
    

    【讨论】:

      【解决方案2】:

      另一种方法是使用预打包的 NginX http to https docker 镜像,例如https://hub.docker.com/r/foxylion/nginx-https-redirect/

      将所有请求重定向到 https 版本的最小 nginx。

      例如,如果您有一个在安全端口 443 上运行的微服务,那么您可以使用它将所有 80(http 流量)重定向到 443( https 流量)在您的docker-compose.yml 文件中如下所示:-

      services:
      
        my-micro-service:
          image: my-micro-service
          container_name: my-micro-service
          ports:
            - "443:443"
      
        # Redirect http traffic to https
        nginx-redirect:
          image: foxylion/nginx-https-redirect
          ports:
            - "80:80"
      

      如您所见,它非常简单明了。

      【讨论】:

        猜你喜欢
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2019-03-11
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多