Configuring Hugo to Serve from Multiple Domains and IPs with Nginx

Hosting your Hugo site across multiple domains and IPs is simple when combining Hugo’s flexibility with Nginx’s robust configuration capabilities. This guide walks you through the steps to set up your site seamlessly.


Step 1: Configure Hugo

  1. Set Up Your Hugo Site:

    • If you haven’t already, create a new Hugo site:
      hugo new site mysite
      
  2. Add Content and Themes:

    • Add content and apply your preferred themes to build your site.
  3. Set the Base URL:

    • Open your config.toml (or config.yaml/config.json) and set the baseURL for your primary domain:
      baseURL = "http://example.com/"
      

Step 2: Generate Static Files for Each Domain

Hugo lets you specify a custom baseURL for each domain when generating static files.

  1. Generate Static Files for Each Domain:
    • Use the --baseURL flag and set separate output directories for each domain:
      hugo --baseURL="http://example.com" -d public/example.com
      hugo --baseURL="http://example.org" -d public/example.org
      

Step 3: Set Up Nginx for Multiple Domains

Nginx allows you to serve multiple domains by creating separate server blocks.

  1. Install Nginx:

    • Install Nginx on your server:
      sudo apt-get install nginx
      
  2. Configure Nginx for Each Domain:

    • Create a configuration file for each domain in /etc/nginx/sites-available/.

      Example for example.com:

      server {
          listen 80;
          server_name example.com;
      
          root /path/to/your/hugo/site/public/example.com;
          index index.html;
      
          location / {
              try_files $uri $uri/ =404;
          }
      }
      

      Example for example.org:

      server {
          listen 80;
          server_name example.org;
      
          root /path/to/your/hugo/site/public/example.org;
          index index.html;
      
          location / {
              try_files $uri $uri/ =404;
          }
      }
      
  3. Enable the Configurations:

    • Create symbolic links in /etc/nginx/sites-enabled/ to activate each domain configuration:
      sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
      sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
      
  4. Restart Nginx:

    • Apply the configuration changes by restarting Nginx:
      sudo systemctl restart nginx
      

Step 4: Configure DNS Settings

Update your DNS records to point each domain to your server’s IP address:

  • A Records: Use your server’s IP for each domain.
  • CNAME Records: Point subdomains to your primary domain if needed.

Step 5: Test Your Configuration

After completing the setup:

  1. Navigate to http://example.com and http://example.org in your browser.
  2. Verify that the correct static files are served for each domain.
  3. Check that links and navigation work seamlessly.

Conclusion

With Hugo and Nginx, you can efficiently host a single site across multiple domains and IPs. By leveraging Hugo’s baseURL feature and Nginx’s flexible configuration, you ensure scalability and maintainability with minimal effort.


Chuck Norris Joke: When Chuck Norris configures Hugo to serve from multiple domains, those domains don’t just serve—they battle to be his favorite.


Related Posts