Nginx anti hotlinking and Nginx access control with Nginx parsing php configuration

  • 2020-05-13 04:41:52
  • OfStack

Nginx anti-hotlinking and Nginx access control with Nginx parsing php configuration

Nginx hotlinking prevention

The following configuration can be combined with the above configuration


location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
  expires 7d;
  valid_referers none blocked server_names *.test.com ;
  if ($invalid_referer) {
    return 403;
  }
  access_log off;
}

Nginx access control

Requirements: requests to access /admin/ directories, only certain IP can be accessed.

The configuration is as follows:


location /admin/
{
  allow 192.168.133.1;
  allow 127.0.0.1;
  deny all;
}

Create the test


mkdir /data/wwwroot/test.com/admin/
echo  " test,test " >/data/wwwroot/test.com/admin/1.html

Detection to restart


/usr/local/nginx/bin/nginx -t && -s reload

test


 curl -x127.0.0.1:80 test.com/admin/1.html -I
 curl -x192.168.133.130:80 test.com/admin/1.html -I

Nginx access control

The configuration is as follows:


  location ~ .*(abc|image)/.*\.php$
{
    deny all;
}

Limit by user_agent


if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
   return 403;
}

deny all and return 403 effect 1 like

Nginx parses the configuration of php

The configuration is as follows:


location ~ \.php$
  {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php-fcgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
  }

fastcgi_pass is used to specify the address to which php-fpm is listening or socket

The above is the explanation of Nginx anti-hotlinking and Nginx access control and Nginx analysis of php configuration, if you have any questions, please leave a message or to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: