Skip to content

NGINX Redirects

Gabriel Fernandes de Araujo Costa edited this page Jun 19, 2018 · 1 revision

It is possible to configure both temporary and permanent redirects using nginx.

Temporary redirects (response status code 302 Found) are useful if a URL temporarily needs to be served from a different location. For example, if you are performing site maintenance, you may wish to use a temporary redirect of from your domain to an explanation page to inform your visitors that you will be back shortly.

Example: rewrite ^/oldlocation$ http://www.newdomain.com/newlocation redirect;

Permanent redirects (response status code 301 Moved Permanently), on the other hand, inform the browser that it should forget the old address completely and not attempt to access it anymore. These are useful when your content has been permanently moved to a new location, like when you change domain names.

Example: rewrite ^/oldlocation$ http://www.newdomain.com/newlocation permanent;

Nginx server standard structure:

server {
  listen 80;listen [::]:80;
  server_name example.com;
  location / {
    root /var/www/example.com/public;
    index index.html index.php;
  }
}
  • This configuration listens to port 80 and sets the server name to example.com.
  • The location / {} section sets the root folder of the website and the index files.

Including a redirect in the structure:

server {
  listen 80;listen [::]:80;
  server_name example.com;
  rewrite ^/folder/file$ http://www.newdomain.com/folder2/file2 redirect;
  location / {
    root /var/www/example.com/public;
    index index.html index.php;
  }
}

Redirecting according to a HTTP header:

map $http_accept $file_type {
  default def;
  text/html html;
  application/rdf+xml rdf;
  application/ld+json jsonld;
  text/turtle ttl;
}
if ($file_type = def) {
    rewrite ^/namespace/domain/$ http://docs.oasis-open.org/domain.html redirect;
}
if ($file_type = html) {
    rewrite ^/namespace/domain/$ http://docs.oasis-open.org/domain.html redirect;
}
if ($file_type = rdf) {
    rewrite ^/namespace/domain/$ /namespace/domain/domain.rdf redirect;
}
if ($file_type = jsonld) {
    rewrite ^/namespace/domain/$ /namespace/domain/domain.jsonld redirect;
}
if ($file_type = ttl) {
    rewrite ^/namespace/domain/$ /namespace/domain/domain.ttl redirect;
}
server {
  listen 80;listen [::]:80;
  server_name example.com;
  location / {
    root /var/www/example.com/public;
    index index.html index.php;
  }
}
  • The first step to implement HTTP header redirects is to configure a map with the desired headers and the expected values.
  • The default (def) value will be set to any unmapped value received in the http_accept header and therefore redirected in the if ($file_type = def) {} statemet.
  • When performing a GET request using the http_accept header, the request will be redirected to the respective file.
  • BE CAREFUL WHEN USING IF STATEMENTS IN NGINX, YOU SHOULD AVOID USING IT INSIDE A LOCATION {} SECTION. For more info, see https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/