本文介绍了nginx位置和Django身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于查询字符串中的URL参数创建NGINX重定向.基本上具有:

I'm trying to create a NGINX redirect based on an URL param in the querystring. Basically having:

http://localhost/redirect/?url=https://www.google.it/search?dcr=0&source=hp&q=django&oq=django

location /redirect/ {
    proxy_cache STATIC;
    # cache status code 200 responses for 10 minutes
    proxy_cache_valid 200 1d;
    proxy_cache_revalidate on;
    proxy_cache_min_uses 3;
    # use the cache if there's a error on app server or it's updating from another request
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
    # don't let two requests try to populate the cache at the same time
    proxy_cache_lock on;

    # Strip out query param "timestamp"
    if ($args ~ (.*)&timestamp=[^&]*(.*)) {
      set $args $1$2;
    }

    return 302 $arg_url$args;
}

现在,只有Django身份验证用户(JWT/Cookie)可以使用/redirect?url = 端点,因此可以在不打开整个代理的情况下实现会话/cookie检查世界?

Now, only Django authenticated users (JWT/Cookie) can use the /redirect?url= end point, hence is it possible to implement a session/cookie check without opening a proxy to the entire world?

无论如何,我都可以在Django级别上做到这一点( https://github.com/mjumbewu/django-proxy/blob/master/proxy/views.py ),但我认为它在NGINX级别上更快,计算量更少.

Anyway I could do it at the Django level (https://github.com/mjumbewu/django-proxy/blob/master/proxy/views.py) but I suppose it's faster and less computationally expensive at the NGINX level.

谢谢

D

推荐答案

基于先前的答案(谢谢!),这是解决方案:

Based on the previous answers (thanks!) this is the solution:

http {
    upstream app_api {
    # server 172.69.0.10:8000;
    server api:8000;
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response (in case the Unicorn master nukes a
    # single worker for timing out).
    # server unix:/var/www/gmb/run/gunicorn.sock fail_timeout=0;
  }

server {

    location = /auth {
      proxy_pass http://app_api/api-auth/login/;
      proxy_pass_request_body off;
      proxy_set_header Content-Length "";
      proxy_set_header X-Original-URI $request_uri;
    }

    location /redirect/ {
      auth_request /auth;

      proxy_cache STATIC;

      # cache status code 200 responses for 10 minutes
      proxy_cache_valid 200 1d;
      proxy_cache_revalidate on;
      proxy_cache_min_uses 3;
      # use the cache if there's a error on app server or it's updating from another request
      proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
      # don't let two requests try to populate the cache at the same time
      proxy_cache_lock on;

      # Strip out query param "timestamp"
      if ($args ~ (.*)&timestamp=[^&]*(.*)) {
        set $args $1$2;
      }
      return 302 $arg_url$args;
    }

这篇关于nginx位置和Django身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:53