如何在php中访问nginx虚拟目录?

Rad*_*dek 9 php nginx

假设我有一个Web服务器(nginx)server.com,我只有一个php文件index.php(没有目录结构).我希望能够在server.com之后访问任何内容.它将是一个网址结构.例如server.com/google.com,server.com/yahoo.com.au等...

一个例子是http://whois.domaintools.com/google.com(他们没有名为/google.com的目录,对吧?)

Q1:如何从index.php访问'server.com'之后的内容

Q2:我可以从这样的URL获取协议吗?例如server.com/http://www.google.comserver.com/https://www.google.com

PS 我不确定这里是否正确使用术语虚拟目录.我只想做我在其他地方看到的事情.

mat*_*boy 11

location / {
    rewrite ^/(.*)$ /index.php?q=$1
}

location = /index.php {
    #Do your normal php passing stuff here now
}
Run Code Online (Sandbox Code Playgroud)

那是你在找什么?

作为第二个问题的答案,您可以在php中解析协议.Nginx不需要这样做.要解析网址,您可以使用该parse_url功能


gho*_*oti 7

好吧,matzahboy和VBart已经提供了nginx配置摘录,正确地向您展示了如何将URL重写为GET变量.但是为了使用它,你必须解释提供的值$_GET['q'].您尚未指定要遵循的规则,因此这是一个建议.

要按此顺序进行测试:

  1. 使用PHP的验证过滤器对每个RFC2396的有效URL:使用cURL进行测试,对于HTTP响应代码<400,响应为TRUE,对于其他任何代码,则为FALSE.
  2. (host.)example.com/path(缺少协议):假定HTTP协议,每#1测试一次.
  3. host.example.com(仅限主机名):与#2相同
  4. example.com(仅限域):测试为#2,然后测试为 www.example.com.
  5. 别的什么:失败.

如果这对您有意义,那么以下index.php可能会让您入门:

<?php

function http_response($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, TRUE);
  curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $head = curl_exec($ch);
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);

  if (!$head) {
    return FALSE;
  }

  if ($httpCode < 400) {
    return $url;
  } else {
    return FALSE;
  }
}

function test_string($q) {
  if (filter_var($q, FILTER_VALIDATE_URL)) {
    // Matches RFC2396, so let's generate a hit.
    return http_response($q);
  }
  elseif (preg_match('/^([a-z0-9][a-z0-9-]+\.)+[a-z]{2,}(:[0-9]+)?\/.+$/', $q)) {
    // Matches: (host.)example.com/path
    return http_response("http://" . $q);
  }
  elseif (preg_match('/^([a-z0-9][a-z0-9-]+\.){2,}[a-z]{2,}$/', $q)) {
    // Matches: host.example.com
    return http_response("http://" . $q . "/");
  }
  elseif (preg_match('/^([a-z0-9][a-z0-9-]+\.)+[a-z]{2,}$/', $q)) {
    // Matches: example.com
    $ret=http_response("http://" . $q . "/");
    if ($ret === FALSE) {
      return http_response("http://www." . $q . "/");
    } else {
      return $ret;
    }
  }
  else {
    return FALSE;
  }
}

$q = $_GET['q'];
//$q = $argv[1]; // for command-line testing

$url = test_string($q);

if ($url === FALSE) {
  printf("<p>The URL <strong>%s</strong> is invalid.</p>\n", $q);
} else {
  printf("<p>The URL is <strong>%s</strong>.</p>\n", $url);
}
Run Code Online (Sandbox Code Playgroud)

我并不认为这是最漂亮或最安全的代码,但至少它为所提供的URL实现了分析策略,例如:

  • http://example.com/https://www.example.net/foo/bar,
  • http://example.com/example.org/foo/bar 要么
  • http://example.com/example.org

请注意,cURL的gopher支持可能会被破坏,上面的代码不支持其他协议(不返回HTTP响应代码).如果您需要支持HTTP和HTTPS以外的协议,请在您的问题中说明,我会相应地调整PHP.

具体来说,如果你想要检查http://example.com/ping://host.example.net它并不难,但它必须与cURL处理的位分开编码.

  • 是的,cURL通常被推荐用于此类事情.它只是为您提供了比一个月内可以自己编码的任何东西更多的烘焙功能.但有一个附带条件; 它需要一个PHP模块,默认情况下可能不会在您的Web服务器上安装.在Debian/Ubuntu上,你会'sudo apt-get install php5-curl`.在FreeBSD上,你可能会`cd/usr/ports/ftp/php5-curl; make install clean`.如果您遇到问题,请向我们提供一些操作系统详细信息...或在[ServerFault](http://serverfault.com/)中发布问题.:-) (2认同)

VBa*_*art 7

location / {
    try_files $uri @dynamic;
}

location @dynamic {
    fastcgi_pass backend;

    include fastcgi_params;
    fastcgi_param  PATH_INFO        $uri;
    fastcgi_param  SCRIPT_NAME      /index.php;
    fastcgi_param  SCRIPT_FILENAME  /absolute/path/to/index.php;
}
Run Code Online (Sandbox Code Playgroud)

fastcgi_params文件与nginx捆绑在一起

$ cat fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

#fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
Run Code Online (Sandbox Code Playgroud)

并且您可以使用$_SERVERPHP中的内置数组访问所有这些fastcgi环境参数.http://php.net/manual/en/reserved.variables.server.php