apache绑定多域名

例如:服务器公网IP:99.110.110.123,需要将abc.com绑定到/var/www/html/dir1,将abc.net绑定到/var/www/html/dir2。

打开httpd.conf:
1、将#ServerName www.example.com:80修改成ServerName 99.110.110.123:80,注意别忘记了将句首的#去掉;
2、将#NameVirtualHost *:80修改成NameVirtualHost 99.110.110.123:80,注意别忘记了将句首的#去掉;
3、在文件最后面有虚拟主机格式:

#  
#   ServerAdmin webmaster@dummy-host.example.com  
#   DocumentRoot /www/docs/dummy-host.example.com  
#   ServerName dummy-host.example.com  
#   ErrorLog logs/dummy-host.example.com-error_log  
#   CustomLog logs/dummy-host.example.com-access_log common  
#

在其后添加:

配置代码如下:

可以使用IP(99.110.100.123)访问/var/www/html/dir目录


    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/html/dir
    ServerName 99.110.110.123  
    ErrorLog logs/dummy-host.example.com-error_log  
    CustomLog logs/dummy-host.example.com-access_log common  

使用abc.com与www.abc.com访问/var/www/html/dir1目录

  
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/html/dir1 
    ServerName abc.com  
    ServerAlias www.abc.com  
    ErrorLog logs/dummy-host.example.com-error_log  
    CustomLog logs/dummy-host.example.com-access_log common  

使用abc.net与www.abc.net访问/var/www/html/dir2目录

  
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/html/dir2
    ServerName abc.net
    ServerAlias www.abc.net
    ErrorLog logs/dummy-host.example.com-error_log  
    CustomLog logs/dummy-host.example.com-access_log common  

附:如果想开其他端口,请按照如下操作(以8080端口为例):
1、打开http.conf,查找Listen 80,在下面一行添加Listen 8080;

2、在文件末尾添加:

代码如下:

实现www.abc.com:8080绑定到/var/www/html/dir3目录。

  
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/html/dir3 
    ServerName abc.com
    ServerAlias www.abc.com
    ErrorLog logs/dummy-host.example.com-error_log  
    CustomLog logs/dummy-host.example.com-access_log common  

本文地址 https://laoona.com/post/a152ec43.html