googlenginx

Access an application using only a single domain with NGINX

The default Bitnami NGINX server configuration allows you to access the server using the domain name or using the IP address directly.

To redirect all these domains to your own domain, modify the existing HTTP and HTTPS configuration in the /opt/bitnami/nginx/conf/bitnami/bitnami.conf file and add the lines below in each case. Remember to replace example.com with your own domain and note that the URL prefix is http or https depending on which section is being modified:

  # add to HTTP section
  if ($host != "example.com") {
      return 301 http://example.com$request_uri;
  }

  # add to HTTPS section
  if ($host != "example.com") {
      return 301 https://example.com$request_uri;
  }

For example:

  # HTTP server

  server {
      listen       80;
      server_name  localhost;
      if ($host != "example.com") {
          return 301 http://example.com$request_uri;
      }
  ...
  }

  # HTTPS server

  server {
     listen       443 ssl;
     server_name  localhost;
     if ($host != "example.com") {
         return 301 https://example.com$request_uri;
     }
     ssl_certificate      server.crt;
  ...
  }

Then, restart the NGINX server for the changes to take effect.

Last modification December 21, 2022