Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure nginx to auto redirect to the main hostname?

Each nginx config can act for a wide range of domains but I want to auto-redirect requests to the first domain name (the official one).

server {
    server_name a.example.com b.example.com;
}

I want that if someone enters b.example.com/some, to go directly to a.example.com/some

like image 640
sorin Avatar asked May 10 '12 10:05

sorin


People also ask

What is Nginx permanent redirect?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.

How do I automatically redirect http to https nginx?

Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.

How does Nginx know which domain to redirect to?

Nginx will check first www.example.com and if not matched all what ever other subdomains will be redirected to www one. fastcgi_script_name? All incoming requests, that don’t find a matching server will be served by the default server, which will redirect them to the correct domain.

How do I change the default URL in Nginx?

Restart the Nginx web server to put the changes into effect using the command: sudo systemctl restart Nginx If you wish to redirect from non-www to www, simply replace the website URL’s mentioned in the above command. Replace www.devisers.in with devisers.in and vice versa.

What is Nginx server in Linux?

Introduction Nginx (pronounced “Engine-X”) is a Linux-based web server and proxy application. Nginx is a powerful tool for redirecting and managing web traffic. It can be easily configured to redirect unencrypted HTTP web traffic to an encrypted HTTPS server.

How do I access Nginx from the command line?

PuTTY is a free utility which will allow command-line access to your server. The Nginx config location, as you may expect, is in /etc/nginx. You can type cd nginx followed by ls to view them all, but know that the main file you’ll be working with is nginx.conf. Before we dive into things, it’s worth pinning down some terminology.


2 Answers

This is pretty much the same thing as the GOOD example for http://wiki.nginx.org/Pitfalls#Server_Name. That is, you should use two servers:

server {
  server_name b.example.com;
  return 301 $scheme://a.example.com$request_uri;

  # For pre-0.8.42 installations:
  # rewrite ^ $scheme://a.example.com$request_uri? permanent;
}

server {
  server_name a.example.com;
  # Do stuff
}
like image 104
kolbyjack Avatar answered Sep 28 '22 18:09

kolbyjack


To do this in a single server block, you can use an if and the $server_name variable:

    server_name primary.tld secondary.tld;
    if ($host != $server_name) {
        rewrite ^ $scheme://$server_name permanent;
    }

Or, to keep any query parameters:

    server_name primary.tld secondary.tld;
    if ($host != $server_name) {
        rewrite ^/(.*) $scheme://$server_name/$1 permanent;
    }

Here, $server_name refers to primary server name, which is the first name in the server_name directive, while $host refers to the hostname given in the HTTP request.

Note that the if statement in nginx configuration does not always do what you would expect and its use is discouraged by some. See also https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

This answer was inspired by this answer to another question which uses a similar aproach.

like image 44
Matthijs Kooijman Avatar answered Sep 28 '22 18:09

Matthijs Kooijman