使用 nginx-proxy 和 nginx-proxy-companion 进行 Nginx 301 重定向

使用 nginx-proxy 和 nginx-proxy-companion 进行 Nginx 301 重定向

我正在使用类似这个 docker-compose.yml 文件。

version: '3'
services:
  httpd:
    image: httpd:alpine
    environment:
      - VIRTUAL_HOST=example.com,www.example.com
      - LETSENCRYPT_HOST=example.com,www.example.com
      - [email protected]
    ports:
      - '1080:80'
  proxy:
    container_name: my_proxy
    image: jwilder/nginx-proxy:alpine
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - '/var/run/docker.sock:/tmp/docker.sock:ro'
      - './volumes/proxy/letsencrypt:/etc/nginx/certs:ro'
      - './volumes/proxy/vhost:/etc/nginx/vhost.d'
      - './volumes/proxy/html:/usr/share/nginx/html'
  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    environment:
      - NGINX_PROXY_CONTAINER=my_proxy
      - [email protected]
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock:ro'
      - './volumes/proxy/letsencrypt:/etc/nginx/certs'
      - './volumes/proxy/vhost:/etc/nginx/vhost.d'
      - './volumes/proxy/html:/usr/share/nginx/html'

我有以下 301 重定向(和两个证书,一个用于 example.com,一个用于 www.example.com)。

这没问题,但我正在尝试获取以下 301 重定向(仅对域 www.example.com 有一个证书)。

是否可以使用这个 docker 镜像来做到这一点?

答案1

这是一个可行的解决方案,包含以下新docker-compose.yml文件。

version: '3'
services:
  httpd:
    image: httpd:alpine
    environment:
      - VIRTUAL_HOST=www.example.com
      - LETSENCRYPT_HOST=example.com,www.example.com
      - [email protected]
    ports:
      - '1080:80'
  proxy:
    container_name: my_proxy
    image: jwilder/nginx-proxy:alpine
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - '/var/run/docker.sock:/tmp/docker.sock:ro'
      - './volumes/config/proxy/nginx.conf:/etc/nginx/conf.d/example.conf:ro'
      - './volumes/proxy/letsencrypt:/etc/nginx/certs:ro'
      - './volumes/proxy/vhost:/etc/nginx/vhost.d'
      - './volumes/proxy/html:/usr/share/nginx/html'
  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    environment:
      - NGINX_PROXY_CONTAINER=my_proxy
      - [email protected]
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock:ro'
      - './volumes/proxy/letsencrypt:/etc/nginx/certs'
      - './volumes/proxy/vhost:/etc/nginx/vhost.d'
      - './volumes/proxy/html:/usr/share/nginx/html'

./volumes/config/proxy/nginx.conf以下是文件的内容。

server {
    server_name example.com;
    listen 80 ;
    listen [::]:80 ;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    return 301 https://www.$host$request_uri;
    ssl_certificate /etc/nginx/certs/default.crt;
    ssl_certificate_key /etc/nginx/certs/default.key;
}

我不知道是否可以重定向https://示例->https://www.example.com不使用此默认 SSL 证书。

相关内容