Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get part of the $host variable in nginx?

Tags:

nginx

I need to get only part of the $host variable. Domain is in the form img1.domain.com and I need to get "domain.com" and then use it in redirect.

I am trying it wrong like this:

$host ~* img[0-9]\.(.*);
set $host_without_img $1;

I know it would work, if I would put in in IF condition like this:

if ($host ~* img[0-9]\.(.*)) {
  set $host_without_img $1;
}

But I just don't want to use IF, when it is not necessary.

like image 815
Frodik Avatar asked Nov 12 '15 19:11

Frodik


1 Answers

You can use map, something like this:

map $host $host_without_img {
    default ...;
    ~*img[0-9]\.(?<x_host_without_img>.*) $x_host_without_img;
}
like image 190
Roman Avatar answered Oct 22 '22 23:10

Roman