Creating redirects with Nginx


To redirect a URL for a website running on an Nginx server, you must manually create a redirects.conf file. This article explains how to create this file and includes a few code examples you can add to it.

Redirecting your site from HTTP to HTTPS

The instructions in this article relate to redirecting requests to another location.

View the following article instead if you need to redirect HTTP requests to HTTPS:

Creating a redirect

  1. View the Nginx configuration file locations article to create your local /nginx/ directory.
  2. Change into this directory:
    [server]$ cd ~/nginx/
  3. Create a file named redirects.conf.
  4. Add code from one of the examples below.

    Make sure to change to your actual domain.

  5. Reload Nginx for the changes to take effect.

Redirecting a single file

The following code redirects requests from to

if ($request_filename ~ oldfile.html){
    rewrite ^ permanent;

Redirecting an entire site

The following code redirects all requests to your website site to

if ($request_filename ~ /*){
    rewrite ^ permanent;

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?