Nginx

Nginx 301 重定向與 nginx-proxy 和 nginx-proxy-companion

  • May 2, 2020

我正在使用類似 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
     - LETSENCRYPT_EMAIL=name@example.com
   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
     - DEFAULT_EMAIL=name@example.com
   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 images 來做到這一點?

這是一個有效的解決方案,具有以下新docker-compose.yml文件。

version: '3'
services:
 httpd:
   image: httpd:alpine
   environment:
     - VIRTUAL_HOST=www.example.com
     - LETSENCRYPT_HOST=example.com,www.example.com
     - LETSENCRYPT_EMAIL=name@example.com
   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
     - DEFAULT_EMAIL=name@example.com
   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;
}

我不知道是否可以在不使用此預設 ssl 證書的情況下重定向https://example -> https://www.example.com 。

引用自:https://serverfault.com/questions/1015242