操作或分割字符串 [英] Manipulate or split string

查看:75
本文介绍了操作或分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的问题,如果可能的话,它将为我解决一些问题:

I've a quick question, that would solve me some problems if possible:

是否可以使用nginx拆分/操作请求网址?

Is it possible to split / manipulate the request-url with nginx?

我的意思是:这样的网址:sub.somewhere.com/something/somethingelse

What I mean is: an url like this: sub.somewhere.com/something/somethingelse

变成了:

次要的东西

然后进一步进入:

sub/som/eth/ing/som/eth/ing/els/e

sub/som/eth/ing/som/eth/ing/els/e

然后使用给定的路径来检索文件(因此,很可能必须将其存储在可以重复使用或直接使用的变量中)

And then the given path is used to retrieve a File (so probably, it has to be stored in a variable that can be re-used, or used directly)

这有可能吗?或者,如果没有,到底有什么可能?限制在哪里?

Is this possible somehow? Or if not, what exactly would be possible, and where are the limitations?

(编辑),包括PERL模块在内,是否有本机的可能性?还是那是唯一的方法? (也许是一个只进行字符串处理的较小模块?)

推荐答案

这是可能且相对容易的,您要做的就是将正则表达式上的位置与适当的反向引用进行匹配

it is possible and relatively easy, all you need to do is match your location on a regexp with the approriate back references

location ~ (sub).(somewhere).(com)/(some)(thing)/(something)(else) {
  set $var1 = $1; # =sub in above example
  set $var2 = $2; # =somewhere in above example
  set $var3 = $3; # =com in above example
  set $var4 = $4; # =some in above example
  set $var5 = $5; # =thing in above example
  set $var6 = $6; # = something in above example
  set $var7 = $7; # = elsein above example
  rewrite ^ $1/$2 last; # would be sub/somewhere
}

您需要在重写之前保存反向引用,因为rewrite指令会将引用重置为regexp第一个参数中的引用(因此,如果您使用其他诸如try_files这样的指令不起作用,那么您可以直接使用反向引用而不使用保存)

you need to save the backreferences before the rewrite because the rewrite directive resets the references to those in the regexp first argument (so if you use some other directive like try_files that doesn't do that you coul just use the backreferences directly without saving them)

这篇关于操作或分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆