0x1 写在前面
今儿一早,一个大佬在我们主题群里发了一个好玩儿的音乐盒子,可以播放和下载几乎全网所有Music,本着好奇害死猫的心态玩一玩,于是就有了这篇文章。
- 找到大佬博客
- 下载大佬写好的代码
- 建立子目录
- 上传并解压代码到子目录
- 输入域名/子目录
duangduangduang
这TM不科学啊,F12检查发现所有CSS、JS全部都指向根目录了,WTF。
于是开始折腾代码,一通捣鼓之后发现我访问的地址是~~https://www.v2ex.cc/Tool/Music-api~~ ,怎么后面没有斜杠(/)呢?
可别小看子目录下的这么个小斜杠(/),没有它你可访问不到子目录的资源了。
0x2 求助度娘
无赖之下求助度娘(吐槽一下度娘的搜索结果千篇一律)
在茫茫网站之中找到一种说法如下:
说是Nginx参数中有个关于目录优化的东东把这个斜杠去掉了
optimize_server_names off;#优化服务器名称:关 (默认开启)
server_name_in_redirect off;#服务器名称重定向:关(默认开启)
懒得动Nginx配置的我就只能另辟蹊径用301重定向来解决了。
在Rewrite规则中加入如下语句
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
如果端口不是80则使用下列语句
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$http_host/$1$2/ permanent;
}
规则写完,重启Nginx使规则生效后再次访问,这次子目录根据规则在后面加上了斜杠(/),问题解决。
晒出一张音乐盒子的截图
安利一下,有需要下载音乐的请手动点击后面链接:~~https://www.v2ex.cc/Tool/Music-api/~~
0x3 假装有总结
Nginx官方参数说明:
optimize_server_names (优化服务器名称)
syntax: optimize_server_names [ on|off ]
default: optimize_server_names on
context: http, server
Directive activates or deactivates optimization of host name checks for name-based virtual servers.
In particular, the check influences the name of the host used in redirects. If optimization is on, and all name-based servers listening on one address:port pair have identical configuration, then names are not checked during request execution and redirects use first server name.
If redirect must use host name passed by the client, then the optimization must be turned off.
Note: this directive is deprecated in nginx 0.7.x, use server_name_in_redirect instead.server_name_in_redirect (服务器名称重定向)
syntax: server_name_in_redirect on|off
default: server_name_in_redirect on
context: http, server, location
If server_name_in_redirect is on, then Nginx will use the first value of the server_name directive for redirects. If server_name_in_redirect is off, then nginx will use the requested Host header.
Note: for Location headers coming from an upstream proxy (via proxy_pass for example) this may not be the only directive you need. In fact, it seems to be ignored a lot of the time. If you are seeing the upstream\'s server name come through and not be rewritten, you will need to use proxy_redirect to rewrite the upstream\'s provided hostname to what you want. Something like proxy_redirect http://some.upstream.url/ / - you will want to rewrite it to a / relative path.