大网数据广告活动
服务器运维问答

Nginx 301 redirect with nginx-proxy and nginx-proxy-companion

· 52CE 我爱测

I'm using something like this docker-compose.yml file. version: '3' services: httpd: image: httpd:alpine environment: - VIRTUAL_HOST=example.com,www.example.com - LETSENCRYPT_HOST=example.com,www.example.com - LETSENCRYPT_EMAIL=[…

问题原文

原帖链接 · MB4E · CC BY-SA 4.0

I'm using something like this docker-compose.yml file.

version: '3'
services:
  httpd:
    image: httpd:alpine
    environment:
      - VIRTUAL_HOST=example.com,www.example.com
      - LETSENCRYPT_HOST=example.com,www.example.com
      - LETSENCRYPT_EMAIL=[email omitted]
    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=[email omitted]
    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'

I have following 301 redirections (and two certificates, one for example.com and one for www.example.com).

This is ok, but I have I'm trying to get following 301 redirections (with only one certificate for the domain www.example.com).

Is it possible to do that using this docker images ?

已采纳解答

原帖链接 · MB4E · CC BY-SA 4.0

Here is a working solution, with the following new docker-compose.yml file.

version: '3'
services:
  httpd:
    image: httpd:alpine
    environment:
      - VIRTUAL_HOST=www.example.com
      - LETSENCRYPT_HOST=example.com,www.example.com
      - LETSENCRYPT_EMAIL=[email omitted]
    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=[email omitted]
    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'

And here is the content of ./volumes/config/proxy/nginx.conf file.

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;
}

I don't know if it's possible to redirect https://example -> https://www.example.com without using this default ssl certificates.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。