How to block specific user agents on nginx web server

To configure user-agent block list, open the nginx configuration file of your website, where the server section is defined. This file can be found in different places depending on your nginx setup or Linux distribution (e.g., /etc/nginx/nginx.conf, /etc/nginx/sites-enabled/<your-site>, /usr/local/nginx/conf/nginx.conf, /etc/nginx/conf.d/<your-site>).

location / {
if ($http_user_agent ~* (ahrefs|megaindex|majestic|mj12) ) {
return 403;
}

You can test it:

wget –user-agent “malicious bot” http://<nginx-ip-address&gt;

Render latest git date commit in django

Create file on server:

nano pull_repo.sh

#!/bin/sh

git –work-tree=/home/…web/ –git-dir=/home/…web/.git pull origin dev

git –work-tree=/home/…web/ –git-dir=/home/…web/.git log -1 –format=%cd > /home/…web/last.commit_date

chmod +x pull_repo.sh

Edit crontab:

crontab -e

Add line:

*/5 * * * * /home/…/pull_web.sh

In Django

decorator:

def prepare_commit_date(function):
    def wrap(request, *args, **kwargs):
        try:
            with open(os.path.join(BASE_DIR, "last.commit_date"), "r") as last_commit_date:
                request.commit_date = last_commit_date.readlines()[0].replace('\n', '')
        except Exception as ex:
            request.commit_date = str(ex.message)
        return function(request, *args, **kwargs)
    return wrap

view:

@prepare_commit_date
def index(request):
 return render(request, 'index.html', {})

template:

{{ request.commit_date }}