时间:2023-07-17 11:39:01 | 来源:网站运营
时间:2023-07-17 11:39:01 来源:网站运营
nginx配置虚拟主机的详细步骤:虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。本文通过三种方法给大家介绍配置虚拟主机的方法,感兴趣的朋友跟随小编一起看看吧# 两张物理网卡ens32和ens34[root@nginx network-scripts]# ifconfig ens32 | awk 'NR==2 {print $2}' 192.168.126.41[root@nginx network-scripts]# ifconfig ens34 | awk 'NR==2 {print $2}' 192.168.126.42
编辑配置文件,基于每个IP创建一个虚拟主机# 为防止 /etc/nginx/conf.d/default.conf 配置文件影响,对其进行重命名[root@nginx ~]# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default [root@nginx ~]# vim /etc/nginx/conf.d/ip.conf# ens32网卡对应的虚拟主机server { listen 192.168.126.41:80; location / { root /ip_ens32; index index.html; }}# ens34 网卡对应的虚拟主机server { listen 192.168.126.42:80; location / { root /ip_ens34; index index.html; }}
创建虚拟主机的网页文件目录及文件[root@nginx ~]# mkdir /ip_ens32[root@nginx ~]# mkdir /ip_ens34[root@nginx ~]# echo "ens32" > /ip_ens32/index.html[root@nginx ~]# echo "ens34" > /ip_ens34/index.html
检查配置文件的语法[root@nginx ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
重载nginx服务[root@nginx ~]# systemctl reload nginx
测试[root@nginx ~]# curl 192.168.126.41ens32[root@nginx ~]# curl 192.168.126.42ens34
ip addr add IP/MASK dev 网卡名# 删除ip addr del IP/MASK dev 网卡名
其余步骤同上面多网卡多IP的配置[root@nginx ~]# vim /etc/nginx/conf.d/port.confserver { listen 81; location / { root /port_81; index index.html; }}server { listen 82; location / { root /port_82; index index.html; }}[root@nginx ~]# mkdir /port_{81..82}[root@nginx ~]# echo "81" > /port_81/index.html[root@nginx ~]# echo "82" > /port_82/index.html[root@nginx ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@nginx ~]# systemctl reload nginx
测试[root@nginx ~]# curl 192.168.126.41:8181[root@nginx ~]# curl 192.168.126.41:8282
[root@nginx ~]# vim /etc/nginx/conf.d/test1.dxk.com.confserver { listen 80; server_name test1.dxk.com; location / { root /test1; index index.html; }}[root@nginx ~]# vim /etc/nginx/conf.d/test2.dxk.com.confserver { listen 80; server_name test2.dxk.com; location / { root /test2; index index.html; }}[root@nginx ~]# mkdir /test{1..2}[root@nginx ~]# echo "test1" > /test1/index.html[root@nginx ~]# echo "test2" > /test2/index.html[root@nginx ~]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@nginx ~]# systemctl reload nginx
测试# 配置域名解析[root@nginx ~]# echo -e "192.168.126.41 test1.dxk.com/n192.168.126.41 test2.dxk.com" >> /etc/hosts[root@nginx ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.126.41 test1.dxk.com192.168.126.41 test2.dxk.com[root@nginx ~]# curl test1.dxk.comtest1[root@nginx ~]# curl test2.dxk.comtest2
[root@nginx ~]# vim /etc/hosts192.168.126.41 test1.dxk.com192.168.126.41 test3.dxk.com # 这里本应该是 test2.dxk.com ,但是由于写错了,而且对应test3.dxk.com域名的虚拟主机并不存在
访问该错误域名[root@nginx ~]# curl test3.dxk.comtest1# 可以看到,还是会返回网页信息
因为在配置域名解析时,虽然域名写错了,但是IP是对的,那么此时服务端默认会返回满足是该IP且端口为80的排在第一个的虚拟主机的网页信息给客户端[root@nginx ~]# ll /etc/nginx/conf.d/-rw-r--r--. 1 root root 112 Jul 3 21:23 test1.dxk.com.conf-rw-r--r--. 1 root root 112 Jul 3 21:22 test2.dxk.com.conf
这是需要注意的关键词:详细,步骤,主机,配置,虚拟