前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx应用场景之虚拟主机

Nginx应用场景之虚拟主机

原创
作者头像
Hellboycc
修改2020-09-22 17:49:38
7060
修改2020-09-22 17:49:38
举报

一、业务需求

在公司企业环境中,有业务服务A、业务服务B、业务服务C等等,这样就面临多套业务都需要往线上部署,并且都需要通过nginx进行web服务的反向代理或者web服务的配置,那么多套业务该如何来进行配置和部署呢?

二、部署方式

  • 每个服务对应一个nginx服务,该方式需要启动多个nginx进程,每个nginx进程处理一个服务,效率较低,浪费资源,在真实线上环境中不推荐该方式。
  • 采用虚拟主机配置,该方式可以使用同一个nginx进程处理多个业务,占用资源少,效率高,配置相对比较灵活,在真实线上环境中强烈推荐使用。

三、什么是虚拟主机

什么叫做虚拟主机配置呢,用一句话概括就是在同一个nginx上运行多个单独不同的业务,每个业务之间彼此独立,互不干扰。

虚拟主机配置.jpg
虚拟主机配置.jpg

四、虚拟主机常见配置场景

  • 基于主机多IP方式
主机多IP.jpg
主机多IP.jpg

在同一个nginx主机上分别配置了三个不同的IP,服务A、B、C分别通过三个不同的IP进行访问,这样就实现了主机多IP的配置方式,具体配置如下:在nginx主机上配置多个IP地址

代码语言:txt
复制
[root@node1 conf.d]# ip a add 192.168.30.11/24 dev eth1
[root@node1 conf.d]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
...
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a4:b0:78 brd ff:ff:ff:ff:ff:ff
    inet 192.168.30.10/24 brd 192.168.30.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet 192.168.30.11/24 scope global secondary eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fea4:b078/64 scope link
       valid_lft forever preferred_lft forever

进入nginx配置目录分别创建vserver1.confvserver2.conf

代码语言:txt
复制
[root@node1 ~]# cd /etc/nginx/conf.d/
[root@node1 conf.d]# ls
default.conf  vserver1.conf  vserver2.conf
[root@node1 conf.d]#

/opt/app/code/opt/app/code1目录下均创建server.html文件

代码语言:txt
复制
[root@node1 conf.d]# cat /opt/app/code/server.html

<h1>Hello, vserver1<h1>
[root@node1 conf.d]# cat /opt/app/code1/server.html

<h1>Hello,vserver2<h1>
[root@node1 conf.d]#

编辑vserver1.confvserver2.conf,将server模块下listen参数改为对应的IP地址,并将locationroot目录分别改为/opt/app/code/opt/app/code1

代码语言:txt
复制
server {
    listen       192.168.30.10;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
	root   /opt/app/code;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

检查nginx语法配置并重启nginx服务

代码语言:txt
复制
[root@node1 conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
[root@node1 conf.d]# ps -aux|grep nginx
root     25919  0.0  0.4  47256  2088 ?        Ss   08:34   0:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx    26227  0.0  0.4  47260  2060 ?        S    11:15   0:00 nginx: worker process
root     26231  0.0  0.1 112712   972 pts/0    S+   11:15   0:00 grep --color=auto nginx
[root@node1 conf.d]#

通过浏览器输入http://192.168.30.10/server.html 和 http://192.168.30.11/server.html进行访问, 一个页面输出Hello,vserver1 一个页面输出Hello,vserver2,基于多IP的配置方式成功。

  • 基于端口的方式
基于端口虚拟主机配置.jpg
基于端口虚拟主机配置.jpg

在同一个nginx主机上,对于不同虚拟主机让其监听不同端口,服务A、B、C分别通过同一IP不同端口进行访问,这样就实现了基于端口虚拟主机的配置方式,具体配置如下:编辑vserver1.confvserver2.conf,将server模块下listen参数改为对应监听的端口即可

代码语言:txt
复制
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
	root   /opt/app/code;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

检查nginx语法配置并重启nginx服务

代码语言:txt
复制
[root@node1 conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
[root@node1 conf.d]# ps -aux|grep nginx
root     25919  0.0  0.4  47256  2088 ?        Ss   08:34   0:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx    26227  0.0  0.4  47260  2060 ?        S    11:15   0:00 nginx: worker process
root     26231  0.0  0.1 112712   972 pts/0    S+   11:15   0:00 grep --color=auto nginx
[root@node1 conf.d]#

通过浏览器输入http://192.168.30.10:80/server.html和http://192.168.30.11:81/server.html进行访问, 一个页面输出Hello,vserver1 一个页面输出Hello,vserver2,基于端口的配置方式成功。

  • 基于多host名称方式(多域名,通过HTTP协议实现)
基于hostname的配置.jpg
基于hostname的配置.jpg

HTTP协议中,host信息会通过客户端的request请求携带并发送给服务端,在同一个nginx主机上收到客户端的请求信息后,会根据请求中携带的hostname将不同的请求发送给对应的虚拟主机进行处理,这样就实现了基于多host名称方式虚拟主机的配置,具体配置如下:编辑vserver1.confvserver2.conf,将server模块下server_name参数改为对应的hostname即可

代码语言:txt
复制
server {
    listen       80;
    server_name  1.hellboycc.cn;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
	root   /opt/app/code;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

检查nginx语法配置并重启nginx服务

代码语言:txt
复制
[root@node1 conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
[root@node1 conf.d]# ps -aux|grep nginx
root     25919  0.0  0.4  47256  2088 ?        Ss   08:34   0:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx    26227  0.0  0.4  47260  2060 ?        S    11:15   0:00 nginx: worker process
root     26231  0.0  0.1 112712   972 pts/0    S+   11:15   0:00 grep --color=auto nginx
[root@node1 conf.d]#

修改本机host文件进行测试,我本机是Mac,直接修改/etc/hosts文件,如果服务器搭建了DNS服务,可以进行域名解析,然后测试是否配置生效

代码语言:txt
复制
~  cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

172.20.10.2     hadoop000
192.168.30.10 1.hellboycc.cn
192.168.30.10 2.hellboycc.cn

通过浏览器输入http://1.hellboycc.cn/server.html和http://2.hellboycc.cn/server.html进行访问,一个页面输出Hello,vserver1 一个页面输出Hello,vserver2,基于多host名称的配置方式成功。

五、总结

  • 通过上面三种方式,实现了在同一个nginx主机下的虚拟主机应用配置。
  • 在实际部署环境中,基于host名称的部署方式应用较多,要重点掌握。
  • 通过修改本地host文件,对基于host名称的部署方式进行对应测试验证。

技术创作101训练营

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、业务需求
  • 二、部署方式
  • 三、什么是虚拟主机
  • 四、虚拟主机常见配置场景
  • 五、总结
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档