nginx 代理 springboot,Springboot 使用了 JWT 认证,HTTP头为 Authorization: Bearer {BASE64}
admin.netkiller.cn 后台需要限制登陆,公司没有固定IP地址,尝试了 VPN 方案,被封。最终决定使用 HTTP Auth,HTTP Auth 使用 HTTP Authorization: Basic {BASE64}。
问题来了,由于HTTP的 key 都是 Authorization,Authorization: Basic 会覆盖掉 Authorization: Bearer 导致 Springboot 无法认证返回 401.
使用下面👇配置解决,注意⚠️调试的时候需要每次关闭浏览器,否则会保留状态,不生效。
auth_basic off;
proxy_pass_request_headers on;
完成的例子
server {
listen 80;
listen 443 ssl http2;
server_name admin.netkiller.cn;
include /etc/nginx/default.d/*.conf;
access_log /var/log/nginx/admin.netkiller.cn.access.log;
error_log /var/log/nginx/admin.netkiller.cn.error.log;
error_page 497 https://$host$uri?$args;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
location / {
auth_basic "Administrator's Area";
auth_basic_user_file htpasswd;
root /opt/netkiller.cn/admin.netkiller.cn;
try_files $uri $uri/ /index.html;
index index.html;
}
location /api/ {
auth_basic off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://api.netkiller.cn:8080/;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}