change root directory Nginx

Asked

Viewed 299 times

1


goal: change the default root directory.


below I will demonstrate the current settings configured in order for someone to possibly understand and tell me what is wrong, missing...

observation that I will demonstrate here only the first line of the file Nginx.conf because it is big and would be unnecessary the whole code, follows the first line of the file /etc/Nginx/Nginx.conf:

user www-data;

filing cabinet /etc/Nginx/sites-enabled/website.conf:

server {
    listen 80;
    listen [::]:80;
    root /var/www/website;
    index index.htm index.html;
    server_name website;
    location / {
    autoindex on;
 }
}

permission and folder details /var/www/website

-rwxrwxrwx 1 root root 6 Nov 13 04:57 index.html


in this case I performed this command:

sudo chown -R root:root /var/www/website


I also tested like this:

sudo chown -R www-data:www-data /var/www/website


regardless of the two ñ succeeded, ask p/ that before negativar be supportive because I’m qse going hungry.

I hope that my question and demonstration of my failed configuration will be of easy understanding, in other words I hope I have been clear in my doubt, any attention upon response will be of great appraisal. Thanks


1 answer

1


This setting will only work for the domain name website, that is, when someone tries to access http://website

If Nginx is configured to run with the www-data user, then the correct user and group settings are these:

sudo chown -R www-data:www-data /var/www/website

It is outside the scope of your question, but to ensure that file and directory permissions are correct PROVIDED that you don’t have cgi scripts that should be executable (if you don’t know, you probably don’t have):

sudo find /var/www/website -type d -exec chmod 0755 '{}' \;
sudo find /var/www/website -type f -exec chmod 0644 '{}' \;

Your file index.html is with the permissions incorrect (rwxrwxrwx which is equivalent to 0777). The correct permissions for files are rw-r-r-- equivalent to 0644, and for directories rwxr-Xr-x equivalent to 0755.

If you only want to change the directory and the file you have:

sudo chmod 0755 /var/www/website
sudo chmod 0644 /var/www/website/index.html

Or else with these equivalent commands:

sudo chmod 'u=rwx,go=rx' /var/www/website
sudo chmod 'u=rw,go=r' /var/www/website/index.html

To set a default root directory for any and all sites that are not configured (for example if someone tries to access the web server using the ipv4 address), you need to create a default server block. I particularly in my installation create the following file:

Content of /etc/Nginx/conf. d/default-server.conf:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /var/www/default;
}

This setting will "capture" any and all sites that are not configured (the ones on /etc/Nginx/sites-enabled). If you can read in English, the official documentation explains how the requisitions are processed: http://nginx.org/en/docs/http/request_processing.html

The websites you set up in /etc/Nginx/sites-enabled will use another root directory as per each one’s configuration. But when Nginx can’t find the root directory of any site, it will use this default set in this setting.


Always remember that to test the configuration before restarting or reloading the service:

sudo nginx -t
  • 1

    Tks Uri, I followed what you said and tbm I visualized some Asian/English tutorials p/ better understanding worked out, regarding the chown command www-data seems obvious + 100 understanding visualized root in the default folder Nginx risked atoa the other command, finally solved strong hug (y)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.