Redirect azurewebsites.net to your custom domain

1 minute read | By Kendrick Dubuisson

How to update Nginx to redirect to your custom domain.

App Services on Linux supports a number of built-in images to help fast track your development & If the runtime your application requires is not supported in the built-in images we also support building your own docker images & deploying this to App Services on Linux.

Depending on the path taken, Nginx may be the webserver used within the image & might require some additional configuration. Below, we’ll be walking through how to update the Nginx configuration file to redirect your users from azurewebsites.net & to your custom domain.

Getting Started

  1. Using Azure Portal, open a new SSH session. (https://<your-site-name>.scm.azurewebsites.net/webssh/host) Azure Portal - App Service SSH"

  2. Since we are using VIM below are a few of the basic commands we’ll reference.

    VIM Command Description
    i Enter Insert Mode
    <Esc> key Enter command mode
    <Esc> + :wq Save and quit Vim
    <Esc> + :q! Force quit Vim discarding all changes
    [[ or gg Move to the beginning of a file
    ]] or G Move to the end of a file

Updating Nginx Default.conf - Server Block

  1. Using vim Go to “/home/etc/nginx/conf.d/default.conf” & here we can see the default server configuration block. On line ten we should see “server_name _;” but instead we’ll update this with our custom domain of kdubuis.com which should be seen as “server_name kdubuis.com;”. Nginx server block using new custom domain of "kdubuis.com"

Updating Nginx Default.conf - Location Block

  1. On line 58 we’ll find the location block which we will update a new condition to redirect to our custom domain.

      if ( $host != $server_name ) {
      return 301 $scheme://kdubuis.com$request_uri;
       }
    

    Nginx location block with redirect logic

Saving Changes

  1. After the changes have been made & the files are saved, use command below to restart the Nginx Service & load the new configuration so that your redirects should be working as expected.
    nginx -s reload