Wednesday, April 3, 2019

Nginx Custom 404 Not Found Error or Internal Server Error page for Django

If you are trying to configure nginx custom error page for a Python web application like Django but it always shows Django Default 404 Error Page, here is solution for you.

All solutions you found out but they are not working because they are showing you solution on custom error page for application which does not need proxy pass, Django runs on port 8000 (By Default) and you must have used proxy pass in your nginx virtual host for domain configuration.

You need to give one extra attribute in your virtual host and all will work well.

1) Create an index.html in /usr/share/nginx/html/index.html

2) Add following lines under active virtualhost     

        proxy_intercept_errors on;
        error_page 500 502 503 504 404 /index.html;
        location = /index.html {
        root /usr/share/nginx/html;


     
3) Reload nginx. Now open an error page or 404 page. It will open index.html content in browser.

Note : Your solutions were not working because you did not use attribute `proxy_intercept_errors on;`
 For a proxy pass virtualhost, this attribute is important.

s3cmd ERROR: Test failed: 400 (InvalidToken): The provided token is malformed or otherwise invalid

If you are trying to configure your AWS s3 bucket in s3cmd and you get Test Failed Error 400 although you have used correct secret and access keys. Here is solution for you.

You need to export both keys on terminal before configuring them.

Run these commands :
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE



export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY


Now configure
s3cmd --configure

You will not get the error while testing credentials.

nginx htpasswd for a django or proxy pass url location

When you try to configure htpasswd for a url location in nginx, you may get error connection time out, Incorrect Redirect or 404 not found. Most of the time reason is incorrectly configured proxy pass. If there is python or ruby based app where you have used proxy pass to access your web application, this error is very much common.

Tricky part if you try to set an htpasswd on login page but you do not land on My account page after login. It shows 404 not found and when you remove your htpasswd, this error does not show up after entering your login credentials.

So how can you set htpasswd successfully for a url location?

Solution :
Open nginx configuration file where you have set virtualhost for your domain. Add this location snippet in it. Add it below location / { ... }

Suppose you want to set htpasswd on login url i.e. http://domain.com/login. It should prompt htpasswd. Rest of the site should work fine without htpasswd.
  location /login {

        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host; 
        proxy_pass         http://127.0.0.1:8000$uri;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
Use $uri in proxy_pass attribute after port. You will not get 404 Not Found error after login.