nginx force rewrite of full address

On my enterprise intranet, websites can be accessed in two ways. With their alias, like https://foo or with their full address, something like https://foo.example.internal.

I want to configure my nginx server to force user to use the second option. So if they browse https://foo, I want the nginx server to rewrite the address to https://foo.example.internal.

I've tried several options:

http {
  server {
    server_name foo;

    # Test 1
    rewrite ^https://foo/(.*)$ https://foo.example.internal/$1;

    # Test 2
    rewrite ^/foo/(.*)$ https://foo.example.internal/$1;

    # Test 3
    rewrite ^/(.*) https://foo.example.internal/$1;
  }
}

Test1 and Test2 has no effect (i.e. the user may access the website on https://foo or https://foo.example.internal), while Test3 does the rewrite from https://foo to https://foo.example.internal but the latest does not work (nothing is returned by the nginx server, I suspect that there is some loop in that URL rewrite).

So, how can I force nginx to rewrite https://foo to https://foo.example.internal?

Thanks

1
задан 30 April 2018 в 11:13
1 ответ

Наконец-то проблема обнаружена. Рабочее решение достигается путем установки 2 серверов:

http {

  server {
    server_name foo;
    listen 443 ssl;

    return 301 https://foo.example.internal$request_uri;
  }

  server {
    server_name foo.example.internal;
    listen 443 ssl;

    # rest of configuration
  }


}
1
ответ дан 3 December 2019 в 23:17

Теги

Похожие вопросы