如何通过终端访问网页?

Kel*_*Kel 5 linux command-line-interface apache-2.2

我正在测试 Apache 服务器。通常我x.x.x.x/directory/index.php在 Firefox 中打开,然后阅读httpd/error_logs并排除故障。

我的问题是,如果我的测试系统上没有 Web 浏览器(例如 Chrome/Firefox)怎么办?如何通过终端进行等效测试?我试过了,ping x.x.x.x/directory/index.php但它不能那样工作。

Gia*_*968 7

我的问题是,如果我没有打开 Chrome/Firefox 怎么办?如何通过终端进行等效测试?我试过了,ping x.x.x.x/directory/index.php但它不能那样工作。

使用ping永远不会奏效。所有ping没有被发送/从网络地址接收ICMP数据包。因此,在您的示例中,您实际上唯一可以“ping”的是ping x.x.x.xURL ( /directory/index.php)的其余部分,ping因为它试图将整个URL解析为主机名。错误将类似于:

ping: cannot resolve x.x.x.x/directory/index.php: Unknown host
Run Code Online (Sandbox Code Playgroud)

但是对于您正在寻找的特定类型的 Web 服务器测试/调试,我通常使用curl但特别是我使用curl -I -L它只会返回基本响应标头并跟踪服务器可能具有的任何位置重定向;该-I标志告诉curl只显示标题和-L标志告诉curl遵循它遇到的任何服务器重定向。

For example, if I run this curl -I -L command on google.com:

curl -I -L google.com
Run Code Online (Sandbox Code Playgroud)

I get the following response headers:

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 24 Aug 2015 02:16:32 GMT
Expires: Wed, 23 Sep 2015 02:16:32 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

HTTP/1.1 200 OK
Date: Mon, 24 Aug 2015 02:16:32 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: PREF=ID=1111111111111111:FF=0:TM=1440382592:LM=1440382592:V=1:S=5ToXkoBHyK2BAjyf; expires=Thu, 31-Dec-2015 16:02:17 GMT; path=/; domain=.google.com
Set-Cookie: NID=70=VKM1D8HeCMlye1YjMDYSqPlyIpPHKkitAor--wiqYznamENfNig69ZBW5oBgIR7wOFzVaUB6i4WKj-tqa2WcqbOCeVTc0hB4xQWQzBxpNazPp_20dBiU4in0wIop8mhz; expires=Tue, 23-Feb-2016 02:16:32 GMT; path=/; domain=.google.com; HttpOnly
Transfer-Encoding: chunked
Accept-Ranges: none
Vary: Accept-Encoding
Run Code Online (Sandbox Code Playgroud)

Note there are two headers returned:

  • HTTP/1.1 301 Moved Permanently
  • HTTP/1.1 200 OK

This is useful not just for Apache server testing but also useful for debugging mod_rewrite rewrite rules and such.

This curl -I -L method is far more useful and efficient than using a visual browser like Chrome or Firefox since those programs are designed to optimize browsing speed by caching content. Which means you can make an adjustment to your Apache server one second, but the visual browser won’t necessarily show the change right away unless you clear the cache or force the page to reload a few times. The curl -I -L shows you exactly what the server at that moment is doing in response to your request which is exactly what you want/need when debugging server configs.


wom*_*ble 6

您想要的命令是curlwget(取决于您的个人喜好)。这些命令向服务器发出 HTTP 请求。它们不太适合模拟整个页面的加载(默认情况下,它们不会加载 HTML 页面引用的资产,并且它们根本无法执行 javascript 或与页面交互),但它不会听起来不像你在追求任何先进的东西。所以,curl或者wget几乎肯定会为你工作。