NGINX auth_request被忽略

Jan*_*Jan 6 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;
}
Run Code Online (Sandbox Code Playgroud)

因此,我希望http://localhost:5021/auth在尝试到达时可以访问(一个Flask应用)/。这不会发生,并且使用nginx-debug二进制文件,并且error_log /var/log/nginx/error.log debug;我从没看到期望看到的任何调试输出(请求的日志在此处)。没有错误,没有打印到控制台,也没有任何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
Run Code Online (Sandbox Code Playgroud)

Dus*_*jic 6

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


l'L*_*L'l 5

有一些事情可能会影响配置的正常工作。

  • 第一个是internal指令应该包含在/auth.
  • 其次,应该初始化一个NULL Content-Length
  • 第三$hostvs$http_host很重要;不正确的请求将不会被传递(见下文)
  • 另一个考虑是/在 URI 的最后一个路径之后包含一个尾随
    (我注意到如果深度 > 1,文档会以这种方式显示 URI)。

可以像这样传递未更改的“Host”请求标头字段:

proxy_set_header 主机 $http_host;

但是,如果客户端请求标头中不存在此字段,则不会传递任何内容。在这种情况下,最好使用 $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;
}
Run Code Online (Sandbox Code Playgroud)

  • 在赏金到期之前我没有时间尝试您的建议,但您的答案仍然很详细且高质量 (2认同)