欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Nginx虚拟主机

程序员文章站 2022-07-03 14:13:47
...

Nginx虚拟主机

虚拟主机介绍

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。
Nginx虚拟主机
nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。
1、基于域名的虚拟主机 。(server_name来区分虚拟主机——应用:外部网站)
2、基于ip的虚拟主机 。(一块主机绑定多个ip地址)
3、基于端口的虚拟主机 。(端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

虚拟主机配置

基于域名

[aaa@qq.com] vim /etc/nginx/conf.d/nginx.conf
#将nginx主配置文件/etc/nginx/nginx.conf中的server模块注释掉,在conf.d目录下自己创建
#要确定http模块中有include /etc/nginx/conf.d/*.conf这句话
server {
    listen         80;
    server_name    web.xiaobai.com;
    location / {
        root    /var/www/nginx/;
        index   index.html index.htm;
        }
}
server {
    listen         80;
    server_name    www.little.com;
    location / {
        root    /little/html;
        index    index.html index.htm;
        }
}

[aaa@qq.com] mkdir -p /little/html
[aaa@qq.com] mkdir -p /var/www/nginx/html
[aaa@qq.com] vim /var/www/nginx/index.html
xiaobai
[aaa@qq.com] vim /little/html/index.html
little
[aaa@qq.com] nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[aaa@qq.com] systemctl start nginx

客户端配置解析,在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行
192.168.226.128 web.xiaobai.com
192.168.226.128 www.little.com


不要忘记关闭nginx机器上的防火墙。

基于ip

[aaa@qq.com] ip a
    inet 192.168.226.128/24 brd 192.168.226.255 scope global noprefixroute dynamic ens33

[aaa@qq.com] ip a add dev ens33 192.168.226.100/24
[aaa@qq.com] ip a
    inet 192.168.226.128/24 brd 192.168.226.255 scope global noprefixroute dynamic ens33
    inet 192.168.226.100/24 scope global secondary ens33

[aaa@qq.com] mv /etc/nginx/conf.d/nginx.conf /etc/nginx/conf.d/nginx.conf.bak
[aaa@qq.com] vim /etc/nginx/conf.d/nginx.conf
server {
      listen       80;
      server_name  192.168.226.128;
      location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
      }
}        
server {
      listen       80;
      server_name  192.168.226.100;
      location / {
            root   /little/html/;
            index  index.html index.htm;
      }
}

[aaa@qq.com] vim /var/www/nginx/index.html
192.168.226.128
[aaa@qq.com] vim /little/html/index.html
192.168.226.100
[aaa@qq.com] nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[aaa@qq.com] systemctl restart nginx

Nginx虚拟主机


Nginx虚拟主机

[aaa@qq.com] ip a del dev ens33 192.168.226.100/24
#删除ip

基于端口

[aaa@qq.com] mv /etc/nginx/conf.d/nginx.conf /etc/nginx/conf.d/nginx.conf.bak2
[aaa@qq.com] vim /etc/nginx/conf.d/nginx.conf
server {
      listen       80;
      server_name  web.xiaobai.com;
      location / {
            root   /var/www/nginx/;
            index  index.html index.htm;
      }
}
server {
      listen       8080;
      server_name  www.little.com;
      location / {
            root   /little/html/;
            index  index.html index.htm;
      }
}

[aaa@qq.com] vim /var/www/nginx/index.html
xiaobai
[aaa@qq.com] vim /little/html/index.html
little
[aaa@qq.com] nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[aaa@qq.com] systemctl restart nginx
#之前已经解析过了,直接域名+端口访问就行

文章总览分类目录 @小白

相关标签: Nginx nginx