Sub topic 1
Where Nginx configuration lives
Prerequisites
This chapter assumes you have:
- Nginx installed from your distribution's packages and running under systemd.
- Comfort with the Linux command line,
sudo, and a text editor. - A basic understanding of how DNS maps domain names to IP addresses.
Where Nginx configuration lives
On Debian and Ubuntu, the main file is /etc/nginx/nginx.conf. It includes other files, so most site settings live in two directories:
/etc/nginx/sites-available/holds every site file you write./etc/nginx/sites-enabled/holds symbolic links to the site files Nginx should load.
Other distributions may use a different layout. Check the include lines in nginx.conf to see what your system loads.
Server blocks: one Nginx, many sites
A server block is a server { ... } section that describes one site. A single Nginx process can hold many server blocks. When a request arrives, Nginx selects one block and uses its settings to answer.
Giving each site its own document root
The document root is the directory that holds a site's files. Give each site its own:
sudo mkdir -p /var/www/example.com/html
echo '<h1>example.com</h1>' | sudo tee /var/www/example.com/html/index.html
sudo chown -R www-data:www-data /var/www/example.comThen point the server block at that directory:
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
index index.html;
}On Debian and Ubuntu, the Nginx worker processes usually run as www-data, so they need read access to the files. A 403 error often means the permissions or the root path are wrong.
Choosing addresses and ports with listen
The listen directive sets the address and port that a server block accepts connections on. listen 80; accepts IPv4 traffic on port 80, and listen [::]:80; accepts IPv6 traffic. For HTTPS, use listen 443 ssl;.
Exercise
A server block contains listen 8080; and no other block listens on port 8080. Which URL on the same machine reaches this block?
Show answer
http://127.0.0.1:8080/. Port 80 does not reach it, because the listen directive sets the port.