%% date:: [[2021-05-10]], [[2021-02-09]], [[2024-01-08]], [[2024-09-14]] parent:: %% # [[Setting up server blocks on Nginx]] Server blocks are a way to use a single machine to serve content for multiple domains. In [[Apache Software Foundation|Apache]] servers, they are called "virtual hosts". By default, Nginx only has one server block, and serves data from the directory `/var/www/html`. Having server blocks just means configuring it so that it serves from `/var/www/domain.com/html` instead. Essentially, it allows [[Namespacing]]. ## Create the target directory `sudo mkdir -p /var/www/notes.nicolevanderhoeven.com/html` ## Assign ownership to user `sudo chown -R nic:nic /var/www/notes.nicolevanderhoeven.com/html` `sudo chmod -R 755 /var/www/notes.nicolevanderhoeven.com` (just to make sure the permissions are correct) ## Create an index page for testing `nano /var/www/notes.nicolevanderhoeven.com/html/index.html` Add content to index page, such as this basic html: ```html <html> <head> <title>Welcome to notes.nicolevanderhoeven.com!</title> </head> <body> <h1>Success! The notes.nicolevanderhoeven.com server block is working!</h1> </body> </html> ``` ## Configure Nginx to serve from this new directory `sudo nano /etc/nginx/sites-available/notes.nicolevanderhoeven.com` and add this configuration block, which points to the right server name: ```json server { listen 80; listen [::]:80; root /var/www/notes.nicolevanderhoeven.com/html; index index.html index.htm index.nginx-debian.html; server_name notes.nicolevanderhoeven.com www.notes.nicolevanderhoeven.com; location / { try_files $uri $uri/ =404; } } ``` This updates the directory to your new one and also sets `server_name` to the domain. Enable the config file by linking to it from the `sites-enabled` directory: `sudo ln -s /etc/nginx/sites-available/notes.nicolevanderhoeven.com /etc/nginx/sites-enabled/` At this point, you've actually created two server blocks: - One for `domain.com` and `www.domain.com` - One for everything else (listens on port 80 for anything that doesn't match the first one) ## Adjust hash bucket size To avoid an issue with hash bucket memory due to the multiple server blocks, open the Nginx configuration file: `sudo nano /etc/nginx/nginx.conf` and un-comment the line: ````bash server_names_hash_bucket_size 64; ```` Save and close the file. ## Verify your Nginx configuration `sudo nginx -t` You should get back something like this: ```bash nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful ``` ## Restart Nginx `sudo systemctl restart nginx` ## Point your domain to the IP address Add a DNS (A) record pointing `domain.com` or `notes.nicolevanderhoeven.com` and `www.domain.com` or `www.notes.nicolevanderhoeven.com` if needed to the IP address of your new Nginx machine. For example, on [[DigitalOcean]]: ![[A record.png]] It might take a while for these changes to be propagated. Check [DNS Checker](https://dnschecker.org) to see the correct IP address. Don't move on to the next step until this has happened. ## Verify that the server block has been correctly configured Navigate to your new domain (`http://notes.nicolevanderhoeven.com` or `http://domain.com`). You should see the HTML that you saved earlier. ## Troubleshooting server blocks - [[HTTP 404 on Nginx]] ## See Also - [[Nginx]] - [[Setting up Nginx as a reverse proxy]] - [[Installing Nginx on Ubuntu]] - [[Installing an SSL certificate on Nginx server with Let's Encrypt and Certbot]]