【问题标题】:httpd local virtualhost examplehttpd 本地虚拟主机示例
【发布时间】:2017-12-09 15:58:57
【问题描述】:

-- 原帖---

我想设置一个开发环境来使用一些 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 中执行相同操作 - localhost 按预期工作,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 中(如果不是 localhost)?任何暗示我做错了什么?如何在不修改 dns 设置的情况下让 sub.example1.com 工作?

问候, 帕维尔

【问题讨论】:

    标签: apache virtualhost httpd.conf


    【解决方案1】:

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

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

    /etc/hosts 不支持通配符(*.example2.com),需要设置一个本地 dns 代理服务器,例如 dnsmasq。否则,出于开发目的,您必须在 /etc/hosts 中一一列出(然后维护 :/ )子域以用于本地开发目的。

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

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

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

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

    问候,帕维尔

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • Rewrite 会将您重定向,例如从域 1 到域 2,当您想对用户隐藏某些内容或缩短链接等时使用它。在您的情况下,通过输入 sub.example2 快速获取将请求重定向到 example2 的命令。
      • 您好,感谢您的提议。我尝试注释掉 RewriteEngine On 和 RewriteRule,但它并没有导致预期的结果... # curl example2.com Hello from /var/www/example2/index.html # curl sub.example2.com curl: (6 ) 无法解析主机:sub.example2.com
      【解决方案3】:

      如果您遇到问题,请告诉我。此 dns 将能够与您的常规浏览一起使用,因为对于所有无法解析的名称,它会检查 googles 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
      

      【讨论】:

        猜你喜欢
        • 2013-04-26
        • 2014-04-25
        • 2018-06-26
        • 2018-04-29
        • 2014-08-17
        • 2011-05-13
        • 1970-01-01
        • 2013-01-27
        • 2012-03-29
        相关资源
        最近更新 更多