@dimaQd

Django LocaleMiddleware redirect не работает на nginx gunocorn, но с debug True все ок, в чем может быть дело?

Привет! На локальной машине или в debug true на боевой машине при запросе на условный site.ai/ переходит автоматический редирект на site.ai/ru/ или, например, при site.ai/home/ на site.ai/ru/home. В debug false редирект не происходит, но nginx передает Accept-Language $http_accept_language. В чем может быть дело, куда копать?

MIDDLEWARE = [
        "django.contrib.sessions.middleware.SessionMiddleware",
        "corsheaders.middleware.CorsMiddleware",
        "django.middleware.common.CommonMiddleware",
        "django.middleware.csrf.CsrfViewMiddleware",
        "django.contrib.auth.middleware.AuthenticationMiddleware",
        "django.contrib.messages.middleware.MessageMiddleware",
        "django.middleware.clickjacking.XFrameOptionsMiddleware",
        "django.middleware.security.SecurityMiddleware",
        "django.middleware.locale.LocaleMiddleware",
        "wagtail.contrib.redirects.middleware.RedirectMiddleware",
        "debug_toolbar.middleware.DebugToolbarMiddleware",
    ]


upstream botto_django {
    server web:8000;
}

server {
    listen 80;
    server_name servername;

    location / {
        proxy_set_header Accept-Language $http_accept_language;
        return 301 https://$host$request_uri;
    }

}

server {
    listen 443 ssl;
    server_name sitename;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    ssl_certificate /etc/letsencrypt/live/botto.ai/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/botto.ai/privkey.pem;

    location / {
        proxy_pass http://botto_django;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Accept-Language $http_accept_language;
    }

    location /media/ {
        alias /usr/src/app/media/;
    }

    location /static/ {
        alias /usr/src/app/static/;
        expires 30d;
    }

    client_max_body_size 64M;
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
}
  • Вопрос задан
  • 122 просмотра
Решения вопроса 1
@dimaQd Автор вопроса
Я переписал "django.middleware.locale.LocaleMiddleware", чтобы вывести принты того, что происходит.

class LocaleMiddleware(MiddlewareMixin):
    """
    Parse a request and decide what translation object to install in the
    current thread context. This allows pages to be dynamically translated to
    the language the user desires (if the language is available).
    """

    response_redirect_class = HttpResponseRedirect

    def process_request(self, request):
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        (
            i18n_patterns_used,
            prefixed_default_language,
        ) = is_language_prefix_patterns_used(urlconf)
        print(urlconf, 'urconf')
        language = translation.get_language_from_request(
            request, check_path=i18n_patterns_used
        )
        print(language, 'get_language_from_request')
        language_from_path = translation.get_language_from_path(request.path_info)
        print(language_from_path, 'language_from_path')
        if (
                not language_from_path
                and i18n_patterns_used
                and not prefixed_default_language
        ):
            language = settings.LANGUAGE_CODE
            print(language, """if not language_from_path
                and i18n_patterns_used
                and not prefixed_default_language""")
        translation.activate(language)
        request.LANGUAGE_CODE = translation.get_language()
        print(request.LANGUAGE_CODE, 'translation.get_language')

    def process_response(self, request, response):
        language = translation.get_language()
        print('process_response: ', language, 'translation.get_language()')
        language_from_path = translation.get_language_from_path(request.path_info)
        print('language_from_path: ', language_from_path)
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        (
            i18n_patterns_used,
            prefixed_default_language,
        ) = is_language_prefix_patterns_used(urlconf)
        print(urlconf, 'urconf')
        print(i18n_patterns_used, prefixed_default_language, 'i18n_patterns_used, prefixed_default_language')
        if (
                response.status_code == 404
                and not language_from_path
                and i18n_patterns_used
                and prefixed_default_language
        ):
            print("""response.status_code == 404
                and not language_from_path
                and i18n_patterns_used
                and prefixed_default_language""")
            # Maybe the language code is missing in the URL? Try adding the
            # language prefix and redirecting to that URL.
            language_path = "/%s%s" % (language, request.path_info)
            print(request.path_info, 'request.path_info')
            print(language_path, '(language, request.path_info)')
            path_valid = is_valid_path(language_path, urlconf)
            print(path_valid, 'path_valid')
            path_needs_slash = not path_valid and (
                    settings.APPEND_SLASH
                    and not language_path.endswith("/")
                    and is_valid_path("%s/" % language_path, urlconf)
            )

            if path_valid or path_needs_slash:
                script_prefix = get_script_prefix()
                print(script_prefix, 'script_prefix')
                # Insert language after the script prefix and before the
                # rest of the URL
                language_url = request.get_full_path(
                    force_append_slash=path_needs_slash
                ).replace(script_prefix, "%s%s/" % (script_prefix, language), 1)
                print(language)
                # Redirect to the language-specific URL as detected by
                # get_language_from_request(). HTTP caches may cache this
                # redirect, so add the Vary header.
                redirect = self.response_redirect_class(language_url)
                print(redirect.url, 'redirect')
                patch_vary_headers(redirect, ("Accept-Language", "Cookie"))
                return redirect

        if not (i18n_patterns_used and language_from_path):
            print('if not (i18n_patterns_used and language_from_path)')
            patch_vary_headers(response, ("Accept-Language",))
        response.headers.setdefault("Content-Language", language)
        print(response)
        return response


Вот так выглядит запрос с включенным debug True:

spoiler
botto.urls urconf
ru get_language_from_request
None language_from_path
ru translation.get_language
[21/May/2023 10:19:41] "GET / HTTP/1.1" 302 0
process_response: ru translation.get_language()
language_from_path: None
botto.urls urconf
True True i18n_patterns_used, prefixed_default_language
response.status_code == 404
and not language_from_path
and i18n_patterns_used
and prefixed_default_language
/ request.path_info
/ru/ (language, request.path_info)
ResolverMatch(func=wagtail.views.serve, args=('',), kwargs={}, url_name='wagtail_serve', app_names=[], namespaces=[], route='ru/((?:[\\w\\-]+/)*)$') path_valid
/ script_prefix
ru
/ru/ redirect
botto.urls urconf
ru get_language_from_request
ru language_from_path
ru translation.get_language
process_response: ru translation.get_language()
language_from_path: ru
botto.urls urconf
True True i18n_patterns_used, prefixed_default_language

[21/May/2023 10:19:41] "GET /ru/ HTTP/1.1" 200 74412


А вот так, когда debug False
spoiler
botto-web-1 | [2023-05-21 10:21:42 +0300] [10] [DEBUG] GET /
botto-web-1 | botto.urls urconf
botto-web-1 | ru get_language_from_request
botto-web-1 | None language_from_path
botto-web-1 | ru translation.get_language
botto-web-1 | process_response: ru translation.get_language()
botto-web-1 | language_from_path: None
botto-web-1 | botto.urls urconf
botto-web-1 | True True i18n_patterns_used, prefixed_default_language
botto-web-1 | if not (i18n_patterns_used and language_from_path)
botto-web-1 |
botto-nginx-1 | 89.20.8.102 - - [21/May/2023:07:21:42 +0000] "GET / HTTP/1.1" 500 378 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0" "-"

По какой-то причине не приходит код 302

Добавил следующий код в nginx и все заработало:
map $http_accept_language $lang {
        default en;
        "~*^((|,)\s*(?!(ru|es|fr|pt|en))\w+(-\w+)?(;q=[\d\.]+)?)*(|,)\s*en\b" en;
        "~*^((|,)\s*(?!(ru|es|fr|pt|en))\w+(-\w+)?(;q=[\d\.]+)?)*(|,)\s*es\b" es;
        "~*^((|,)\s*(?!(ru|es|fr|pt|en))\w+(-\w+)?(;q=[\d\.]+)?)*(|,)\s*ru\b" ru;
        "~*^((|,)\s*(?!(ru|es|fr|pt|en))\w+(-\w+)?(;q=[\d\.]+)?)*(|,)\s*fr\b" fr;
        "~*^((|,)\s*(?!(ru|es|fr|pt|en))\w+(-\w+)?(;q=[\d\.]+)?)*(|,)\s*pt\b" pt;
        "~*(^|,)\s*(uk|be|ky|kk|ba|tt|uz|sr|mk|bg)\b" ru;
        "~*(^|,)\s*(ca|gl)\b" es;
    }
location / {
 proxy_pass http://botto_django$request_uri;
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы