在站点范围内启用基本身份验证并为子页面禁用它?

Ben*_*end 36 nginx http-basic-authentication

我有一个相对简单的配置:

upstream appserver-1 {
    server unix:/var/www/example.com/app/tmp/gunicorn.sock fail_timeout=0;
}
server {
    listen  80;
    server_name  example.com;

    location / {
        proxy_pass http://appserver-1;
        proxy_redirect              off;
        proxy_set_header            Host $host;
        proxy_set_header            X-Real-IP $remote_addr;
        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

        auth_basic                  "Restricted";
        auth_basic_user_file        /path/to/htpasswd;

    }

    location /api/ {
        auth_basic          off;
    }
}
Run Code Online (Sandbox Code Playgroud)

目标是在整个网站上使用基本身份验证,除了在/api/子树上。虽然它确实适用于基本身份验证,但其他指令proxy_pass也不起作用/api/

是否可以仅禁用基本身份验证,同时保留其他指令而不复制和粘贴所有内容?

cjc*_*cjc 39

两个文件呢?

包括/proxy.conf 将是:

proxy_pass http://appserver-1;
proxy_redirect              off;
proxy_set_header            Host $host;
proxy_set_header            X-Real-IP $remote_addr;
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
Run Code Online (Sandbox Code Playgroud)

以及您当前的 conf 文件:

upstream appserver-1 {
    server unix:/var/www/example.com/app/tmp/gunicorn.sock fail_timeout=0;
}
server {
    listen  80;
    server_name  example.com;

    location / {
        auth_basic                  "Restricted";
        auth_basic_user_file        /path/to/htpasswd;
        include includes/proxy.conf;
    }

    location /api/ {
        auth_basic          off;
        include includes/proxy.conf;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 14

配置文件

在 Nginx 1.4.4 中,您需要offauth_basic设置加上引号。

location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/passwd;
        include /etc/nginx/uwsgi_params;
        uwsgi_pass unix:/tmp/app.sock;
}

location /api {
    auth_basic "off";
        include /etc/nginx/uwsgi_params;
        uwsgi_pass unix:/tmp/app.sock;
}
Run Code Online (Sandbox Code Playgroud)

创建您的 htpasswd/passwd 文件

安装apache2-utils,有一个很好的帮助应用程序可以非常快速地为您创建 htpasswd 文件。http://httpd.apache.org/docs/2.2/programs/htpasswd.html

htpasswd -c -m <filename> <username>
Run Code Online (Sandbox Code Playgroud)


小智 5

下面的配置适用于从我的磁盘共享文件夹,而无需对共享文件夹和站点的其余部分进行任何身份验证

server {
        listen       80;
        server_name  localhost;
        root C:\\Users\\Work\\XYZ\\;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        auth_basic "Administrator Login";
        auth_basic_user_file C:\\Users\\Work\\.htpasswd;

        location /share {
            auth_basic "off";
            allow all; # Allow all to see content 
            alias C:\\Users\\sg32884\\Work\\share\\;
        }
}
Run Code Online (Sandbox Code Playgroud)