Access virtual host from another machine over LAN

前端 未结 5 1066
遇见更好的自我
遇见更好的自我 2020-12-12 21:16
  • I am using Windows 7 with Wamp 2.2 server.
  • I have setup 2 virtual hosts: www.project1.com and www.project2.com.
  • I have
5条回答
  •  借酒劲吻你
    2020-12-12 21:30

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

    1. NameVirtualHost is no longer used after Apache version 2.3.11 and 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
      
          DocumentRoot "D:/websites/project1/"
          
              Require local
              Require ip 192.168.1
          
          ServerName www.project1.com
      
      
      # This will allow only the machine hosting the website to access www.project2.com
      
          DocumentRoot "D:/websites/project2/"
          
              Require local
          
          ServerName www.project2.com
      
      

      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.

    * 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.

提交回复
热议问题