简体   繁体   English

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

[英]Docker + Gunicorn + Nginx + Django: redirect non-www to www on AWS Route 53

I have a Docker + Gunicorn + Nginx + Django setup on AWS EC2 and Route 53. Right now I want to redirect mydomain.com to www.mydomain.com.我在 AWS EC2 和 Route 53 上安装了 Docker + Gunicorn + Nginx + Django。现在我想将 mydomain.com 重定向到 www.mydomain.com。

Is it appropriate to do a redirect in a Nginx configuration?在 Nginx 配置中进行重定向是否合适? Or are there are better solutions.或者有更好的解决方案。

Here is docker-compose-yml, using gunicorn to start the Django server.这里是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

Here is my Nginx Conf这是我的 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/;
    }
}

Yes, it's appropriate to do these kinds of redirects in the webserver.是的,在网络服务器中进行这些类型的重定向是合适的。 If it's https your certificate needs to cover both domains.如果是 https,则您的证书需要涵盖两个域。

Yes it's appropriate to do nginx redirections, but i find doingPREPEND_WWW more simple.是的,做 nginx 重定向是合适的,但我发现做PREPEND_WWW更简单。

Add this in your settings.py将此添加到您的settings.py 中

PREPEND_WWW = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM