linux nginx虚拟主机配置文件,详述Linux系统中Nginx虚拟主机的配置

news/2024/7/24 1:51:50 标签: linux nginx虚拟主机配置文件

Nginx虚拟主机应用

Nginx支持的虚拟主机有三种

基于域名的虚拟主机.

基于IP的虚拟主机

基于端口的虚拟主机

通过"server{}"配置段实现

本篇实验接着上一篇搭建Nginx服务继续搭建,前面Nginx的编译安装不在介绍

基于域名的虚拟主机

[root@localhost nginx-1.12.2]# mkdir -p /var/www/html/accp //递归创建accp网页站点目录

[root@localhost nginx-1.12.2]# mkdir -p /var/www/html/kgc //递归创建kgc网页站点目录

[root@localhost nginx-1.12.2]# cd /var/www/html/ //进入站点目录

[root@localhost html]# ls //查看

accp kgc

[root@localhost html]# echo "this is kgc web" > kgc/index.html //创建站点文件

[root@localhost html]# echo "this is accp web" > accp/index.html //创建站点文件

[root@localhost html]# ls accp/ //查看accp站点目录

index.html

[root@localhost html]# ls kgc/ //查看kgc站点目录

index.html

[root@localhost conf]# vim /etc/named.rfc1912.zones //编辑DNS服务区域配置文件

...//省略部分内容...

zone "kgc.com" IN {

type master;

file "kgc.com.zone";

allow-update { none; };

};

zone "accp.com" IN {

type master;

file "accp.com.zone"; //添加accp网页域名解析

allow-update { none; };

};

...//省略部分内容...

:wq

[root@localhost conf]# cd /var/named/

[root@localhost named]# cp -p named.localhost kgc.com.zone //复制区域数据文件

[root@localhost named]# vim kgc.com.zone //编辑kgc区域数据文件

$TTL 1D

@ IN SOA @ rname.invalid. (

0 ; serial

1D ; refresh

1H ; retry

1W ; expire

3H ) ; minimum

NS @

A 127.0.0.1

www IN A 192.168.144.133

:wq

[root@localhost named]# cp -p kgc.com.zone accp.com.zone

//复制kgc区域数据文件保持权限不变,复制为accp区域数据文件

[root@localhost named]# systemctl restart named //重启DNS服务

[root@localhost init.d]# cd /usr/local/nginx/conf/

[root@localhost conf]# vim nginx.conf //编辑Nginx主配置文件

...//省略部分内容...

server {

listen 80;

server_name www.kgc.com; //设置kgc域名访问条目

charset utf-8; //更改字符串类型

access_log logs/www.kgc.com.access.log; //更改日志文件名称

location / {

root /var/www/html/kgc; //更改站点位置

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name www.accp.com;

charset utf-8;

access_log logs/www.accp.com.access.log;

location / {

root /var/www/html/accp;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

...//省略部分内容...

:wq

[root@localhost conf]# nginx -t //检测语法

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# service nginx restart //重启服务

[root@localhost conf]# systemctl restart named //重启DNS服务

在客户机中测试网页

c13b0b2b8ea2975918d5f682de80ac6b.png

90103ec6fea8d5ac70e711adc102bdf8.png

基于端口的虚拟主机

[root@localhost conf]# vim nginx.conf //编辑Nginx主配置文件

...//省略部分内容...

# server {

# listen 80;

# server_name www.kgc.com;

# charset utf-8;

# access_log logs/www.kgc.com.access.log;

# location / {

# root /var/www/html/kgc; //注释掉此部分内容

# index index.html index.htm;

# }

# error_page 500 502 503 504 /50x.html;

# location = /50x.html {

# root html;

# }

# }

server {

listen 192.168.144.133:80; //端口条目钱添加IP地址

server_name www.accp.com;

charset utf-8;

access_log logs/www.accp.com.access.log;

location / {

root /var/www/html/accp;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server { //复制上面部分的内容

listen 192.168.144.133:8080; //设置相同IP监听不同端口

server_name www.accp.com;

charset utf-8;

access_log logs/www.accp8080.com.access.log; //更改日志文件名称

location / {

root /var/www/html/accp8080; //整改站点文件目录名称

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

...//省略部分内容...

:wq

[root@localhost conf]# cd /var/www/html/ //进入站点目录

[root@localhost html]# mkdir accp8080 //创建目录

[root@localhost html]# echo "this is accp8080 web" > accp8080/index.html //编辑网页内容

[root@localhost html]# cd /usr/local/nginx/conf/

[root@localhost conf]# nginx -t //检测语法

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# service nginx restart //重启服务

[root@localhost conf]# netstat -ntap | grep 80 //查看端口

tcp 0 0 192.168.144.133:8080 0.0.0.0:* LISTEN 11967/nginx: master

tcp 0 0 192.168.144.133:80 0.0.0.0:* LISTEN 11967/nginx:

在客户机中访问测试

5873f9b5a6144ed888dd75970294ea86.png

a940e2e19439dc049565432cda199520.png

基于不同IP的虚拟主机

首先给Linux虚拟机添加网卡

2c767bd92308110d95483ec2abdf081f.png

[root@localhost conf]# ifconfig //查看新添加的网卡信息,并记录IP地址

ens33: flags=4163 mtu 1500

inet 192.168.144.133 netmask 255.255.255.0 broadcast 192.168.144.255

inet6 fe80::a85a:c203:e2e:3f3c prefixlen 64 scopeid 0x20

ether 00:0c:29:5b:d3:a0 txqueuelen 1000 (Ethernet)

RX packets 67362 bytes 72060261 (68.7 MiB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 16836 bytes 1825469 (1.7 MiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens36: flags=4163 mtu 1500

inet 192.168.144.145 netmask 255.255.255.0 broadcast 192.168.144.255

inet6 fe80::deb1:3cec:3e26:5ec2 prefixlen 64 scopeid 0x20

ether 00:0c:29:5b:d3:aa txqueuelen 1000 (Ethernet)

RX packets 6 bytes 926 (926.0 B)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 27 bytes 4513 (4.4 KiB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

[root@localhost conf]# vim /var/named/kgc.com.zon //更改DNS区域数据文件中kgc网页解析的IP地址

$TTL 1D

@ IN SOA @ rname.invalid. (

0 ; serial

1D ; refresh

1H ; retry

1W ; expire

3H ) ; minimum

NS @

A 127.0.0.1

www IN A 192.168.144.145 //更改为新添加网卡的IP地址

:wq

[root@localhost conf]# vim nginx.conf //编辑主配置文件

...//省略部分内容...

server {

listen 192.168.144.145:80; //去掉上面配置基于端口时全面添加的注释,并添加监听的IP地址

server_name www.kgc.com;

charset utf-8;

access_log logs/www.kgc.com.access.log;

location / {

root /var/www/html/kgc;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 192.168.144.133:80;

server_name www.accp.com;

charset utf-8;

access_log logs/www.accp.com.access.log; //保持不变

location / {

root /var/www/html/accp;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

# server {

# listen 192.168.144.133:8080;

# server_name www.accp.com;

# charset utf-8;

# access_log logs/www.accp8080.com.access.log; //注释掉此部分内容

# location / {

# root /var/www/html/accp8080;

# index index.html index.htm;

# }

# error_page 500 502 503 504 /50x.html;

# location = /50x.html {

# root html;

# }

# }

...//省略部分内容...

:wq

[root@localhost conf]# nginx -t //检测语法

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost conf]# service nginx restart //重启服务

[root@localhost conf]# systemctl restart named //重启DNS服务

在客户机中访问测试(此处建议新开启一台客户机做访问测试)

930eb589bac541d7d1131d6e23e68234.png


http://www.niftyadmin.cn/n/1265509.html

相关文章

手写一个配置参数缓存器

本人擅长用Django开发应用,但是由于部分配置文件频繁更改,导致必须重启服务才能生效,特别是服务上线之后,频繁重启服务是万万不可能的,那么有没有好的解决方案? 经过我苦思冥想,终于想到了一套…

linux系统按内容查找,Linux系统中查找文本的技巧你都掌握哪些?

今天小编要跟大家分享的文章是关于Linux系统中查找文本的技巧你都掌握哪些?之前小编也为大家分享过很多Linux相关的命令,但是对文件内容搜索的命令似乎还没有涉及,今天小编就为大家带来了这篇文章,让我们一起来看一看文件查找grep命令的相关…

linux奶瓶U盘使用方法,CDlinux如何制作U盘启动及Beini(奶瓶)制作U盘启动的方法...

最近在研究无线网络安全。查了一下,在BT5之外还有一个很强大而便捷且专一的无线安全审计工具Beini,不过Beini原版系统比较简洁点,集成的功能单一。而国内CDlinux思维论坛针对Beini做了二次开发,添加很多工具,而且界面更…

Linux IO重定向及管道

一,标准输入输出: 程序 : 指令数据 程序 :IO 可用于输入的设备:文件,键盘设备,文件系统上的常规文件,网卡等; 可用于输出的设备:文件,显示器&a…

linux usb3.0无法识别u盘启动,Deepin 20系统能识别USB3.0:如果不能用请重启系统或重插几次...

在Deepin 15.11系统中能正常识别到USB3.0,但到了Deepin 20系统中却不能识别。如果出现这种情况,请多重新插几次U盘,或者重启一下系统试试。根据用户的反馈来看,Deepin 20是完全可以识别到USB3.0的。日志注:以下是插入U…

Linux 用户和组管理

介绍3A安全机制: Authentication:认证 Authorization:授权 Accouting|Audition :审计 linux 运行的程序是以进程发起者的身份运行的,进程所能访问资源的权限取决于进程的运行者的身份 用户 user: 令牌 token&#xf…

python复制远程linux文件,python paramiko+scpclient SCP远程文件拷贝应用

image.pnggit链接:https://github.com/monkeyish-smart/python-paramiko-scpclient.git1、实现scp协议的远程文件拷贝,注意:不是FTP 也不是SFTP。2、为什么要做scp, 因为scp使用ssh 对资源有限的嵌入式linux 比较常用3、scp文件拷贝使用param…

Linux 权限管理

权限管理 ls -l aa -rwxrwxrwx: -表示文件 左三位:定义user(owner)的权限 中三位:定义group的权限 右三位:定义other的权限 进程安全上下文: 进程对文件的访问权限应用模型: 进程的属主与文件的属主是否相同&…