问题描述
在使用宝塔面板7.7配置网站的时候,发现网站开启反代时,再开启密码保护后。反向代理会直接失效,访问网站的时候,会直接转跳到默认的index.html页面。
问题定位
Basic Authentication文件
我们直接打开/www/server/panel/vhost/nginx/dir_auth/your_site可以看到宝塔为我们生成的 Basic Authentication 配置文件。
1 2 3 4 5 6
| location ~* ^/* { auth_basic "Authorization"; auth_basic_user_file /www/server/pass/your_site/demo.pass; }
|
我们可以清晰的看到,这个配置文件嵌套了一个location,并且他控制访问的路径是/。
网站配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| server { listen 80; listen 443 ssl http2; server_name your_site; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/your_site; if ($server_port !~ 443){ rewrite ^(/.*)$ https://$host$1 permanent; } ssl_certificate /www/server/panel/vhost/cert/your_site/fullchain.pem; ssl_certificate_key /www/server/panel/vhost/cert/your_site/privkey.pem; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; add_header Strict-Transport-Security "max-age=31536000"; error_page 497 https://$host$request_uri;
include /www/server/panel/vhost/nginx/dir_auth/your_site/*.conf;
location ~ /purge(/.*) { proxy_cache_purge cache_one $host$1$is_args$args; } include /www/server/panel/vhost/nginx/proxy/your_site/*.conf;
include enable-php-00.conf; include /www/server/panel/vhost/rewrite/your_site.conf; location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md) { return 404; } location ~ \.well-known{ allow all; } access_log /www/wwwlogs/your_site.log; error_log /www/wwwlogs/your_sitez.error.log; }
|
在这里,我们可以看到
1 2
| include /www/server/panel/vhost/nginx/dir_auth/your_site/*.conf;
|
1 2
| include /www/server/panel/vhost/nginx/proxy/your_site/*.conf;
|
引用秘钥的include 在引用反代的 include 之前。在加上秘钥中的location ~* ^/*,所有的请求就直接从这里走了,走不到反代的那一步。
解决方法
直接把 Basic Authentication 中像下面的代码删了即可。然后在网站配置文件中点击保存,是配置文件生效即可。
当然,如果你只想要控制访问某个路径的时候,需要密码验证。你就把下面的两行放置在相应的location中即可
1 2
| auth_basic "Authorization"; auth_basic_user_file /www/server/pass/your_site/demo.pass;
|
别忘记在宝塔网站的配置页面,点击保存。是的修改之后的文件生效。