apache rewrite the rewrite, log cutting, anti-hotlinking

A, rewrite rewrite

mod_rewriteIt provided based on regular expressions URL of the incoming request method for dynamically altering the rules. You can define any of the url mapped to the internal site files

1 illustrates the phenomenon, resolve the effect, come to rewrite the concept

2-1 explain how to implement specific steps that can bring the principles of

Practice 2-2

3 The principle analysis, to enhance their knowledge

4 Summary

1, rewrite demand

When we use Apache as the Web server, sometimes due to optimization or SEO is a simple url path, url need to enter into a more friendly url, this time you can use rewrite overwrite function.

rewrite can also implement security chain, domain jump

使用rewrite前:article/php?ip=1
使用rewrite后: article/1.html

Use rewrite function must first enable the mod_rewrite module. yum install apache default has been opened.

2, rewrite use Comments

rewrite rules can be configured in the Directory command

three core rewrite learning is RewriteEngine , RewriteCond , RewriteRule

2.1 RewriteEngine

Rewrite master switch function, to enable the rewriting rewrite

RewriteEngine on

2.2 RewriteCond

RewriteCond rules define conditions, when the request condition is satisfied RewriteCond configuration, the latter statement is executed RewriteRule RewriteCond

such as:

RewriteEngine on
RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla//5/.0.*
RewriteRule  index          index.html    

The above rule says: If a match to http request HTTP_USER_AGENT is Mozilla // 5 / .0 beginning with *. When accessing index, it will automatically have access to index.html

RewriteCond and RewriteRule corresponding vertical relationship. Or you may have a several RewriteCond to match a RewriteRule

** RewriteCond common HTTP request matches the way

RewriteCond %{HTTP_REFERER} (www.mytest.com)
RewriteCond %{HTTP_USER_AGENT}  ^Mozilla//5/.0.*
RewriteCond %{REQUEST_FILENAME} !-f

HTTP_REFERER

Determine the source of visitors

Case:

RewriteCond %{HTTP_REFERER} (www.mytest.com)
RewriteRule (.*)$ mytest.html
# 如果访问的上一个页面是www.mytest.com,无论当前访问的是哪个页面,都会跳转到mytest.html

REQUEST_FILENAME

Matching files in the current access

Case:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/sports/(\d+)\.html web/index\.php?c=news&a=sports&num=$1 [QSA,NC,L]

# 访问news/sports/123.html,真实访问的是web/index.php?c=news&a=sports&num=123

-dWhether it is a directory, not a directory to determine whether:!-d

-fWhether it is a file, whether or not a judge asking price:!-f

$1Indicates that the first parameter

2.3 RewriteRule

RewriteRule is used together with RewriteCond, RewriteRule implementation process is the successful match RewriteCond

RewriteRuleWording:

RewriteRule Pattern Substitution [flags]

PatternIt is a regular match

SubstitutionReplace the contents match

[flags]Parameter limits

[QSA]qsappend (append query string) meaning, once flag forces the rewriting engine to append a query string, rather than simply replace the existing replacement string. If additional information in the query string via a rewrite rule can use this tag.

NCnocase (ignoring case) meant that the Pattern ignore case, that is, when the Pattern URL match the current, "AZ" and "az" no difference. This generally will add, because our url itself is not case-sensitive.

Rredirect(强制重定向)的意思,适合匹配Patter后,Substitution是一个http地址url的情况,就调整出去了。

Llast (ending rule) meaning, that is, have been matched to, stop immediately and no longer match the following Rule, similar to the programming language breaksyntax, jump out of.

Two, apache logs cut

1, Why the log cutting

With increasing access to the site, the log files generated by the web service will be growing, and this time the log file server not only takes up a lot of space, but also a lot of trouble log analysis

2, the log is divided in two ways

2.1 rotatelogs

rotatelogs are apache built-in log cutting tool

Case: Use rotatelogs recording a daily log file

# 编辑httpd主配置文件 /etc/httpd/conf/httpd.conf
# 注释下面两行
ErrorLog "logs/error_log" 
CustomLog "logs/access_log" combined

# 添加下面两行
ErrorLog "|/usr/sbin/rotatelogs -l logs/error_%Y%m%d.log 86400"
CustomLog "|/usr/sbin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined

Description:

86400 is the rotation time, seconds

2.2 timeline

Cronolog is a log Round Robin (rotation) tool, you can use it to cut the log output on Apache, Tomcat and other Web server into a file saved on a daily or monthly.

cronolog installation

[root@ ~]# tar zxf cronolog-1.6.2.tar.gz
[root@ ~]# cd cronolog-1.6.2/
[root@ cronolog-1.6.2]# ./configure && make && make install

Case: Use cronologs recording a daily log file

ErrorLog "|/usr/local/sbin/cronolog logs/error-%Y%m%d.log"
CustomLog "|/usr/local/sbin/cronolog logs/access-%Y%m%d.log" combined

Extension: generating log hourly poll

CustomLog "|/usr/local/sbin/cronolog logs /access_%Y%m%d%H.log" combined

3 summary

Recommended cronolog, because cronolog stable high-profile simple.

Three, apache security chain

Anti-hotlinking site code is to prevent other people call our server images, documents, video and other resources. If someone else theft of our resources, will increase the pressure on the server loans.

By anti-hotlinking way, you can set limits on third-party sites to obtain pictures of the server by way of reference, the picture data if you want to get this site, this site can only be accessed through the acquisition, which also effectively reduce the server resource of.

1, rewrite achieve security chain

  • The first hosts and second hosts are installed apache service, can normally access
  • The first Home Add a Photo Gallery
[root@localhost html]# vi index.html
<h1>防盗链主机</h1>
<img src='123.pag'/>
1. RewriteEngine On
2. RewriteCond %{HTTP_REFERER} !^http://www.myitcast.com/.*$ [NC]
3. RewriteCond %{HTTP_REFERER} !^http://www.myitcast.com$ [NC]
4. RewriteCond %{HTTP_REFERER} !^http://myitcast.com/.*$ [NC]
5. RewriteCond %{HTTP_REFERER} !^http://myitcast.com$ [NC]
6. RewriteRule .*\.(gif|jpg|swf)$ http://www.myitcast.com/link.png [R,NC]

Description:

Article 1: Turn rewrite rewrite

2 to 5: trust granted to open the site, the site can access resources pictures

Article 6: gif visited sites | jpg | swf when other types of resources, to jump

2、SetEnvIfNoCase

Daolian request by determining whether to block the browser header information

SetEnvIfNoCase Referer "^$" local_ref
SetEnvIfNoCase Referer "www.benet.com/.*$" local_ref
SetEnvIfNoCase Referer "benet.com/.*$" local_ref
<filesmatch "\.(mp3|mp4|zip|rar|jpg|gif)">
        Require all denied
        Require env local_ref
</filesmatch>

Description:

SetEnvIfNoCase when certain conditions are met, assigning a variable, i.e., the request attribute set the environment variable according to the client.

Referer: URL indicates the current resource request the original resource

Guess you like

Origin www.cnblogs.com/li-dy/p/12091467.html