时间:2023-07-17 13:27:01 | 来源:网站运营
时间:2023-07-17 13:27:01 来源:网站运营
nginx虚拟主机的配置:在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器会同时配置N个虚拟主机,这样可以充分利用服务器的资源,方便管理员的统一管理http{......省略其他代码 #第一台虚拟主机 server { listen 80; server_name a.jkyst.xyz; #这里域名一定不要重复 access_log logs/a.access.log; #日志需求可以根据自己的要求去做,如果觉得日志无所谓分不分开大可以放到一起 location{ root html/a; #这里是网站的根目录,注意为了测试一定要分开,里面写上不同的html index index.html index.htm; } ......这里省略其他代码 } #第二台虚拟主机 server{ listen 80; server_name b.jkyst.xyz; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......这里省略其他代码 } #第三台虚拟主机 server{ listen 80; server_name c.jkyst.xyz; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } }}
http{......省略其他代码 #第一台虚拟主机 server { listen 8000; server_name www.jkyst.xyz; access_log logs/a.access.log; location{ root html/a; index index.html index.htm; } ......省略其他代码 } #第二台虚拟主机 server{ listen 8001; server_name www.jkyst.xyz; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......省略其他代码 } #第三台虚拟主机 server{ listen 8002; server_name www.jkyst.xyz; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } ......省略其他代码 }}
http{......省略其他代码 #第一台虚拟主机 server { listen IP地址:80; server_name a.jkyst.xy或者ip地址; access_log logs/a.access.log; location{ root html/a; index index.html index.htm; } ......省略其他代码 } #第二台虚拟主机 server{ listen IP地址:80; server_name b.jkyst.xyz或者IP地址; access_log logs/b.access.log; location{ root html/b; index index.html index.htm; } ......省略其他代码 } #第三台虚拟主机 server{ listen IP地址:80; server_name c.jkyst.xyz或者IP地址; access_log logs/c.access.log; location{ root html/c; index index.html index.htm; } ......省略其他代码 }}
root@jia:~# lsb_release -aNo LSB modules are available.Distributor ID: UbuntuDescription: Ubuntu 19.10Release: 19.10Codename: eoanroot@jia:~# cat /proc/versionLinux version 5.3.0-18-generic (buildd@lcy01-amd64-027) (gcc version 9.2.1 20190909 (Ubuntu 9.2.1-8ubuntu1)) #19-Ubuntu SMP Tue Oct 8 20:14:06 UTC 2019
nginx版本:root@jia:~# nginx -vnginx version: nginx/1.16.1 (Ubuntu)
root@jia:/var/www/html# cat /etc/nginx/conf.d/default.conf server { listen 80; server_name a.jkyst.xyz; location / { root /var/www/html/a; index index.html index.htm; }}server { listen 80; server_name b.jkyst.xyz; location / { root /var/www/html/b; index index.html index.htm; }}server { listen 80; server_name c.jkyst.xyz; location / { root /var/www/html/c; index index.html index.htm; }
记住修改配置文件后一定要重新启动nginxroot@jia:~# systemctl restart nginx
root@jia:~# cd /var/www/html/root@jia:/var/www/html# mkdir a b c#生成主页文件root@jia:/var/www/html# echo a.jkyst.xyz > a/index.htmlroot@jia:/var/www/html# echo b.jkyst.xyz > b/index.htmlroot@jia:/var/www/html# echo c.jkyst.xyz > c/index.html
127.0.0.1 a.jkyst.xyz //127.0.0.1是nginx服务器的IP地址,我这里使用的是同一台PC127.0.0.1 b.jkyst.xyz 127.0.0.1 c.jkyst.xyz
root@jia:~# curl a.jkyst.xyz a.jkyst.xyzroot@jia:~# curl b.jkyst.xyz b.jkyst.xyzroot@jia:~# curl c.jkyst.xyz c.jkyst.xyz
OK发现访问的都是我想要得到的域名,证明基于域名的虚拟主机配置成功关键词:配置,主机,虚拟