您的位置:首页 > 科技 > 能源 > Nginx--虚拟机配置

Nginx--虚拟机配置

2024/10/6 22:21:21 来源:https://blog.csdn.net/lerp020321/article/details/141282676  浏览:    关键词:Nginx--虚拟机配置

前言:本博客仅作记录学习使用,部分图片出自网络,如有侵犯您的权益,请联系删除

1、什么是虚拟主机

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置:

  • 基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)
  • 基于ip的虚拟主机 (一块主机绑定多个ip地址)
  • 基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

2、基于域名的虚拟主机

(1)配置通过域名区分的虚拟机

 [root@centos ~]# cat /etc/nginx/nginx.confworker_processes auto;worker_rlimit_nofile 102400;events {worker_connections  1024;}​http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;​server {listen       80;server_name  web.testpm.com;    # 配置域名为web.testpm.com​location / {root   /var/www/nginx;      # 根目录index  index.html index.htm;    # 文件}}​server {listen       80;server_name  web.1000phone.com; # 配置域名为web.1000phone.com​location / {root   /1000phone/html;     # 网站展示文件根目录index  index.html index.htm;    # 展示文件}}}

(2)为配置的两个域名创建目录与index文件

 [root@centos html]# cat /var/www/nginx/index.htmlThis is the index.html Home of domain web.testpm.com!\nweb.testpm.com![root@centos html]# cat /1000phone/html/index.htmlThis is the index.html Home of domain web.1000phone.com!\nweb.1000phone.com!

(3)重新加载配置文件

 # 如果编译安装的执行[root@nginx]# /usr/local/nginx/sbin/nginx -s reload# 如果 yum 安装的执行[root@nginx]# nginx -s reload

(4)客户端配置路由映射

在 C:\Windows\System32\drivers\etc\hosts 文件中添加两行 | (linux:/etc/hosts)

 # 主机IP 配置的域名10.0.0.2 web.testpm.com10.0.0.2 web.1000phone.com

(5)测试访问

浏览器分别输入http://web.testpm.com与http://web.1000phone.com

3、基于IP的虚拟主机

(1)为网卡新增一个IP地址

 [root@centos ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.0.2  netmask 255.255.255.0  broadcast 10.0.0.255inet6 fe80::20c:29ff:fe39:6cf4  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)RX packets 99486  bytes 71872175 (68.5 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 74475  bytes 5417252 (5.1 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@centos ~]# ifconfig ens33:1 10.0.0.3/24[root@centos ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.0.2  netmask 255.255.255.0  broadcast 10.0.0.255inet6 fe80::20c:29ff:fe39:6cf4  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)RX packets 99522  bytes 71875437 (68.5 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 74505  bytes 5422160 (5.1 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0​ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 10.0.0.3  netmask 255.255.255.0  broadcast 10.0.0.255ether 00:0c:29:39:6c:f4  txqueuelen 1000  (Ethernet)

(2)配置通过IP区分的虚拟机

 [root@centos ~]# cat /etc/nginx/nginx.conf...server {listen       10.0.0.2:80;       # 修改网站监听IP为10.0.0.2:80server_name  web.testpm.com;​location / {root   /var/www/nginx;index  index.html index.htm;}}​server {listen       10.0.0.3:80;       # 修改此处server_name  web.1000phone.com;​location / {root   /1000phone/html;index  index.html index.htm;}}}

(3)重新加载配置文件

nginx -t && nginx -s reload

(4)客户端配置路由映射

 # 主机IP 配置的域名10.0.0.2 web.testpm.com10.0.0.2 web.1000phone.com10.0.0.3 web.1000phone.com

(5)测试访问

4、基于端口的虚拟主机

(1)编辑配置文件并重新加载

 [root@centos ~]# cat /etc/nginx/nginx.conf...server {listen       80;        # 设置80端口server_name  web.testpm.com;​location / {root   /var/www/nginx;index  index.html index.htm;}}​server {listen       8080;      # 设置为8080端口server_name  web.1000phone.com;​location / {root   /1000phone/html;index  index.html index.htm;}}}[root@centos ~]# nginx -s reload

(2)测试访问

致谢

在此,我要对所有为知识共享做出贡献的个人和机构表示最深切的感谢。同时也感谢每一位花时间阅读这篇文章的读者,如果文章中有任何错误,欢迎留言指正。 

学习永无止境,让我们共同进步!!​​​​​​​

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com