Welcome! Log In Create A New Profile

Advanced

Handy config snippet: Redirect to canonical server name

Jeff Waugh
July 31, 2009 02:26PM
Hi all,

Here's a handy config snippet to permanently redirect to a canonical server
name, generically (ie. without needing to repeat yourself in every config).


/etc/nginx/canonical_hostname:
if ( $host != $server_name ) {
rewrite ^(.*)$ $scheme://$server_name$1 permanent;
}


/etc/nginx/sites-enabled/example.com:
server {
listen 10.0.0.1:80;
server_name www.example.com example.com;
include canonical_hostname;
...


Because the included block in canonical_hostname is totally generic, you can
include it in whichever server block you wish. It automagically chooses the
correct scheme, host (based on the first element of server_name) and path...

Enjoy!

- Jeff

--
linux.conf.au 2010: Wellington, NZ http://www.lca2010.org.nz/

http://www.xach.com/debian-users-are-beatniks.html
Subject Author Posted

Handy config snippet: Redirect to canonical server name

Jeff Waugh July 31, 2009 02:26PM

Re: Handy config snippet: Redirect to canonical server name

Igor Sysoev August 07, 2009 04:18AM

Re: Handy config snippet: Redirect to canonical server name

Jeff Waugh August 07, 2009 08:19PM

Re: Handy config snippet: Redirect to canonical server name

Igor Sysoev August 08, 2009 02:53AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 165
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