无法从本地网络访问托管在 apache 上的 Web 应用程序

Can not access web app hosted on apache from local network

作为新手,我在 linux 电脑上的 apache httpd 中设置了 mediawiki。在相应的 .conf 文件中设置了“要求全部授予”。路由器为 pc 分配了静态 IP 192.168.0.2。 pc的80端口对任何设备开放。我从我的 phone 访问了 http://192.168.0.2/mediawiki/index.php from the host. It worked as expected. Now I tried to visit the same address from my android phone connected to the same network and I was presented with the message "127.0.0.1 refused to connect" by chrome. I noticed that the part http://192.168.0.2 had been replaced by https://127.0.0.1 in the address bar. The same thing also occurred on Firefox on my phone. I can still visit http://192.168.0.2,它工作得很好。那么这里发生了什么以及如何解决这个问题?

编辑:问题已解决。 Mediawiki 有这个 LocalSettings.php 文件,它会在安装过程中自动创建。它包含一个字段“$wgServer”,它是“在完全限定的 URL 中使用的协议和服务器名称”,如文档中所示。该值在安装期间自动设置为“http://127.0.0.1”。因此,每当我尝试从我的 phone 连接到 mediawiki 时,我实际上都被重定向到相同的 phone。我将值更改为“http://192.168.0.2”,现在一切正常!

从您的手机 phone 连接到托管网络服务器的电脑时似乎发生了一些问题。 即使您几乎没有提供任何详细信息,您的配置看起来也不错:根据您的描述,我怀疑问题出在您的 phone 或您的路由器上,因为域和 url 从 192.168.0.2 切换到127.0.0.1 并且协议从 http 切换到 https。

您应该在连接到同一网络的另一台电脑上尝试相同的测试。 您可以更好地了解正在发生的事情,使用 curl:

从命令行测试连接

$ curl -v http://192.168.0.2/mediawiki/index.php

如果网络服务器请求了重定向,输出将为您提供一些详细信息。

我的猜测是您的 apache 配置为侦听 127.0.0.1 或 localhost(这是默认设置)。打开 httpd.conf 或您的虚拟主机配置文件并查找 Listen。以下是官方文档中的一些片段:

The Listen directive tells the server to accept incoming requests only on the specified port(s) or address-and-port combinations. If only a port number is specified in the Listen directive, the server listens to the given port on all interfaces. If an IP address is given as well as a port, the server will listen on the given port and interface. Multiple Listen directives may be used to specify a number of addresses and ports to listen on. The server will respond to requests from any of the listed addresses and ports.

For example, to make the server accept connections on both port 80 and port 8000, on all interfaces, use:

Listen 80
Listen 8000

To make the server accept connections on port 80 for one interface, and port 8000 on another, use

Listen 192.0.2.1:80
Listen 192.0.2.5:8000

更多信息请访问

https://httpd.apache.org/docs/2.4/bind.html