Docker + Gunicorn + Nginx + Django:在AWS Route 53上将非www重定向到www

时间:2018-02-10 18:48:26

标签: django amazon-web-services docker nginx

我在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 Conf

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

是的,适合进行nginx重定向,但我觉得PREPEND_WWW更简单。

settings.py

中添加此内容
PREPEND_WWW = True