windows 通过局域网从另一台机器访问虚拟主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11245242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:38:42  来源:igfitidea点击:

Access virtual host from another machine over LAN

windowsapachewindows-7virtualhostwampserver

提问by jatin3893

  • I am using Windows 7 with Wamp 2.2 server.
  • I have setup 2 virtual hosts: www.project1.comand www.project2.com.
  • I have modified the "hosts", the httpd.conf, and the httpd-vhosts.conffiles, to the changes I mentioned below.
  • 我在 Wamp 2.2 服务器上使用 Windows 7。
  • 我已经设置了 2 个虚拟主机:www.project1.comwww.project2.com.
  • 我已将“主机”、httpd.confhttpd-vhosts.conf文件修改为我在下面提到的更改。

Using my browser, when I type www.project1.comor www.project2.com, I successfully get my web pages opened on the laptop that has the server installed on.

使用我的浏览器,当我输入www.project1.com或 时www.project2.com,我成功地在安装了服务器的笔记本电脑上打开了我的网页。

Changes in the "hosts file": I've appended the followings to the end of the file:-

主机文件”中的更改:我已将以下内容附加到文件末尾:-

127.0.0.1       localhost
127.0.0.1       www.project2.com
127.0.0.1       www.project1.com

Changes in the httpd.conffile:-

httpd.conf文件中的更改:-

Include conf/extra/httpd-vhosts.conf

Changes in httpd-vhostsfile:-

httpd-vhosts文件更改:-

NameVirtualHost *:80

<Directory "D:/websites/">
    AllowOverride All
    Order Deny,Allow
    Allow from all
    </Directory>
<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/wamp/www/"
    ServerName localhost
</VirtualHost>


<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/project1/"
    ServerName www.project1.com
</VirtualHost>


<VirtualHost 127.0.0.1>
    DocumentRoot "D:/websites/project2/"
    ServerName www.project2.com
</VirtualHost>



现在; 由于我可以从浏览器PC_1PC_1(带有服务器的浏览器)打开这些网页,我如何从浏览器访问这些网页PC_2PC_2?(我的意思是任何PC_1PC_1通过 LAN连接的 PC 。)

采纳答案by dmp

In your virtualhost directive, change 127.0.0.1to *:80and as Gabriel mentioned, add an entry to the hosts file in the other machine, adding your domain to be associated with the IP of your server.

在你的虚拟主机的指令,改变127.0.0.1*:80和加布里埃尔提到,添加到其他机器的主机文件中的条目,将域添加到您的服务器的IP关联。

When you put an explicit IP into the directive, apache will only listen on that IP - but the wildcard will tell it bind to all IPs available to it.

当您将显式 IP 放入指令中时,apache 只会侦听该 IP - 但通配符会告诉它绑定到所有可用的 IP。

<VirtualHost *:80>
    DocumentRoot "D:/websites/project1/"
    ServerName www.project1.com
</VirtualHost>

If your server is on 192.168.1.70 for example, then in the other machines on your lan, the hosts entry will look like:

例如,如果您的服务器在 192.168.1.70 上,那么在您局域网上的其他机器上,主机条目将如下所示:

192.168.1.70     www.project1.com

Restart apache and it should work fine.

重新启动 apache,它应该可以正常工作。

As a note, when you are using virtualhosts, apache will use the first definition as a default for when it can't make a match between the domain passed in the HTTP request header and the sites setup in the config, which is why your default page was appearing.

请注意,当您使用虚拟主机时,当无法在 HTTP 请求标头中传递的域和配置中设置的站点之间进行匹配时,apache 将使用第一个定义作为默认值,这就是为什么您的默认设置页面出现。

You told apache to bind to all IPs with the NameVirtualHost *:80directive, but then didn't setup a site for that external IP. Hope that helps!

您告诉 apache 使用NameVirtualHost *:80指令绑定到所有 IP ,但随后没有为该外部 IP 设置站点。希望有帮助!

回答by EdC

In addition to danp's answer, you can access the virtual host without having to change the client machine's etc/hosts file by assigning a port to the virtual host. This is ideal if you want to access the server with a mobile or tablet device:

除了 danp 的回答之外,您还可以通过为虚拟主机分配端口来访问虚拟主机,而无需更改客户端机器的 etc/hosts 文件。如果您想使用移动或平板设备访问服务器,这是理想的选择:

  1. Edit server's httpd.conffile at:

    \wamp\bin\apache\apache2.2.x\conf\httpd.conf
    

    Search for "Listen" (around line 61). You should see the following that allows for Apache to listen for port 80:

    Listen 0.0.0.0:80
    Listen [::0]:80
    

    Add the following lines to add listening for port 81(or any port):

    Listen 0.0.0.0:81
    Listen [::0]:81
    
  2. Edit the httpd-vhosts.conffile at:

    \wamp\bin\apache\apache2.2.x\conf\extra\httpd-vhosts.conf
    

    Change your "Virtual Host"tag to port 81:

    <VirtualHost *:81>
        DocumentRoot "D:/websites/project1/"
        ServerName www.project1.com
    </VirtualHost>
    
  3. Restart Apache server.

  4. On the client machine/tablet/mobile, on the web browser, enter the server's IP address (192.168.0.10, or whatever IP) followed by the port number in the following format:

    http://192.168.0.10:81
    
  1. 编辑服务器的httpd.conf文件:

    \wamp\bin\apache\apache2.2.x\conf\httpd.conf
    

    搜索“ Listen”(第 61 行左右)。您应该会看到以下内容,允许 Apache 侦听端口 80:

    Listen 0.0.0.0:80
    Listen [::0]:80
    

    添加以下行以添加侦听端口81(或任何端口):

    Listen 0.0.0.0:81
    Listen [::0]:81
    
  2. httpd-vhosts.conf在以下位置编辑文件:

    \wamp\bin\apache\apache2.2.x\conf\extra\httpd-vhosts.conf
    

    将您的“虚拟主机”标签更改为 port 81

    <VirtualHost *:81>
        DocumentRoot "D:/websites/project1/"
        ServerName www.project1.com
    </VirtualHost>
    
  3. 重新启动 Apache 服务器。

  4. 在客户端机器/平板电脑/移动设备上,在 Web 浏览器上,输入服务器的 IP 地址(192.168.0.10或任何 IP),然后按以下格式输入端口号:

    http://192.168.0.10:81
    

回答by Jason Yeung

There are two computer in local network.

局域网中有两台电脑。

A computer(192.168.1.70) Setup(D:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf):

一台电脑(192.168.1.70)设置(D:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf):

<VirtualHost *:80>
    DocumentRoot "D:/websites/project1/"
    ServerName www.project1.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "D:/websites/project2/"
    ServerName www.project2.com
</VirtualHost>

B computer Setup(c:/windows/system32/drives/etc/hosts):

B 电脑设置(c:/windows/system32/drives/etc/hosts):

192.168.1.70    www.project1.com
192.168.1.70    www.project2.com

B access A,My project is working.

B 访问 A,我的项目正在运行。

回答by iyrin

A couple of updated points to consider for the selected answer:

所选答案要考虑的几个更新点:

  1. NameVirtualHostis no longer used after Apache version 2.3.11and can be omitted.

    In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address.

  2. Because we are talking about hosting a website over LAN, let's set a requirement* to only accept connections from IP addresses on your local network. For example, on a common Linksys router, the default IP assigned to each device on the network is between 192.168.1.100 to 192.168.1.255. You can allow connection from all devices on the LAN with an IP address 192.168.1.XXX by using Require ip 192.168.1(notice the final octet is left off the IP to allow the entire range).

    This allows you to configure access per project so that one may be available over LAN and another is only available locally.

    # This will allow all LAN connections to www.project1.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project1/"
        <Directory  "D:/websites/project1/">
            Require local
            Require ip 192.168.1
        </Directory>
        ServerName www.project1.com
    </VirtualHost>
    
    # This will allow only the machine hosting the website to access www.project2.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project2/"
        <Directory  "D:/websites/project2/">
            Require local
        </Directory>
        ServerName www.project2.com
    </VirtualHost>
    

    While your site will not be served publicly without the router forwarding traffic on port 80 to your host, I believe this is considered best practice. It is especially necessary if you need to control which projects are available to devices on the LAN.

  3. Reminder: Your host machine should be configured to use a static IP address instead of being assigned one by your router's DHCP. Since we are editing the hosts file of other devices to point to the server's IP, we don't want it to change.

  1. NameVirtualHostApache 2.3.11版本后不再使用 ,可以省略。

    在 2.3.11 及更高版本中,任何时候在多个虚拟主机中使用 IP 地址和端口组合时,都会自动为该地址启用基于名称的虚拟主机。

  2. 因为我们讨论的是通过 LAN 托管网站,所以让我们设置一个要求*,只接受来自本地网络上 IP 地址的连接。例如,在普通的 Linksys 路由器上,分配给网络上每个设备的默认 IP 介于 192.168.1.100 到 192.168.1.255 之间。您可以通过使用允许来自 LAN 上 IP 地址为 192.168.1.XXX 的所有设备的连接Require ip 192.168.1(注意最后一个八位字节从 IP 中删除以允许整个范围)。

    这允许您配置每个项目的访问权限,以便一个可以通过 LAN 访问,而另一个只能在本地访问。

    # This will allow all LAN connections to www.project1.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project1/"
        <Directory  "D:/websites/project1/">
            Require local
            Require ip 192.168.1
        </Directory>
        ServerName www.project1.com
    </VirtualHost>
    
    # This will allow only the machine hosting the website to access www.project2.com
    <VirtualHost *:80>
        DocumentRoot "D:/websites/project2/"
        <Directory  "D:/websites/project2/">
            Require local
        </Directory>
        ServerName www.project2.com
    </VirtualHost>
    

    如果没有路由器将端口 80 上的流量转发到您的主机,您的站点将不会公开提供服务,但我相信这被认为是最佳实践。如果您需要控制 LAN 上的设备可以使用哪些项目,则尤其需要。

  3. 提醒:您的主机应配置为使用静态 IP 地址,而不是由路由器的 DHCP 分配。由于我们正在编辑其他设备的 hosts 文件以指向服务器的 IP,因此我们不希望它更改。

* I'm including this because it is common to have access restrictions on a local development server and you will need to specifically make it available to your local network.

* 我包含这个是因为在本地开发服务器上有访问限制是很常见的,你需要专门让它对你的本地网络可用。

回答by Gabriel Baker

You need to change the hosts file on the machine you're trying to view the page from.

您需要更改您尝试从中查看页面的机器上的主机文件。

So you need to add them to the hosts file on the other lan machine.

所以你需要将它们添加到另一台局域网机器上的主机文件中。