How to specify the document root for a website in Nginx?

To specify the document root for a website in Nginx, you need to modify the server block in the nginx.conf file or the configuration file of your website.

  1. Open the nginx.conf file using a text editor. The location of this file may vary depending on your operating system and the installation of Nginx. Common locations are /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf.
  2. Within the http block, locate the server block that corresponds to your website's domain name or IP address. It will typically look like this:
server { listen 80; server_name example.com; ... }
  1. Inside this server block, add the root directive to specify the document root directory:
server { listen 80; server_name example.com; root /path/to/document/root; ... }

Replace /path/to/document/root with the actual path to your website's document root directory. For example, if your website's files are located in /var/www/example.com/, you would specify root /var/www/example.com/.

  1. Save the changes to the nginx.conf file.
  2. Restart or reload Nginx for the changes to take effect. The command to do this will depend on your operating system and how Nginx was installed. Common commands are sudo service nginx restart, sudo systemctl restart nginx, or sudo nginx -s reload.

After specifying the document root, Nginx will serve the files located in that directory when requests are made to your website.