Welcome! Log In Create A New Profile

Advanced

variable for domain name in redirect

Posted by shiz 
shiz
variable for domain name in redirect
July 01, 2018 10:00AM
Hello,

I have something in one of my server bloc:


```
# switch to TLS for page.php, contact.php, faq.php, known https sources
and /
if ($scheme = http) {
set $rule_9 1$rule_9;
}
if ($outdated = 0){
set $rule_9 2$rule_9;
}
if ($request_uri ~
"^/(auction|biglemon|carview(-bike)?|carsensor(-c)?|daihatsu|gazoo|goo(bike(-catalog)?|net)|honda|koubai|kuriyama-truck|kurumaerabi(-search)?|truck-reparts(-b)?|truskey|list1|pdns|rakuten|page|faq|contact(_japan)?|jpa(-b|L1)?|_jpa[23])\.php|^/(\?.*)?$")
{
set $rule_9 3$rule_9;
}
if ($rule_9 = "3210"){
return 301 https://www.server.com$request_uri;
}
```

Then something similar in another

```
# switch to TLS for page.php, contact.php, faq.php, known https sources
and /
if ($scheme = http) {
set $rule_9 1$rule_9;
}
if ($outdated = 0){
set $rule_9 2$rule_9;
}
if ($request_uri ~
"^/(auction|biglemon|carview(-bike)?|carsensor(-c)?|daihatsu|gazoo|goo(bike(-catalog)?|net)|honda|koubai|kuriyama-truck|kurumaerabi(-search)?|truck-reparts(-b)?|truskey|list1|pdns|rakuten|page|faq|contact(_japan)?|jpa(-b|L1)?|_jpa[23])\.php|^/(\?.*)?$")
{
set $rule_9 3$rule_9;
}
if ($rule_9 = "3210"){
return 301 https://dev.server.com$request_uri;
}
```

The problem is that have several servers.

1 - Is there a way to use variable for the some server names in the 'return
301' line so I could use shared code and not have to edit this snippet
everywhere another site turns https?

2 - I have a lot of those return 301 where code could be shared if something
like this were available e.g.
$scheme://$actual_real_server_name$request_uri

Difficulty, the server_name can and often contains multiple entries.

I have found nothing useful about this on serverfault nor anywhere else.
Dealing with this for several years.

Thanks!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,280353,280353#msg-280353

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Maxim Dounin
Re: variable for domain name in redirect
July 01, 2018 05:20PM
Hello!

On Sun, Jul 01, 2018 at 03:52:59AM -0400, shiz wrote:

> I have something in one of my server bloc:

[...]

> if ($rule_9 = "3210"){
> return 301 https://dev.server.com$request_uri;
> }
> ```
>
> The problem is that have several servers.
>
> 1 - Is there a way to use variable for the some server names in the 'return
> 301' line so I could use shared code and not have to edit this snippet
> everywhere another site turns https?
> 2 - I have a lot of those return 301 where code could be shared if something
> like this were available e.g.
> $scheme://$actual_real_server_name$request_uri
>
> Difficulty, the server_name can and often contains multiple entries.
>
> I have found nothing useful about this on serverfault nor anywhere else.
> Dealing with this for several years.

In most cases, $server_name is the variable you want. This
variable contains the name of the server as defined by the
server_name directive, see http://nginx.org/r/$server_name. If
there are multiple names defined, it will contain the first one.

In some cases, you may also want to use the $host variable. It
will contain the name as obtained from the client, and might be
better than $server_name if a single server{} block is used to
handle requests to multiple different names and you don't want
nginx to return redirects to a canonical name, but prefere to keep
names separate.

--
Maxim Dounin
http://mdounin.ru/
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
shiz
Re: variable for domain name in redirect
July 01, 2018 10:00PM
> In most cases, $server_name is the variable you want.

Thanks so much. Works like a charm. This simplifies my
configuration/maintenance a lot.

Best!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,280353,280360#msg-280360

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login