无法在Digital Ocean上的django Nginx项目中将非www重定向到www

kognpnkq  于 2023-05-16  发布在  Nginx
关注(0)|答案(1)|浏览(221)

您好,如果有人能帮助我完成以下Nginx配置,我将不胜感激:
/etc/nginx/sites-available/example

server {
    server_name example.com *.example.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /home/admin/pyapps/example/example/static/;
    }

    location /static/admin/ {
        alias /home/admin/pyapps/example/example/static/admin/;
    }

    location /media/ {
        root /home/admin/pyapps/example;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = admin.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = backoffice.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name example.com *.example.com;
    return 404; # managed by Certbot
}

我想为www.example.com创建一个重定向example.com到https://www.example.com
注意我使用的是django-hosts。
我的django-hosts配置如下:
settings.py

ROOT_URLCONF = 'example.urls'
ROOT_HOSTCONF = 'example.hosts'
DEFAULT_HOST = 'www'
# DEFAULT_REDIRECT_URL = "http://www.example.com:8000"
DEFAULT_REDIRECT_URL = "http://www.example.com"
PARENT_HOST = 'example.com'
# HOST_PORT = '8000'

MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',
    ....
    'django_hosts.middleware.HostsResponseMiddleware',
]

hosts.py

from django.conf import settings
from django_hosts import patterns, host
from example.hostsconf import urls as redirect_urls
from . import admin_urls

host_patterns = patterns('',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'admin', admin_urls, name='admin'),
    host(r'backoffice', 'backoffice.urls', name="backoffice"),
    host(r'(?!www).*', redirect_urls, name='wildcard'),
)

hostsconf/urls.py

from django.urls import path
from .views import wildcard_redirect

urlpatterns = [
    path(r'^?P<path>.*)', wildcard_redirect),
]

hostsconf/views.py

from django.conf import settings
from django.http import HttpResponseRedirect

DEFAULT_REDIRECT_URL = getattr(settings, "DEFAULT_REDIRECT_URL", "http://www.example.com")

def wildcard_redirect(request, path=None):
    new_url = DEFAULT_REDIRECT_URL
    if path is not None:
        new_url = DEFAULT_REDIRECT_URL + '/' + path
    return HttpResponseRedirect(new_url)

我已经尝试了几乎所有的方法,但我认为这可能与django-hosts配置有关。
当我转到https://example.com时,它会显示“欢迎使用nginx”屏幕,而不是重定向到https://www.example.com

当我访问www.example.com或admin.example.com或backoffice.example.com时,网站加载正常。
有人请帮助我。我想重定向https://example.comhttps://www.example.com
让我知道如果你需要任何其他配置从我这里。

1cklez4t

1cklez4t1#

1.从您的第一个服务器块中删除example.com,因为如果没有HTTPS的子域,您将无法向此域提供任何服务。
1.将以下if语句添加到第二个服务器块,以将非www流量直接重定向到www HTTPS版本:if ($host = example.com) { return 301 https://www.example.com$request_uri; }
1.添加包含以下内容的第三个服务器块,以将HTTPS流量从非www重定向到www:

server {
     if ($host = example.com) {
         return 301 https://www.example.com$request_uri;
     }

     server_name example.com;
     listen [::]:443;  # if you would like to support IPv6
     listen 443;
     return 404;
 }

相关问题