我如何在 nginx Web 服务器的重写规则中将大写字母转换为小写字母?
我需要翻译地址:
www.example.com/TEST in ---> www.example.com/test
I need to translate the address:
www.example.com/TEST in ---> www.example.com/test
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,你将需要 Perl。
如果您使用的是
Ubuntu
,请使用apt-get install nginx-extras
,而不是apt-get install nginx-full
,它将具有嵌入式 Perl 模块。然后,在您的配置文件中:
Yes, you are going to need perl.
If you are using
Ubuntu
, instead ofapt-get install nginx-full
, useapt-get install nginx-extras
, which will have the embeddedperl
module.Then, in your configuration file:
我设法使用嵌入式 Perl 实现了目标:
i managed to achieve the goal using embedded perl:
根据 Adam 的回答,我最终使用了 lua,因为它在我的服务器上可用。
Based on Adam's answer, I ended up using lua, as it's available on my server.
位置可以由前缀字符串或正则表达式定义。正则表达式使用前面的“~*”修饰符(用于不区分大小写的匹配)或“~”修饰符(用于区分大小写的匹配)来指定。
来源: http://nginx.org/en/docs/http/ngx_http_core_module.html #位置
A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching).
Soruce: http://nginx.org/en/docs/http/ngx_http_core_module.html#location
我想指出大多数 Perl 答案都容易受到 CRLF 注入的影响。
您永远不应该在 HTTP 重定向中使用 nginx 的 $uri 变量。 $uri 变量需要标准化(更多信息),包括:
URL 解码是 CRLF 注入漏洞的原因。如果您在重定向中使用 $uri 变量,以下示例 url 将在您的重定向中添加恶意标头。
https://example.org/%0ASet-Cookie:MaliciousHeader:Injected
% 0A 被解码为 \n\r 并且 nginx 会将以下行添加到标头中:
安全 Perl 重定向需要替换所有换行符。
I would like to point out that most of Perl answers are vulnerable to CRLF injection.
You should never use nginx's $uri variable in a HTTP redirection. $uri variable is subject to normalization (more info), including:
URL decoding is the reason of CRLF injection vulnerability. The following example url would add a malicious header into your redirect, if you used $uri variable in the redirection.
https://example.org/%0ASet-Cookie:MaliciousHeader:Injected
%0A is decoded to \n\r and nginx will add into headers the following lines:
The secure Perl redirection requires to replace all newline characters.
使用 LUA 模块重定向。
Redirect with LUA module.
我在互联网上看到过这段代码,它确实有效。但是,它使用 nginx 服务器收到的主机名。对于 Kubernetes 基础设施,这可能与客户端使用的不同,因此重定向不仅会失败,还会泄露有关集群命名方案的私有信息。
这段代码对我有用,并且速度稍快一些,因为它使用内部重定向
它可以在 nginx 配置文件中使用,如下所示:
我的 Dockerfile 以这一行开头,不需要额外的包:
在本例中,您将需要复制静态文件站点到
/usr/share/nginx/html
例如:I've seen this code all over the internet, and it does work. However, it uses the host name that the nginx server receives. In the case of a Kubernetes infrastructure, this may not be the same as what the client uses so the redirect will not only fail but give away private information about the cluster naming scheme.
This code works for me and is slightly faster because it uses internal redirection
It might be used in an nginx configuration file like this:
My Dockerfile starts with this line and doesn't require additional packages:
In this example you will want to copy your static site to
/usr/share/nginx/html
for example: