如何configurationNgnix来实现任何可能的网站错误进行testing

我正在开发一个使用Django框架和Nginx的网站。 想创build一个错误的系统错误和观察者如何我的系统响应。

这是一篇不错的文章,但是这只会产生错误的错误502。 我想在上线之前testing其他错误页面(HTTP403,HTTP404,HTTP405或常见错误页面)。

# Testing for Error 502 - Bad Gateway location /testing { fastcgi_pass unix:/does/not/exist; } 

401未经授权

与403 Forbidden类似,但是专门用于需要身份validation且失败或尚未提供的情况。 响应必须包含一个WWW-Authenticate标题字段,其中包含适用于所请求资源的挑战。

要做到这一点,你可以添加一个新的位置,并用htaccess来限制它。 使用htpasswd -c /etc/nginx/htpasswd/htpasswd.domain username创buildhtaccess文件。 然后添加一个新的位置到你的nginxconfiguration,并在该位置,把这些:

 auth_basic "Restricted"; auth_basic_user_file /etc/nginx/htpasswd/htpasswd.domain; 

现在,如果你到达你指定的位置,auth_basic被执行,那么网站将要求input用户名和密码。 如果input的数据不正确,将会丢失401错误代码。

403禁止

请求是有效的,但服务器拒绝操作。 用户可能没有资源所需的权限,或者可能需要某种帐户。

只需添加一个新的位置,并拒绝访问它,并告诉NginX返回一个特定的错误代码。

 location ~ /(dir1|dir2|dir3) { deny all; return 403; } 

404没有find

请求的资源找不到,但可能在将来可用。 客户的后续请求是允许的。

类似于以前的版本:

 location ~ /(dir1|dir2|dir3) { deny all; return 404; } 

这应该拒绝你用404访问该位置。


显然重启NginX是最后一步。 为每个修改。

从理论上讲,只要添加所有的位置并且总是返回不同的错误代码就足够了。

在服务器上放置一个小脚本,允许您提供要检查的错误代码。

在PHP中的一个例子(这应该很容易地移植到您的服务器上的任何脚本语言):

 <?php $errorCode = ( isset( $_GET['code'] ) ) ? intval( $_GET['code'] ) : 200; header( 'Test error', true, $errorCode ); 

这允许您testing任何你想要的错误,而无需进一步的服务器configuration:

 $ wget -S --spider http://localhost/error.php?code=404 Spider mode enabled. Check if remote file exists. --2017-11-24 10:23:44-- http://localhost/error.php?code=404 Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 404 Not Found Date: Fri, 24 Nov 2017 09:23:44 GMT Server: Apache/2.4.7 (Ubuntu) X-Powered-By: PHP/5.5.9-1ubuntu4.22 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html Remote file does not exist -- broken link!!! $ wget -S --spider http://localhost/error.php?code=501 Spider mode enabled. Check if remote file exists. --2017-11-24 10:23:47-- http://localhost/error.php?code=501 Resolving localhost (localhost)... ::1, 127.0.0.1 Connecting to localhost (localhost)|::1|:80... connected. HTTP request sent, awaiting response... HTTP/1.1 501 Not Implemented Date: Fri, 24 Nov 2017 09:23:47 GMT Server: Apache/2.4.7 (Ubuntu) X-Powered-By: PHP/5.5.9-1ubuntu4.22 Connection: close Content-Type: text/html Remote file does not exist -- broken link!!!