Назад | Перейти на главную страницу

Nginx и Django используют домен с префиксом www

Пытаюсь понять, как заставить www. в моем домене и перенаправить что-нибудь вроде ww.example.com или test.example.com -> www.example.com

Когда я захожу на www.example.com или example.com, все работает нормально, я просто пытаюсь прикрыться, если люди пытаются перейти не на тот домен, и мне не нужно добавлять ничего, кроме «www.example.com» и « example.com 'в моем django ALLOWED_HOSTS

я все настроил согласно:

Как использовать установочный образ Django в один клик

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static; 
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

Вы не определили директиву перенаправления в nginx. Вот правильный config.

#here the redirect section. If server doesn't match www.example.com or example.com, it will redirected to http://www.example.com
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name _;

    return 301 http://www.example.com$request_uri;
}

#your main config files, handles request whenever Host header match www.example.com or example.com
server {
    listen 80;
    listen [::]:80;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name www.example.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static; 
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}