متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 197Web · Nginx

How to host two websites on one server

One small server can serve many websites: Nginx reads the Host header of each request and picks the matching server block (virtual host). Each site gets its own folder, logs and TLS certificate. This how-to adds two sites, catches unknown host names with a default server, gets certificates for both, and tests everything before the DNS change.

  • 5 الدروس
  • 8 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

197.1

The quick answer

/etc/nginx/sites-available/site-a.example.com
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; }
}
mkdir -p /var/www/site-a /var/www/site-b
ln -s /etc/nginx/sites-available/site-a.example.com /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/site-b.example.com /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx
curl -s -H 'Host: site-a.example.com' http://127.0.0.1/

197.2

Step by step

  1. One file per site (quick answer) — copy the file for site-b and change server_name, root and the log names. Debian/Ubuntu: sites-available + a link in sites-enabled. RHEL: one file per site in /etc/nginx/conf.d/NAME.conf.
  2. Folders and rights — the files must be readable by the nginx/www-data user; on RHEL with SELinux, web content must have the right label (restorecon -Rv /var/www) or be under /usr/share/nginx/html.
  3. A default server for unknown names — requests by IP or for a domain you do not host should not show site A by chance:
/etc/nginx/sites-available/00-default
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.

  1. Test before DNS — with the Host header (quick answer), or point the names to the server in your own /etc/hosts for a browser test.
  2. 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
  1. See which block answers — list all server names Nginx knows:
nginx -T 2>/dev/null | grep -E '^\s*(server_name|listen)'
  1. 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.

197.3

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.

الأوامر في هذا الدرس

197.4

Common problems

ProblemCauseFix
Both names show site Aserver_name typo, or site B not linked in sites-enabled.Check with nginx -T; add the link; reload.
conflicting server name … ignoredThe same name in two blocks.Keep each name in one block only.
403 ForbiddenNo index file, or Nginx cannot read the folder (rights, SELinux).Add index.html; fix rights; restorecon.
HTTPS shows the wrong certificateClient without SNI, or no 443 block for that name.Run certbot for every name; modern clients all send SNI.
default_server error on reloadTwo blocks claim default_server on the same port.Only one default per port.
Changes do not showEdited sites-available but not reloaded, or a browser cache.nginx -t && systemctl reload nginx; test with curl.

197.5

Go further

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.

كل الأوامر في هذا الفصل