Nginx基本认证

时间:2019-03-17 05:50:25

标签: django nginx

我使用Django和Nginx

我在配置中添加了以下条目,以限制对example.com/admin/的访问

该函数要求输入密码,然后一切正常,但是之后,我收到Nginx的 404 Not Found错误

完整配置

    upstream rates_core_server {

  server unix:/webapps/example.com_app/example.com/run/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
    server_name example.com www.example.com;

    client_max_body_size 4G;

    access_log /webapps/example.com_app/logs/nginx-access.log;
    error_log /webapps/example.com_app/logs/nginx-error.log;

    location /admin/ {
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
     }

    location /static/ {
        alias   /webapps/example.com_app/example.com/static/;
        client_max_body_size 100M;
    }

    location /media/ {
        alias   /webapps/example.com_app/example.com/static/media/;
        client_max_body_size 100M;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://example.com_server;
            break;
        }
    }

    # Error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
        root /webapps/example.com_app/example.com/static/;
    }
}

我不明白问题是什么

1 个答案:

答案 0 :(得分:0)

使用当前配置,nginx不知道在何处查找或重定向到admin块。您是否也可以在您的管理块中包含proxy_pass设置,例如:

location /admin/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_redirect off;

    if (!-f $request_filename) {
        proxy_pass http://example.com_server;
        break;
    }

    auth_basic "Restricted Content";
    auth_basic_user_file /etc/nginx/.htpasswd;
}