Forum: NGINX Nginx rewrite host

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
103e3ecb1c838c6c7e52a0ee12ee6e1a?d=identicon&s=25 Glen Lumanau (Guest)
on 2009-02-11 05:58
(Received via mailing list)
Hello!



How can I create this rule

If host = 127.0.0.1, 127.0.0.2

Do nothing



If host != 127.0.0.1, 127.0.0.2

Rewrite bla bla bla



Thank you for any help
Cada0e1ce6247850bf38269d43c58963?d=identicon&s=25 Kingsley Foreman (Guest)
on 2009-02-11 09:38
(Received via mailing list)
if ($host != blah.com){
                        rewrite  ^(.*)$ http://blah.com$1 break;
                }




From: Glen Lumanau
Sent: Wednesday, February 11, 2009 3:14 PM
To: nginx@sysoev.ru
Subject: Nginx rewrite host


Hello!



How can I create this rule

If host = 127.0.0.1, 127.0.0.2

Do nothing



If host != 127.0.0.1, 127.0.0.2

Rewrite bla bla bla



Thank you for any help
103e3ecb1c838c6c7e52a0ee12ee6e1a?d=identicon&s=25 Glen Lumanau (Guest)
on 2009-02-11 09:44
(Received via mailing list)
Hello!



How can I add 2 or more host?



From: owner-nginx@sysoev.ru [mailto:owner-nginx@sysoev.ru] On Behalf Of
Kingsley Foreman
Sent: 11 Februari 2009 15:26
To: nginx@sysoev.ru
Subject: Re: Nginx rewrite host



                if ($host != blah.com){
                        rewrite  ^(.*)$ http://blah.com$1 break;
                }





From: Glen Lumanau <mailto:glen@lumanau.web.id>

Sent: Wednesday, February 11, 2009 3:14 PM

To: nginx@sysoev.ru

Subject: Nginx rewrite host



Hello!



How can I create this rule

If host = 127.0.0.1, 127.0.0.2

Do nothing



If host != 127.0.0.1, 127.0.0.2

Rewrite bla bla bla



Thank you for any help
52ec6f2d0206837902876df1c72e906d?d=identicon&s=25 Tomasz Pajor (Guest)
on 2009-02-11 10:41
(Received via mailing list)
if ($host !~* "(blah.com|blah2.com)") {
    rewrite  ^(.*)$ http://blah.com$1 break;
}

Reference:
http://wiki.codemongers.com/NginxHttpRewriteModule#if

--
Tom Pajor
5640e332954fc0006aea97a155ce0afd?d=identicon&s=25 Igor Sysoev (Guest)
on 2009-02-11 10:48
(Received via mailing list)
On Wed, Feb 11, 2009 at 10:31:38AM +0100, Tomasz Pajor wrote:

> if ($host !~* "(blah.com|blah2.com)") {
>    rewrite  ^(.*)$ http://blah.com$1 break;
> }
>
> Reference:
> http://wiki.codemongers.com/NginxHttpRewriteModule#if

Never! Never use "if" just to test $host.
There is special very optimized directives server/server_name:

     server {
          server_name  blah1.com  blah2.com;
          rewrite  ^   http://blah.com$request_uri;
     }
52ec6f2d0206837902876df1c72e906d?d=identicon&s=25 Tomasz Pajor (Guest)
on 2009-02-11 11:16
(Received via mailing list)
So from You are saying is should replace:

server {
    listen 8080;
    location / {
        set $my_host $host;
        if ($host ~* "(.+\.nl.test.com|.+\.test.nl|test.nl)") { rewrite
^ http://nl.test.com$request_uri last; }
        proxy_pass http://apps; proxy_set_header Host $my_host;
    }
}

with

server {
    listen 8080;
    server_name *.nl.test.com test.nl *.test.nl;
    rewrite ^ http://nl.test.com$request_uri last;
}

yes?

--
Tom Pajor
5640e332954fc0006aea97a155ce0afd?d=identicon&s=25 Igor Sysoev (Guest)
on 2009-02-11 11:23
(Received via mailing list)
On Wed, Feb 11, 2009 at 11:06:17AM +0100, Tomasz Pajor wrote:

> }
>
> with
>
> server {
>    listen 8080;
>    server_name *.nl.test.com test.nl *.test.nl;
>    rewrite ^ http://nl.test.com$request_uri last;
> }
>
> yes?

Yes. And "test.nl" and "*.test.nl" can be combined in one ".test.nl".

Also you do not need
        set $my_host $host;

use just
        proxy_pass       http://apps;
        proxy_set_header Host $host;
103e3ecb1c838c6c7e52a0ee12ee6e1a?d=identicon&s=25 Glen Lumanau (Guest)
on 2009-06-10 10:09
(Received via mailing list)
It's working if we are redirecting the traffic to onother domain.

But what i need is something like

        if ($host != www.mydomain.com){
            rewrite  ^(.*)$ http://www.mydomain.com$1 break;
        }

Is there any way to do this?
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2009-06-10 11:14
(Received via mailing list)
Hello!

On Wed, Jun 10, 2009 at 02:59:15PM +0700, Glen Lumanau wrote:

> It's working if we are redirecting the traffic to onother domain.
>
> But what i need is something like
>
>         if ($host != www.mydomain.com){
>             rewrite  ^(.*)$ http://www.mydomain.com$1 break;
>         }
>
> Is there any way to do this?

It's just question of writing correct server{} declarations.

    server {
        listen 80 default;
        server_name_in_redirect off;

        rewrite ^ http://www.mydomain.com$request_uri?;
    }

    server {
        server_name www.mydomain.com;
        ...
    }

Maxim Dounin
103e3ecb1c838c6c7e52a0ee12ee6e1a?d=identicon&s=25 Glen Lumanau (Guest)
on 2009-06-10 11:35
(Received via mailing list)
Thank's a lot!

It's working like a charm
This topic is locked and can not be replied to.