【问题标题】:Calling localhost on a multidomain nginx server在多域 nginx 服务器上调用 localhost
【发布时间】:2013-09-16 15:34:22
【问题描述】:

我正在使用 nginx 在一台服务器上运行多个(子)域。其中一个是domain.com,另一个是api.domain.com。我想使用来自主域的 API 的服务器端调用。

如果我从domain.com 到api.domain.com 发出HTTP 请求,数据会从服务器中流出,还是服务器会识别出是他被请求,并立即处理请求?我问这个,因为第二个可能更快。

如果它不识别它,我想向 localhost 发出请求。但是,当我调用 localhost 时,nginx 服务器不知道要服务的域。有没有办法告诉 nginx 如何处理对 localhost 的请求,通过传递请求的主机参数,以便服务器知道他必须服务器 api.domain.com 而不是服务器上的其他域?如果它很重要,我正在使用 cURL - 但我也可以随意使用其他任何东西。

【问题讨论】:

    标签: php http curl nginx localhost


    【解决方案1】:

    解决方案是在文件中添加一行:/etc/hosts

    127.0.0.1 api.domain.com
    

    保存文件后,如果您在服务器上运行 ping,它应该返回如下内容:

    user@domain:~$ ping api.domain.com
    PING api.domain.com (127.0.0.1) 56(84) bytes of data.
    64 bytes from localhost (127.0.0.1): icmp_req=1 ttl=64 time=0.026 ms
    64 bytes from localhost (127.0.0.1): icmp_req=2 ttl=64 time=0.030 ms
    64 bytes from localhost (127.0.0.1): icmp_req=3 ttl=64 time=0.031 ms
    64 bytes from localhost (127.0.0.1): icmp_req=4 ttl=64 time=0.029 ms
    

    文件 /etc/hosts 将比您的机器 DNS 服务器具有更高的优先级来解析名称。

    如果一切配置正确,您的 cURL 现在应该使用 localhost 从 api.domain.com 页面检索页面。您可以通过使用详细选项运行 curl 来验证它:

    user@domain:~$ curl --verbose api.domain.com
    * About to connect() to api.domain.com port 80 (#0)
    *   Trying 127.0.0.1... connected
    

    第一行会让您知道您是否确实使用 localhost ip 进行请求!


    在极不可能的情况下,检查文件:/etc/nsswitch.conf 并验证 hosts 行看起来像这样(文件优先于 dns 服务器):

    hosts: files dns
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 2012-12-06
      • 1970-01-01
      相关资源
      最近更新 更多