Docker + Gunicorn + Nginx + Django:将非 www 重定向到 AWS Route 53 上的 www
Posted
技术标签:
【中文标题】Docker + Gunicorn + Nginx + Django:将非 www 重定向到 AWS Route 53 上的 www【英文标题】:Docker + Gunicorn + Nginx + Django: redirect non-www to www on AWS Route 53 【发布时间】: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/;
【问题讨论】:
【参考方案1】:是的,在网络服务器中进行此类重定向是合适的。如果是 https,则您的证书需要涵盖两个域。
【讨论】:
【参考方案2】:是的,做 nginx 重定向是合适的,但我发现做 PREPEND_WWW 更简单。
将此添加到您的 settings.py
PREPEND_WWW = True
【讨论】:
以上是关于Docker + Gunicorn + Nginx + Django:将非 www 重定向到 AWS Route 53 上的 www的主要内容,如果未能解决你的问题,请参考以下文章
Docker + Gunicorn + Nginx + Django:将非 www 重定向到 AWS Route 53 上的 www
如何在 gunicorn(django) + nginx + docker 中更改缓冲区大小
Docker Image与uWSGI / Gunicorn + Nginx for CentOS中的Flask应用程序