How to configure Nginx to serve a static HTML file?

To configure Nginx to serve a static HTML file, you need to follow these steps:

  1. Install Nginx: If Nginx is not already installed, you can install it using the package manager for your operating system. For example, on Ubuntu, you can use the following command sudo apt-get install nginx.

  2. Create the HTML file: Create a static HTML file that you want to serve. You can create the file using a text editor and save it with a .html extension. For example, create a file called index.html.

  3. Move the HTML file to the appropriate folder: The default root folder for serving files on Nginx is usually /var/www/html/ or /usr/share/nginx/html/. Move the HTML file to the appropriate folder using the following command: sudo mv index.html /var/www/html/ or sudo mv index.html /usr/share/nginx/html/.

  4. Configure Nginx: Open the Nginx configuration file using a text editor. The configuration file is usually located at /etc/nginx/nginx.conf. You can use the following command to open the file: sudo nano /etc/nginx/nginx.conf.

  5. Update the server block: Locate the server block in the configuration file. It should look something like this:

server { listen 80 default_server; listen [::]:80 default_server; ... }

Inside the server block, add a location block to specify the location of your static HTML file. For example:

server { listen 80 default_server; listen [::]:80 default_server; ... location / { root /var/www/html; index index.html; } }
  1. Save and close the configuration file: Save the changes made to the configuration file and close the text editor.

  2. Restart Nginx: Finally, restart the Nginx service to apply the configuration changes. Use the following command: sudo systemctl restart nginx or sudo service nginx restart.

After following these steps, Nginx should be configured to serve the static HTML file you specified. You can access it by opening a web browser and entering the IP address or domain name of your server.