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
Set Up Your Hugo Site:
- If you haven’t already, create a new Hugo site:
hugo new site mysite
- If you haven’t already, create a new Hugo site:
Add Content and Themes:
- Add content and apply your preferred themes to build your site.
Set the Base URL:
- Open your
config.toml
(orconfig.yaml
/config.json
) and set thebaseURL
for your primary domain:baseURL = "http://example.com/"
- Open your
Step 2: Generate Static Files for Each Domain
Hugo lets you specify a custom baseURL
for each domain when generating static files.
- 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
- Use the
Step 3: Set Up Nginx for Multiple Domains
Nginx allows you to serve multiple domains by creating separate server blocks.
Install Nginx:
- Install Nginx on your server:
sudo apt-get install nginx
- Install Nginx on your server:
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; } }
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/
- Create symbolic links in
Restart Nginx:
- Apply the configuration changes by restarting Nginx:
sudo systemctl restart nginx
- Apply the configuration changes by restarting 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:
- Navigate to
http://example.com
andhttp://example.org
in your browser. - Verify that the correct static files are served for each domain.
- 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.