文章目录

  • Nginx支持的虚拟主机有三种
  • 配置环境
    • 配置DNS域名
    • 安装环境
    • 安装nginx
    • 制作管理脚本
  • 基于域名
  • 基于端口
  • 基于IP
  • 基于用IP地址访问域名

Nginx支持的虚拟主机有三种

●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
每一种虚拟主机均可通过“server{}" 配置段实现各自的功能

配置环境

配置DNS域名

root@localhost ~]# yum -y install bind
[root@localhost ~]# vim /etc/named.conf options {listen-on port 53 { any; };listen-on-v6 port 53 { ::1; };directory       "/var/named";dump-file       "/var/named/data/cache_dump.db";statistics-file "/var/named/data/named_stats.txt";memstatistics-file "/var/named/data/named_mem_stats.txt";recursing-file  "/var/named/data/named.recursing";secroots-file   "/var/named/data/named.secroots";allow-query     { any; };[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "benet.com" IN {type master;file "benet.com.zone";allow-update { none; };
};
zone "kgc.com" IN {type master;file "kgc.com.zone";allow-update { none; };
};[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost benet.com.zone
[root@localhost named]# vim benet.com.zone
www  IN A      192.168.136.10
[root@localhost named]# setenforce 0
[root@localhost named]# iptables -F
[root@localhost named]# systemctl stop firewalld
[root@localhost named]# systemctl start named

安装环境

[root@localhost named]# yum -y install gcc gcc-c++ pcre-devel zlib-devel

安装nginx

[root@localhost named]# tar zxvf nginx-1.12.2.tar.gz -C /opt/
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost ~]# useradd -M -s /sbin/nologin nginx   编译需要指定相关用户 -M 创建家目录
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
[root@localhost nginx-1.12.2]# 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 nginx]# cd /etc/init.d/   重建服务
[root@localhost init.d]# vim nginx    创建管理脚本
#!/bin/bash
# chkconfig: - 99 20
#description: nginx service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" instart)$PROG;;stop)kill -s QUIT $(cat $PIDF);;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx   便于servers管理
[root@promote init.d]# service nginx start
[root@localhost init.d]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25954/nginx: master
tcp        0      0 192.168.136.10:22       192.168.136.2:59246     ESTABLISHED 8049/sshd: root@pts
tcp        0      0 192.168.136.10:48214    59.111.0.251:80         TIME_WAIT   -

基于域名

创建虚拟主机

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf在末尾添加
server {server_name www.kgc.com;location / {root /var/www/kgc;index index.html index.php;}}server {server_name www.benet.com;location / {root /var/www/benet;index index.html index.php;}}

开启服务

[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 ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# mkdir -p /var/www/html/benet
[root@localhost html]# echo "this is kgc web" > kgc/index.html
[root@localhost html]# echo "this is benet web" > benet/index.html
[root@localhost conf]#  service nginx restart

基于端口

[root@localhost html]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.confvim /etc/nginx.confserver {listen 192.168.60.60:80;server_name www.kgc.com;location / {root /var/www/html/kgc;index  index.html index.htm; }
}server {listen 192.168.60.60:8080;server_name www.kgc.com;location / {root /var/www/html/benet8080;index  index.html index.htm; }
}

写入网页

[root@localhost conf]# cd /var/www//html/
[root@localhost html]# mkdir benet8080
[root@localhost html]# echo "this is benet8080 web" > benet8080/index.html
[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

基于IP

添加一个网卡 左边是IP地址查看是否可以通信

配置DNS区域配置文件

[root@localhost named]# vim kgc.com.zone
www  IN A     192.168.136.136
[root@localhost named]# systemctl restart named

在win10中查看一下解析地址

修改一下kgc的ip地址

[root@localhost named]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
vim /etc/nginx.confserver {listen 192.168.136.136:80;server_name www.kgc.com;location / {root /var/www/html/kgc;index  index.html index.htm; }
}server {listen 192.168.136.10:80;server_name www.benet.com;location / {root /var/www/html/benet;index  index.html index.htm;}
}

验证一下是否成功并开启

[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

基于用IP地址访问域名

[root@localhost conf]# vim nginx.conf

开启服务

[root@localhost conf]# service nginx restart

开一台win7的服务器

解析一下地址

网页查看一下

win10的网页查看

结论:当我们配置没有问题时候可以找别的方法去解决,比如换一台客户终端测试(可能是客户端的问题)

nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置)相关推荐

  1. IP地址荷官——DHCP(动态主机配置协议)简介

    文章目录 IP地址荷官--DHCP(动态主机配置协议) 前言 一.什么是DHCP? 二.DHCP的优势 三.DHCP的配置原理 DHCP分配的三种模式 DHCP地址池 DHCP配置特殊应用场景 四.D ...

  2. 浅谈IP地址二三事 IP地址代表什么,网络号主机号是什么?

    IP地址 最近准备教资的面试,自然准备的信息技术.能看到好多简单,但是容易忘记的知识点,就算是我这样准备毕业,就要去工作的计算机专业的学生来说.也总是能忘.故记录 IP:internet protoc ...

  3. 计算机IP地址,子网掩码,网络地址,主机地址

    参考:百度百科.https://blog.csdn.net/qq_38410730/article/details/80980749 IP地址(Internet Protocol Address)是指 ...

  4. 国际域名及其他可解析域名免费捆绑动态IP地址

    国际域名及其他可解析域名免费捆绑动态IP地址   [ 日期:2004-09-25 ]   "让网络更免费更自由! 让所有国内的动态域名服务商破产!"本站推荐可解析三级域名:.COM ...

  5. 实现一个域名对应多个IP地址和DNS优缺点详解!

    实现一个域名对应多个IP地址和DNS优缺点详解! 1.DNS定义: DNS(Domain Name System)是因特网的一项服务,它作为域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访 ...

  6. 负载均衡实现,一个域名对应多个IP地址

    http://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.html 使用负载均衡实现,传统和常规做法,其他方式需要特殊处理. ...

  7. 计算机名和DNS域名的关系,域名、DNS、IP地址的对应关系

    什么是域名?什么是IP地址? 域名(英语:Domain Name),简称域名.网域,是由一串用点分隔的名字组成的上某一台计算机或计算机组的名称,用于在数据传输时标识计算机的电子方位(有时也指地理位置) ...

  8. 负载均衡实现,一个域名对应多个IP地址【转】

    来自:https://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.html 使用负载均衡实现,传统和常规做法,其他方式需要特 ...

  9. 引子 我想大家应该都很熟悉DNS了,这回在DNS前面加了一个D又变成了什么呢?这个D就是Dynamic(动态),也就是说,按照传统,一个域名所对应的IP地址应该是定死的,而使用了DDNS后,域名所对应

    引子 我想大家应该都很熟悉DNS了,这回在DNS前面加了一个D又变成了什么呢?这个D就是Dynamic(动态),也就是说,按照传统,一个域名所对应的IP地址应该是定死的,而使用了DDNS后,域名所对应 ...

  10. centos 7 无法查看IP地址,并且在登录虚拟机时就显示connected failed

    本来想通过xftp连接虚拟机传送一些文件.但是怎么都连不上,然后虚拟机用ifconfig也查看不到IP地址(就是ens33中没有显示IP地址).于是参考文献使用虚拟机ping 主机iP地址发现显示:c ...

最新文章

  1. 各种注意力机制PyTorch实现
  2. Redis - Spring Data Redis 操作 Jedis 、Lettuce 、 Redisson
  3. python函数的返回值是返回引用吗_python-函数(上):函数返回值、函数调用、前向引用...
  4. Hive中的一种假NULL
  5. 操作系统以什么方式组织用户使用计算机,操作系统习题
  6. crntos7启动php命令_CentOS7设置php-fpm开机自启动
  7. 从工具到平台|默安科技研发安全一体化管理平台正式发布
  8. 【目标检测】NMS和soft-NMS详解及代码实现
  9. 吴恩达机器学习ex6:支持向量机
  10. (一)Mina源代码解析之总体架构
  11. 菜鸟系列之C/C++经典试题(七)
  12. 浅谈易用性测试及GUI常见的测试要求
  13. java宠物商店_Java实现宠物商店管理系统
  14. mac+python3+selenium做pc的界面自动化测试
  15. noip2011 观光公交 (贪心)
  16. MATLAB笔记:打开数据文件的三种方法+读取数据文件的两种方法+保存数据文件的两种方法
  17. 通过Burp以及自定义的Sqlmap Tamper进行二次SQL注入
  18. jqGrid与Struts2的结合应用(一) —— 显示基本Grid表格
  19. 【社媒营销】四大跨境营销渠道分析
  20. checkpatch海思SDK代码遇见的常见错误《二》

热门文章

  1. 新一代音视频技术架构驱动未来多媒体创新
  2. 如何使用 一行代码 搞定一组数据的(极值、平均值、中位数、四分位数、数量统计和标准差)
  3. 如何写一手好 SQL 【频繁出现慢SQL告警的优化方案】
  4. 使用cloudera manager安装Hive服务【详细步骤】
  5. Hbase的读写速度,写比读快
  6. Spring的Java配置
  7. leetcode 494. Target Sum | 494. 目标和(动态规划)
  8. 高等数学:e的-t平方次方求积分
  9. 【重复提交表单】表单重复提交的三种情况,解决办法
  10. PAT1043 输出PATest (20 分)