基于域名的虚拟主机(基于域名的虚拟主机)

admin 152 2022-11-14

阿里云服务器优惠多,折扣错,惊喜多,请咨询:www.wqiis.com

本文目录一览:

apache 基于域名虚拟主机配置,本机上访问 www.test.com指向 127.0.0.1/test

是修改这两个文件没错,我测试成功,应该你是哪里配置错了,apache文件修改后要重启。

一、hosts文件最下面换行添加:127.0.0.1 ;

注:

1、win 下 hosts文件路径:C:\WINDOWS\system32\drivers\etc\hosts

2、绑定hosts,是为了访问 时,指向的IP是你自己的电脑;hosts文件绑定后,你可以用cmd ping命令,ping ,测试ping到的IP是不是你自己设置IP(127.0.0.1)

二、apache配置文件修改

1、打开apache配置文件httpd.conf,apache绑定网站路径和域名,保存;

2、重启apache;注:修改apache配置文件后,必须重启apache;

3、附apache配置文件默认路径:C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

介绍几种方式用来在web服务器上创建虚拟主机?

一、定义

所谓虚拟主机是指在一台服务器里运行几个网站,提供WEB、FTP、Mail等服务。

二、虚拟主机的实现方法有三种:

基于IP的方法,基于主机名的方法和基于端口的法官法。

①基于IP的方法:

在服务器里绑定多个IP,然后配置WEB服务器,把多个网站绑定在不同的IP上。访问不同的IP,就看到不同的网站。

②基于端口的方法:

一个IP地址,通过不同的端口实在不同网站的访问。

③基于主机名的方法:

设置多个域名的A记录,使它们解析到同一个IP地址上,即同一个服务器上。然后,在服务器上配置WEB服务端,添加多个网站,为每个网站设定一个主机名。因为HTTP协议访问请求里包含有主机名信息,当WEB服务器收到访问请求时,就可以根据不同的主机名来访问不同的网站。

跟我学Nginx,server 配置块介绍

本文将介绍 http 配置块下面的 server 配置块。

在 nginx 中,一个 server 配置块代表了一个虚拟机主机

虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成多台 “虚拟” 的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的 Internet 服务器功能(WWW、FTP、Email 等等),同一台主机上的虚拟主机之间是完全独立的。

从网站访问者来看,每一台虚拟主机和一台独立主机完全一样。

利用虚拟主机,不用为每个要运行的网站提供一台单独的 Nginx 服务器或单独运行一组 Nginx 进程。虚拟主机提供了在同一台服务器、同一组 Nginx 进程上运行多个网站的功能。

在 Nginx 配置文件(nginx.conf)中,一个最简化的虚拟主机配置如下:

上面配置中,server 配置块配置了一个监听 8080 端口的虚拟主机。其中其他参数如下:

Nginx 通过 listen 指令指定的值监听网络请求,可以是 IP 协议的形式,也可以是 UNIX 域套接字。如果不设置 listen 指令,Nginx 在以超级用户运行时则监听 80 端口,以非超级用户运行时则监听 8000 端口。

listen 指令的指令值还针对监听方式提供了丰富的参数,如下所示:

用法示例:

server_name 指令主要用于配置基于名称的虚拟主机,server_name 指令在接到客户请求后的匹配顺序分别为:

(1)准确的 server_name 匹配,例如:

(2)以 * 通配符开始的字符串:

(3)以 * 通配符结束的字符串:

(4)匹配正则表达式:

Nginx 将按照上面1、2、3、4顺序对 server_name 进行匹配,只要有一项匹配 server_name 后就会停止继续匹配。

该指令用来指定日志文件的存放路径、格式(把定义的 log_format 跟在后面)和缓存大小;如果不想启用日志则access_log off ;

location 指令的作用是根据用户请求的 URI 来执行不同的操作,也就是根据用户请求的网站 URL 匹配 location 配置块,匹配成功就执行 location 配置块中定义的相关操作。示例用法:

关于更多 server 配置块的介绍将在后续章节娓娓道来。

对了,跟 Aapche 一样,Nginx 也可以配置多种类型的虚拟主机:

(1)基于IP的虚拟主机

(2)基于域名的虚拟主机

(3)基于端口的虚拟主机

关于怎样配置基于IP、域名和端口的虚拟主机后续章节将进行介绍。

基于ip和基于域名的虚拟主机的区别

基于IP的虚拟主机,如果是不同IP可以,同一IP要使用不同的端口。

基于域名的虚拟主机,可以使用同一端口。

简单点说,就是要让服务器能够辨认不同的虚拟主机。

国内主流的内网穿透工具有哪些?

国内主流的内网穿透工具有电钻或者是振动钻,这些穿透工具的功率比较强大

如何配置 Apache 的虚拟主机

1、基于ip地址的虚拟主机

复制代码代码如下:

Listen 80

VirtualHost 172.20.30.40

DocumentRoot /home/httpd/html1

ServerName

ErrorLog /usr/local/apache/logs/error1_log

CustomLog /usr/local/apache/logs/access1_log combined

/VirtualHost

VirtualHost 172.20.30.50

DocumentRoot /home/httpd/html2

ServerName

ErrorLog /usr/local/apache/logs/error2_log

CustomLog /usr/local/apache/logs/access2_log combined

/VirtualHost

2、基于IP 和多端口的虚拟主机配置

复制代码代码如下:

Listen 172.20.30.40:80

Listen 172.20.30.40:8080

Listen 172.20.30.50:80

Listen 172.20.30.50:8080

VirtualHost 172.20.30.40:80

DocumentRoot /www/example1-80

ServerName

/VirtualHost

VirtualHost 172.20.30.40:8080

DocumentRoot /www/example1-8080

ServerName

/VirtualHost

VirtualHost 172.20.30.50:80

DocumentRoot /www/example2-80

ServerName

/VirtualHost

VirtualHost 172.20.30.50:8080

DocumentRoot /www/example2-8080

ServerName

/VirtualHost

3、单个IP 地址的服务器上基于域名的虚拟主机配置

复制代码代码如下:

# Ensure that Apache listens on port 80

Listen 80

# Listen for virtual host requests on all IP addresses

NameVirtualHost *:80

VirtualHost *:80

DocumentRoot /www/example1

ServerName

ServerAlias example1.com. *.example1.com

# Other directives here

/VirtualHost

VirtualHost *:80

DocumentRoot /www/example2

ServerName

# Other directives here

/VirtualHost

如果您感觉上面的文章还不够详细可以看下下面的文章:

实验目标:在apache实现基于域名的虚拟主机

实验用的XAMPP版本为1.7.7,内含apache版本为2.2.21

实验前准备:

1. 为了测试不同的域名,在Windows/System32/drivers/etc/下觅得hosts文件,在其中添加实验用的域名若干,如 -

复制代码代码如下:

127.0.0.1 test1.net

127.0.0.1 test2.net

如此,则在浏览器中输入该俩域名时,Windows将其解析为127.0.0.1本地地址。即,在浏览器中访问localhost, test1.net, test2.net均可访问XAMPP的欢迎页。

2. 在apache目录下建立目录,以放置您不同的网站。为保护XAMPP原有的htdocs中的欢迎页内容,实验另外建立了与htdocs平级的htdocs1目录,在其下建立了test1.net, test2.net两个子目录用以放置实验用的网站。如下 -

apache/htdocs1/test1.net - 放置test1.net网站内容

apache/htdocs1/test2.net - 放置test2.net网站内容

在这两个目录中各新建hello world一网页 index.html,内容 -

复制代码代码如下:

HTML

HEAD/HEAD

BODY

H1hello~, 这是[对应的网站名,用以区别].net/H1/BODY

/HTML

实验步骤:

1. 找到apache/conf/httpd.conf, 将其中的

ServerAdmin

ServerName

DocumentRoot

注释掉。

2. 在httpd.conf中,找到行

Include "conf/extra/httpd-vhosts.conf"

如被注释则解注。该文件记载了虚拟主机的参数。[以前虚拟主机参数是直接填写在httpd.conf中的,为了更好地组织文件,将其分离出去,类似于某些编程语言一样。因此httpd.conf中include它,即相当于把它的内容填在了httpd.conf中。]

3. 这个httpd-vhosts.conf文件格式基本如下 -

复制代码代码如下:

#blah-blah

NameVirtualHost *:80

#blah-blah

#blah-blah

VirtualHost *:80

ServerAdmin XXXXXXXX

DocumentRoot "XXXXXXXX"

ServerName XXXXXXX

ServerAlias XXXXXX

ErrorLog "logs/XXXXXX-error.log"

CustomLog "logs/XXXXXXX-error.log" combined

/VirtualHost

需要修改的,就是VirtualHost中的参数了。这个可以参见apache官方文档。根据实验域名,可以增加两个VirtualHost:

复制代码代码如下:

VirtualHost *:80

ServerAdmin adm@test1.net

DocumentRoot "C:/xampp/htdocs1/test1.net"

ServerName test1.net

ServerAlias

ErrorLog "logs/test1-error.log"

CustomLog "logs/test1-access.log" combined

Directory "C:/xampp/htdocs1/test1.net"

order allow,deny

allow from all

/Directory

/VirtualHost

VirtualHost *:80

ServerAdmin adm@test2.net

DocumentRoot "C:/xampp/htdocs1/test2.net"

ServerName test2.net

ServerAlias

ErrorLog "logs/test1-error.log"

CustomLog "logs/test1-access.log" combined

Directory "C:/xampp/htdocs1/test2.net"

order allow,deny

allow from all

/Directory

/VirtualHost

注意,如果不在各VirtualHost中定义Directory的可访问性,你将遇到的是Access Forbidden!就连原来的localhost也是。

4. 由于之前注释掉了httpd.conf中的ServerName, DocumentRoot等,为了仍然能以localhost访问原XAMPP欢迎页,就在增加一个VirtualHost,如下 -

复制代码代码如下:

VirtualHost *:80

ServerAdmin adm@localhost

DocumentRoot "C:/xampp/htdocs"

ServerName localhost

ErrorLog "logs/localhost-error.log"

CustomLog "logs/localhost-access.log" combined

Directory "C:/xampp/htdocs"

order allow,deny

allow from all

/Directory

/VirtualHost

为了避免出错,把它放置在第一个Virtualhost位置。

至此,apache基于域名的虚拟主机配置完成。可以通过访问XAMPP欢迎页,通过和访问各自的主页。

#

# Virtual Hosts

#

# If you want to maintain multiple domains/hostnames on your

# machine you can setup VirtualHost containers for them. Most configurations

# use only name-based virtual hosts so the server doesn't need to worry about

# IP addresses. This is indicated by the asterisks in the directives below.

#

# Please see the documentation at

# URL:;

# for further details before you try to setup virtual hosts.

#

# You may use the command line option '-S' to verify your virtual host

# configuration.

#

# Use name-based virtual hosting.

#

NameVirtualHost *:80

#

# VirtualHost example:

# Almost any Apache directive may go into a VirtualHost container.

# The first VirtualHost section is used for all requests that do not

# match a ServerName or ServerAlias in any VirtualHost block.

#

VirtualHost *:80

ServerAdmin kongdaoxian@gmail.com

DocumentRoot "E:/skydao/apache2/htdocs"

ServerName localhost

ServerAlias

ErrorLog "logs/localhost-error.log"

CustomLog "logs/localhost-access.log" combined

Directory "E:/skydao/apache2/htdocs"

order allow,deny

allow from all

/Directory

/VirtualHost

VirtualHost *:80

ServerAdmin kongdaoxian@gmail.com

DocumentRoot "E:/skydao/apache2/htdocs/project1"

ServerName project1.com

ServerAlias

ErrorLog "logs/project1-error.log"

CustomLog "logs/project1-access.log" combined

Directory "E:/skydao/apache2/htdocs/project1"

order allow,deny

allow from all

/Directory

/VirtualHost

VirtualHost *:80

ServerAdmin kongdaoxian@gmail.com

DocumentRoot "E:/skydao/apache2/htdocs/zendTest/public"

ServerName zendTest.com

ServerAlias

DirectoryIndex index.php

Directory /

Options FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

/Directory

/VirtualHost

VirtualHost *:80

ServerAdmin kongdaoxian@gmail.com

DocumentRoot "E:/skydao/apache2/htdocs/testRewrite"

ServerName testRewrite.com

ServerAlias

# DirectoryIndex index.php

Directory /

Options FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

/Directory

/VirtualHost

VirtualHost *:80

ServerAdmin kongdaoxian@gmail.com

DocumentRoot "E:/skydao/apache2/htdocs/test"

ServerName test.com

ServerAlias

ErrorLog "logs/zendTest-error.log"

CustomLog "logs/zendTest-access.log" combined

Directory "E:/skydao/apache2/htdocs/test"

order allow,deny

allow from all

/Directory

/VirtualHost

上一篇:web服务器虚拟主机(虚拟主机 服务器)
下一篇:华为云会议在线(华为云会议系统)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~