虚拟主机

域名、dns、 ip的关系
浏览器、nginx、http的关系
浏览器拿到请求以后,怎么向服务器发情请求?
image.png

多域名对应一个ip
image.png

域名解析与泛域名解析实战

打开电脑的hosts文件,添加

  1. 10.211.55.5 xiumutest.com

打开终端,ping xiumutest.com
image.png
已经配置成功了

阿里云域名解析 (需要买个域名)

这里是我在阿里云买的域名,如下做一个解析,就可以直接使用ngtest.xiumubai.com访问我们虚拟机的nginx网页。在浏览器输入http://ngtest.xiumubai.com 就可以看到我们启动的nginx页面了。
image.png

多端口多域名配置

1.在虚拟机根目录创建www文件

  1. mkdir www

2.在www下面创建ng-test文件夹

  1. mkdir ng-test

3.在ng-test下面分别创建foobar两个文件夹

  1. mkdir www

4.在foobar分别添加index.html,内容如下

  1. this a foo
  1. this a bar

5.在nginx.confg配置文件中配置

  1. #user nobody;
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. keepalive_timeout 65;
  11. server {
  12. listen 80;
  13. server_name localhost;
  14. location / {
  15. root /www/ng-test/bar/;
  16. index index.html index.htm;
  17. }
  18. error_page 500 502 503 504 /50x.html;
  19. location = /50x.html {
  20. root html;
  21. }
  22. }
  23. server {
  24. listen 88;
  25. server_name localhost;
  26. location / {
  27. root /www/ng-test/foo;
  28. index index.html index.htm;
  29. }
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root html;
  33. }
  34. }
  35. }

修改完了配置文件,记得重新加载一下nginx,使用systemctl reload nginx
现在我们使用http://ngtest.xiumubai.comhttp://ngtest.xiumubai.com:88就能访问到foobar页面了。

server_name 匹配

  1. server_name test.xiumubai.com test2.xiumubai.com; # 完整匹配
  2. server_name *.xiumubai.com; # 通配符匹配
  3. server_name www.xiumubai.*; # 通配符结束匹配
  4. server_name ~^[0-9]+\.xiumubai\.com$; # 正则匹配

多用户二级域名

*.xiumubai.com
如上配置以后,我们就可以使用任意的xxx.xiumubai.com访问