Welcome! Log In Create A New Profile

Advanced

How to access key in user set variable

Posted by OpenHaus 
How to access key in user set variable
March 27, 2010 02:10PM
Hello folks,

half way through configuring nginx I ran into a challenge I did not find a solution for, yet. Your help is greatly appreciated! I want to use the name of a given (but not known as there are many) subdomain by extracting the first three characters of the subdomain to build a directory path, e.g. an user requested to access http://world.wide.com/web.html. The file web.html now resides underneath the directory /srv/www/htdocs/wide.com/w/o/r/.

server {
listen 80;
server_name *.wide.com;

if ($host ~* "^([^.]+(\.[^.]+)*)\.wide.com$"){
set $usr $1;
}
...
}

Is it possible to extract "/w/o/r" from $usr which is set to "world" in the example above? If so how? I tried $usr(0), $usr[0], $usr{0} to access the first character and nothing seems to work.

If this does not work what would be an alternative? Please give me advice!

Thank you,
Fabio.

http://www.linux-showroom.com
Re: How to access key in user set variable [Solved]
April 09, 2010 12:23PM
This is actually fairly easy as soon as you learn of [i]Embeddable Perl[/i]. Your nginx binary has to be configured with --with-http_perl_module to support Perl this way.

Once that is done you can do something like that:

[code]
http {
perl_set $storage_path '
sub {
my $r = shift;
my $host = $r->header_in("Host");
return join "/", split(//, substr($host,0,3));
}
';
}
[/code]


In the server directive you can access the previously set $storage_path, e.g.

[code]
server {
location ~ /matchsomething$ {
root /srv/www/htdocs/$storage_path;
}
}
[/code]


HTH someone else,
Fabio.

http://www.linux-showroom.com



Edited 1 time(s). Last edit at 04/09/2010 12:24PM by OpenHaus.
Re: How to access key in user set variable
May 14, 2010 03:23AM
I have trouble on dreamhost serverconfiguration
I need someone that can handle dreamhost nginx configuration? and get the WP-MU proper working and also Stausnet. Do you have the experience or even config file ready??
thanks
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 271
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready