NGINX auth_request被忽略

时间:2019-02-17 12:41:33

标签: nginx

我的nginx.conf中包含以下内容:

location / {
    auth_request /auth;
    add_header Content-Type text/plain;
    return 200 'You are in!';
}

location = /auth {
    proxy_pass http://localhost:5021/auth;
    proxy_pass_request_body off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Original-URI $request_uri;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Authorization $http_authorization;
    proxy_pass_header Authorization;
}

因此,我希望在尝试访问http://localhost:5021/auth时可以访问/(一个Flask应用程序)。不会发生这种情况,并且使用nginx-debug二进制文件和error_log /var/log/nginx/error.log debug;我从没见过I expect to see的任何调试输出(请求日志可用here)。没有错误,没有打印到控制台,也没有任何nginx的日志文件。似乎auth_request行只是被默默忽略。

nginx-debug -V的输出(它确实包含--with-http_auth_request_module):

nginx version: nginx/1.15.8
built by gcc 6.4.0 (Alpine 6.4.0) 
built with OpenSSL 1.0.2q  20 Nov 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-stream_geoip_module=dynamic --with-http_slice_module --with-mail --with-mail_ssl_module --with-compat --with-file-aio --with-http_v2_module --with-debug

5 个答案:

答案 0 :(得分:1)

return将无条件停止处理并将指定的代码返回给客户端,您可能希望将其删除。

答案 1 :(得分:1)

有几件事可能会影响配置,使其无法正常工作。

  • 第一个为internal指令应包含在/auth中。
  • 第二个 NULL Content-Length应该被初始化。
  • 第三,$host$http_host很重要;错误的请求将不会通过(请参阅下文)
  • 另一个考虑因素是在URI的最后路径之后包含一个结尾/
    (我注意到,如果深度> 1,文档将以这种方式显示URI)。

不变的“主机”请求标头字段可以像这样传递:

  

proxy_set_header主机$ http_host;

     

但是,如果此字段不是   出现在客户端请求标头中,则不会传递任何内容。在   在这种情况下,最好使用$ host变量-其值等于   “主机”请求标头字段中的服务器名称或主要   服务器名称(如果不存在此字段):​​

     

proxy_set_header主机$ host;

     

此外,服务器名称可以是   与代理服务器的端口一起传递:

     

proxy_set_header主机$ host:$ proxy_port;

最终配置应类似于以下内容:

location / {
    auth_request /auth;
    auth_request_set $auth_status $upstream_status;
}

location = /auth {
    internal;
    proxy_pass http://localhost:5021/auth/;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header Host $host;
    proxy_set_header X-Original-URI $request_uri;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Authorization $http_authorization;
    proxy_pass_header Authorization;
}

答案 2 :(得分:0)

您的代码看起来绝对正确,return语句没有错误。 自auth_request起,nginx模块在​​version 1.5.4中可用,但默认情况下未编译。

请检查是否使用以下命令在nginx支持下编译了您安装的auth_request版本:

nginx -V 2>&1 | grep -qF -- --with-http_auth_request_module && echo "Module Compiled" || echo "Not Compiled"

如果它是Not Compiled,则默认情况下未构建此模块,因此应使用--with-http_auth_request_module配置参数将其启用,如下所示:

./configure --with-http_auth_request_module

使用以下命令重新加载Nginx服务     service nginx reload,然后再次点击http://localhost。 这应该对您有用。

答案 3 :(得分:0)

相反

return 200 'You are in!';

您可以使用以下代码

echo -n "You are in!";

答案 4 :(得分:0)

你应该使用这个:

server {
    listen 80;
    server_name 127.0.0.1;

    location / {
        return 200 "You are in!";
    }
}

server {
    srever_name ...;

    location / {
        auth_request /auth;

        proxy_pass_request_headers on;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        ...

        proxy_pass http://127.0.0.1:80;
}

location = /auth {
    internal;
    proxy_pass http://localhost:5021/auth/;
    ...
} 

location / {
    auth_request /auth;

    try_files notExistsfile @verified;
}

location @verified {
    internal;
    return 200 "You are in!";
}
相关问题