0

I'm developing a website using Django framework and Nginx. Would like to create a false system error and observer how my system responds.

This is a nice article however this only create false Error 502 only. I would like to test other error pages(HTTP403, HTTP404, HTTP405 or common error page) before going live.

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

2 Answers 2

1

401 Unauthorized

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource.

To achieve this, you can add a new location and restrict it with a htaccess. Create htaccess file with htpasswd -c /etc/nginx/htpasswd/htpasswd.domain username. Then add a new location to your nginx config and inside that location, put these:

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

Now if you go tot he location specified by you and the auth_basic was implemented, then the website will ask for a username and password. If you enter incorrect data, it will drop you the 401 error code.

403 Forbidden

The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account of some sort.

Just add a new location that you have and deny access to it and tell NginX to return a specific error code.

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

404 Not Found

The requested resource could not be found but may be available in the future. Subsequent requests by the client are permissible.

Similar to the previous version:

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

This should deny your access to that location with a 404.


Obviously restart NginX as the last step. for each modifications.

In theory it is enough if you just add all the locations and always return different error codes.

0

Place a small script on the server that allows you to provide the error code you want to check for.

An example in PHP (this should be easily ported to any scripting language that works on your server):

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

This allows you to test any error you want, without further server 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!!!

Not the answer you're looking for? Browse other questions tagged .