httpd的本地虚拟主机的例子

问题描述:

- 原帖---httpd的本地虚拟主机的例子

我想建立一个开发ENV一些apache的功能发挥。我在Fedora上运行httpd。

我加入到本地主机重定向

# cat /etc/hosts 
127.0.0.1 example1.com 
127.0.0.1 example2.com 

# cmkdir /var/www/example1;  echo "Hello from /var/www/example1/index.html" > /var/www/example1/index.html 
# cmkdir /var/www/example2;  echo "Hello from /var/www/example2/index.html" > /var/www/example2/index.html 
# cmkdir /var/www/example2/sub ; echo "Hello from /var/www/example2/sub/index.html" > /var/www/example2/sub/index.html 

# cvi /etc/httpd/conf/httpd.conf 
<VirtualHost _default_:80> 
    DocumentRoot "/var//www/html" 
</VirtualHost> 

<VirtualHost 127.0.0.1:80> 
    DocumentRoot "/var/www/example1" 
    ServerName example1.com 

</VirtualHost> 

<VirtualHost 127.0.0.1:80> 
    DocumentRoot "/var/www/example2" 
    ServerName example2.com 

</VirtualHost> 

<VirtualHost 127.0.0.1:80> 
    DocumentRoot "/var/www/example2/sub" 
    ServerName sub.example2.com 
    ServerPath "/sub/" 
    RewriteEngine On 
    RewriteRule "^(/sub/.*)" "/var/www/example2$1" 

</VirtualHost> 

# capachectl -t ; apachectl restart 

# curl localhost 
Hello from /var/www/html/index.html 
# curl example1.com 
Hello from /var/www/example1/index.html 
# curl example2.com 
Hello from /var/www/example2/index.html 
# curl sub.example2.com 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<meta http-equiv="Content-Script-Type" content="text/javascript"> 
<script type="text/javascript"> 
... (lots of stuff different from the one i echoed) ... 

如果我在本地的Firefox同样的事情 - 如预期本地主机的工作原理,example1.com正常工作,但sub.example2.com重定向我EXAMPLE2。 COM。

你能帮我弄清楚如何配置本地子域?什么不见​​了?根据https://httpd.apache.org/docs/2.4/vhosts/examples.html我相信我所做的是正确的。

- 编辑/更新---

如果我按照从新手以下的建议,改变只重写规则,未做从上面安装任何其他的变化:

<VirtualHost 127.0.0.1:80> 
     DocumentRoot "/var/www/example2/sub" 
     ServerName sub.example2.com 
     ServerPath "/sub/" 
#  RewriteEngine On 
#  RewriteRule "^(/sub/.*)" "/var/www/example2$1" 

    </VirtualHost> 

我得到:

# curl sub.example2.com 
curl: (6) Could not resolve host: sub.example2.com 

如果我

# cat /etc/hosts | grep sub 
127.0.0.1 example2.com sub.example2.com 

它将按预期

#curl example2.com 
Hello from /var/www/example2/index.html 
# curl sub.example2.com 
Hello from /var/www/example2/sub/index.html 

不过这似乎是奇怪的设置。我不想为每个子域创建/ etc/hosts记录......不应该只能通过httpd VirtualHost设置来处理这种情况,而无需在本地更改DNS设置,甚至更糟糕 - 添加C记录在一个域的DNS(如果不是本地主机)?任何暗示我做错了什么?如何让sub.example1.com在不修改dns设置的情况下工作?

问候, 帕维尔

我相信我找到了答案,我的问题...

**如何运行本地子域?**

/etc/hosts中不支持通配符(*。example2.com),并且需要设置一个像dnsmasq这样的本地dns代理服务器。否则,出于开发目的,您必须在/ etc/hosts中为本地开发目的一一列出(然后保留:/)子域。

如何通过域名的官方DNS记录运行子域?

似乎最懒的方法是有设置DNS设置:

example2.com  A  the-server-IP 
example2.com  MX 0 example2.com  
*.example2.com CNAME example2.com 

期待您的意见,如果有更聪明的做法。 如果您同意这是要走的路 - 请接受答案,以便其他成员知道这是要走的路。

问候,帕维尔

删除重写规则,它用于重定向,所以你有一个循环。

<VirtualHost 127.0.0.1:80> DocumentRoot "/var/www/example2/sub" ServerName sub.example2.com ServerPath "/sub/" 

+0

重写将例如从域1重定向到域2,当您想要隐藏用户或缩短链接等时使用它。在你的情况下,通过输入sub.example2,apace获得命令将请求重定向到example2。 – Newbie

+0

您好, 感谢您的建议。我试着注释掉RewriteEngine On和RewriteRule,但它并没有导致预期的结果... #curl example2.com Hello from /var/www/example2/index.html #curl sub.example2.com curl:(6)无法解析主机:sub.example2.com – ppavlov

让我知道,如果你碰上problems.This DNS将能够与正常浏览的工作,因为所有的名字它无法解析,将与Google的DNS查询,并保存到它的文件。

install bind 
apt-get install bind9 -y 

cd /etc/bind 

vim named.conf.options 

And uncomment forwarders and two rows bellow and instead of 0.0.0.0 enter google's dns IP (8.8.8.8). 

service bind9 restart 

vim named.conf.options 

zone "YOURDOMAIN NAME" { 
     type master; 
     file "db.site.com"; 
     notify yes; 
     }; 

cp db.local /var/cache/bind/db.site.com 
cd /var/cache/bind/ 
vim db.site.com 

$TTL 604800 
@  IN  SOA  admin. admin.itlink.edu. (
           2   ; Serial 
         604800   ; Refresh 
          86400   ; Retry 
         2419200   ; Expire 
         604800)  ; Negative Cache TTL 
; 
     IN NS ns.YOURDOMAINNAMEHERE. 
     IN  A  192.168.1.10 replace this with the IP of your PC that has apache installed 
ns  A  192.168.1.10 replace this with the IP of your PC that has apache installed 
www  A  192.168.1.10 replace this with the IP of your PC that has 



service bind9 restart