【问题标题】:Docker + Gunicorn + Nginx + Django: redirect non-www to www on AWS Route 53Docker + Gunicorn + Nginx + Django:将非 www 重定向到 AWS Route 53 上的 www
【发布时间】:2018-07-21 07:36:23
【问题描述】:

我在 AWS EC2 和 Route 53 上设置了 Docker + Gunicorn + Nginx + Django。现在我想将 mydomain.com 重定向到 www.mydomain.com。

在 Nginx 配置中进行重定向是否合适?或者有没有更好的解决方案。

这里是 docker-compose-yml,使用 gunicorn 启动 Django 服务器。

version: '2'  
services:  
  nginx:
    image: nginx:latest
    container_name: dj_nginx
    ports:
      - "80:8000"
      - "443:443"
    volumes:
      - ./src/my_project/static:/static
      - ./src:/src
      - ./config/nginx:/etc/nginx/conf.d
    depends_on:
      - web
  web:
    build: .
    container_name: dj_web
    command: bash -c "python manage.py makemigrations && python manage.py migrate && gunicorn my_project.wsgi -b 0.0.0.0:8000"
    depends_on:
      - db
    volumes:
      - ./src:/src
      - ./apps/django_rapid:/src/my_project/django_rapid
    expose:
      - "8000"

  db:
    image: postgres:latest
    container_name: dj_db

这是我的 Nginx 会议

upstream web {  
  ip_hash;
  server web:8000;
}

# portal
server {  
    listen 8000;

    location / {
        proxy_pass http://web/;
    }

    location /media  {
        alias  /media;  # your Django project  media files - amend as required
    }

    location /static {
        alias  /static; # your Django project  static files - amend as required
    }

    server_name localhost;
}


# portal (https)                                                                                                   
server {
    listen 443;
    server_name localhost;

    ssl    on;
    ssl_certificate    /etc/nginx/conf.d/mynginx.crt;
    ssl_certificate_key    /etc/nginx/conf.d/mynginx.key;

    location /media  {
        alias  /media;  # your Django project  media files - amend as required
    }

    location /static {
        alias  /static; # your Django project  static files - amend as required
    }

    location / {
        proxy_pass http://web/;
    }
}

【问题讨论】:

    标签: django amazon-web-services docker nginx


    【解决方案1】:

    是的,在网络服务器中进行此类重定向是合适的。如果是 https,则您的证书需要涵盖两个域。

    【讨论】:

      【解决方案2】:

      是的,做 nginx 重定向是合适的,但我发现做 PREPEND_WWW 更简单。

      将此添加到您的 settings.py

      PREPEND_WWW = True
      

      【讨论】:

        猜你喜欢
        • 2016-02-22
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 2022-08-19
        • 2014-05-12
        • 2018-06-23
        • 2020-12-31
        相关资源
        最近更新 更多