هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
server {
listen 80;
listen [::]:80;
server_name site-a.example.com www.site-a.example.com;
root /var/www/site-a;
index index.html;
access_log /var/log/nginx/site-a.access.log;
error_log /var/log/nginx/site-a.error.log;
location / { try_files $uri $uri/ =404; }
}Step by step
- One file per site (quick answer) — copy the file for
site-band changeserver_name,rootand the log names. Debian/Ubuntu:sites-available+ a link insites-enabled. RHEL: one file per site in/etc/nginx/conf.d/NAME.conf. - Folders and rights — the files must be readable by the
nginx/www-datauser; on RHEL with SELinux, web content must have the right label (restorecon -Rv /var/www) or be under/usr/share/nginx/html. - A default server for unknown names — requests by IP or for a domain you do not host should not show site A by chance:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}444 closes the connection without an answer. Remove the distribution's own default site link.
- Test before DNS — with the
Hostheader (quick answer), or point the names to the server in your own/etc/hostsfor a browser test. - TLS for both — after DNS points to the server, certbot adds the HTTPS server blocks:
certbot --nginx -d site-a.example.com -d www.site-a.example.com
certbot --nginx -d site-b.example.com- See which block answers — list all server names Nginx knows:
- Keep sites apart — different system users and PHP-FPM pools (or containers) per site, so a hacked site A cannot read site B's files.
Check that it worked
curl -H 'Host: site-a.example.com' http://127.0.0.1/ and the same for site B return different pages; curl http://127.0.0.1/ (no known name) returns nothing (Empty reply from server); each site writes to its own access log; both work with HTTPS after certbot.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Both names show site A | server_name typo, or site B not linked in sites-enabled. | Check with nginx -T; add the link; reload. |
conflicting server name … ignored | The same name in two blocks. | Keep each name in one block only. |
403 Forbidden | No index file, or Nginx cannot read the folder (rights, SELinux). | Add index.html; fix rights; restorecon. |
| HTTPS shows the wrong certificate | Client without SNI, or no 443 block for that name. | Run certbot for every name; modern clients all send SNI. |
default_server error on reload | Two blocks claim default_server on the same port. | Only one default per port. |
| Changes do not show | Edited sites-available but not reloaded, or a browser cache. | nginx -t && systemctl reload nginx; test with curl. |
Go further
- Command pages: nginx, certbot, curl.
- Next how-tos: Get a free TLS certificate with Let's Encrypt, Redirect HTTP to HTTPS in Nginx, Cache static files in Nginx, Check the DNS records of a domain.
- Learn it: Traffic tools at work — Nginx, Web services — Nginx. Full guide: Nginx.
1How does Nginx know which website a request is for?
It compares the Host header of the request with the server_name of each server block.
2Why add a default server that returns 444?
Requests for unknown names or the bare IP then get no answer, instead of showing one of your sites by accident.