nginx重写以替换单个字符 [英] nginx rewrite to replace single character
问题描述
我有一个工具可以将一堆我正在镜像的tarball和补丁下载到本地NGINX服务器,以加快下载过程,并让我们对所使用的内容进行更严格的控制.为了使该工具正常工作,本地文件名必须与远程文件名匹配(路径无关紧要),但是由于该工具认为文件名,我遇到了?
的问题.
I have a tool that downloads a bunch of tarballs and patches that I'm in the process of mirroring to a local NGINX server to speed up the download process and give us tighter control over what's being used. For the tool to work correctly, the local filename has to match the remote filename (path does not matter), but because of what the tool considers a filename, I've run into a snag with ?
.
一个示例将是下载此远程修补程序:
An example would be downloading this remote patch:
就工具而言,文件名是openssl-1.0.2d-parallel-build.patch?id=c8abcbe8de5d3b6cdd68c162f398c011ff6e2d9d
,中间有一个漂亮的?
,本地镜像必须是:
The filename, as far as the tool is concerned, is openssl-1.0.2d-parallel-build.patch?id=c8abcbe8de5d3b6cdd68c162f398c011ff6e2d9d
which has a nice little ?
in the middle and the local mirror has to be:
该工具使用wget下载文件,但不替换'?'用'%3f'表示,因此NGINX服务器返回404.如果您手动替换'?'如果使用'%3f',则wget可以正常工作.
The tool uses wget to download the file but does not replace the '?' with '%3f' so the NGINX server returns a 404. If you manually replace the '?' with '%3f' then the wget works normally.
使用NGINX服务器时不要将?
用作查询字符串,我会很好,但是,如果不可能的话,我需要一种重写uri的方法,以便文件能够正确下载.我以为会是这样的:
I would be fine with the NGINX server never treating the ?
as a query string but if that's not a possibility, I need a way to rewrite the uri so that the file downloads properly. I assumed it would be something like this:
rewrite ^(.*)\?(.*)$ $1%3f$2 permanent;
但这不能解决我的问题.镜像文件夹在/etc/nginx/apps/mirror.conf
中被定义为别名,如下所示:
but that doesn't resolve my issue. The mirror folder is defined as an alias in /etc/nginx/apps/mirror.conf
as follows, if that matters:
location /mirror {
autoindex on;
alias /var/lib/dl-cache;
}
可以在缩小差距方面有所帮助.
Could use a little help bridging the gap.
推荐答案
location
和rewrite
指令使用已分隔?
和查询字符串的规范化URI.
The location
and rewrite
directives use a normalised URI which has already separated out the ?
and the query string.
变量$request_uri
包含未修改的URI,可以使用if...return
语句进行查询:
The variable $request_uri
contains the unmodified URI and can be interrogated using an if...return
statement:
if ($request_uri ~ ^(/.*)[?](.*)$) {
return 301 $1%3f$2;
}
您可能希望将其放置在location
块中以限制范围.请参见此警告有关使用if
You may want to place this inside a location
block to limit the scope. See this caution on the use of if
这篇关于nginx重写以替换单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!