Apache/Nginx pseudo-static match http: // problem and solution

Source: Internet
Author: User
Tags php code browser cache

The problem is that I set up an API interface for capturing the icon of a website. Normally, the object's passing parameters are obtained through $ _ GET ['URL, therefore, the address of the commonly obtained icon should be:

Http://domain.com /? Url = zhangge.net

Or

Http://domain.com /? Url = http://zhangge.net

In order to enable browser caching and subsequent CDN caching, my design philosophy is as follows:

① Create a cache folder under the icon API website directory and save the icon file in the form of domain name. ico, such as zhangge.net. ico

② When capturing the ico of a website, you first use Nginx or Apache to determine whether a cached file exists. If yes, the cached file is directly returned to the browser. In this way, if CDN is not enabled, because the returned static files are pure files, the browser will automatically cache them, that is, the 304 status will be returned, and the loading speed will be improved!

To enable browser caching, I will use the following pseudo-static addresses:

Http://domain.com/zhangge.net

Or

Http://domain.com/http://zhangge.net

This is a pseudo-static rule in Nginx:
# Rewrite the request containing http: //, remove http: //, and save the dynamic judgment of php code.
Rewrite ^/http: // (. *) $/cache/$ 1.ico last;

# The following judgment mainly aims to prevent the elements on the API homepage from being pseudo-static (finally, use the logic judgment $ type = abc )!
Set $ type '';
If (! -F $ request_filename) {# Exclude existing file requests in order not to conflict with static resources on the API page
Set $ type;
}
If ($ request_uri !~ (\. |/) $) {# Requests that contain or end with a slash (/) are not matched. In order to be compatible with the homepage [/] request;
Set $ type '$ {type} B ';
}
If ($ request_uri !~ Cache) {# The request containing the cache does not match the first rule
Set $ type '$ {type} c ';
}

# Nginx does not support the combination of multiple conditions. Therefore, you can determine the flag separately and then merge and determine the flag:
If ($ type = abc ){
# Rewrite all requests other than the condition to cache/domain name. ico
Rewrite ^/(. *) $/cache/$ 1.ico last;
}

# If the requested file already exists, it will be directly returned to the user, instead of using PHP
If (-f $ request_filename ){
Break;
}

# If the requested file does not exist, submit it to index. php for processing.
Rewrite ^/cache/(. *). ico $/index. php? Url = $1 last;

# Rewrite the request containing http: //, remove http: //, and save the dynamic judgment of php code.
Rewrite ^/http: // (. *) $/cache/$ 1.ico last;
 
# The following judgment mainly aims to prevent the elements on the API homepage from being pseudo-static (finally, use the logic judgment $ type = abc )!
Set $ type '';
If (! -F $ request_filename) {# Exclude existing file requests in order not to conflict with static resources on the API page
Set $ type;
}
If ($ request_uri !~ (\. |/) $) {# Requests that contain or end with a slash (/) are not matched. In order to be compatible with the homepage [/] request;
Set $ type '$ {type} B ';
}
If ($ request_uri !~ Cache) {# The request containing the cache does not match the first rule
Set $ type '$ {type} c ';
}
 
# Nginx does not support the combination of multiple conditions. Therefore, you can determine the flag separately and then merge and determine the flag:
If ($ type = abc ){
# Rewrite all requests other than the condition to cache/domain name. ico
Rewrite ^/(. *) $/cache/$ 1.ico last;
}
 
# If the requested file already exists, it will be directly returned to the user, instead of using PHP
If (-f $ request_filename ){
Break;
}
 
# If the requested file does not exist, submit it to index. php for processing.
Rewrite ^/cache/(. *). ico $/index. php? Url = $1 last;

It was found that it could not take effect! Why can't all of them match http: //? In the end, I had to rewrite the http: // parameter in php!

Today, I moved this icon API to the free HiChina host, which is an Apache environment, so I wrote it again according to nginx rules:
RewriteEngine on
RewriteBase/

# Rewrite and remove "http: //" from the request ://"
RewriteRule ^ http: // (. *) $/cache/$ 1.ico [L]

# Consistent condition judgment with nginx. To prevent the API homepage from being pseudo-static
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_URI }! (\. |/) $
RewriteCond % {REQUEST_URI }! Cache

# Rewrite all requests except the conditions to/cache/domain name. ico
RewriteRule ^ (. *) $/cache/$ 1.ico [L]

# If the file does not exist, it is thrown to index. php for processing.
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $/index. php? Url = $1 [L]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    
RewriteEngine on
RewriteBase/
 
# Rewrite and remove "http: //" from the request ://"
RewriteRule ^ http: // (. *) $/cache/$ 1.ico [L]
 
# Consistent condition judgment with nginx. To prevent the API homepage from being pseudo-static
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_URI }! (\. |/) $
RewriteCond % {REQUEST_URI }! Cache
 
# Rewrite all requests except the conditions to/cache/domain name. ico
RewriteRule ^ (. *) $/cache/$ 1.ico [L]
 
# If the file does not exist, it is thrown to index. php for processing.
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $/index. php? Url = $1 [L]

Still not good! It's strange. Why can't I match http? Therefore, various tests, such as caching URLs with colons and slashes, do not work!

In fact, after an nginx failure, I used php to GET $ _ GET ['URL'] and found that the http: // parameter in the result would be http: //, with one slash missing! And directly use http://domain.com /? Url = http://zhangge.net get is also http:/zhangge.net, less than a Slash!

Today, I tried to judge http:/in pseudo-static mode. The result is successful! I tried to match http: //, but it actually matches http:/, with a slash missing! Really incredible, never met before!
Therefore, the above two pseudo-static rules should be written as follows:
A. Nginx pseudo-static:

# Rewrite the request containing http: //, remove http: // from it, and save the dynamic judgment of php code (actually matching http :/)
Rewrite ^/http:/(. *) $/cache/$ 1.ico last;

# The following judgment mainly aims to prevent the elements on the API homepage from being pseudo-static together!

If (-f $ request_filename) {# in order not to conflict with static resources on the API page
Set $ type 1;
}
If ($ request_uri ~ (\. |/) $) {# In order not to conflict with the API homepage, that is,/This request
Set $ type 1;
}
If ($ request_uri ~ Cache) {# conflict with the first rule
Set $ type 1;
}

# Nginx does not support multiple criteria to be determined together, so it is written separately.
If ($ type! = 1 ){
# Rewrite all requests other than the condition to cache/domain name. ico
Rewrite ^/(. *) $/cache/$ 1.ico last;
}

# If the requested file already exists, it will be directly returned to the user, instead of using PHP
If (-f $ request_filename ){
Break;
}

# If the requested file does not exist, submit it to index. php for processing.
Rewrite ^/cache/(. *). ico $/index. php? Url = $1 last;
    
# Rewrite the request containing http: //, remove http: // from it, and save the dynamic judgment of php code (actually matching http :/)
Rewrite ^/http:/(. *) $/cache/$ 1.ico last;
 
# The following judgment mainly aims to prevent the elements on the API homepage from being pseudo-static together!
 
If (-f $ request_filename) {# in order not to conflict with static resources on the API page
Set $ type 1;
}
If ($ request_uri ~ (\. |/) $) {# In order not to conflict with the API homepage, that is,/This request
Set $ type 1;
}
If ($ request_uri ~ Cache) {# conflict with the first rule
Set $ type 1;
}
 
# Nginx does not support multiple criteria to be determined together, so it is written separately.
If ($ type! = 1 ){
# Rewrite all requests other than the condition to cache/domain name. ico
Rewrite ^/(. *) $/cache/$ 1.ico last;
}
 
# If the requested file already exists, it will be directly returned to the user, instead of using PHP
If (-f $ request_filename ){
Break;
}
 
# If the requested file does not exist, submit it to index. php for processing.
Rewrite ^/cache/(. *). ico $/index. php? Url = $1 last;

B. Apache pseudo-static:

RewriteEngine on
RewriteBase/

# Overwrite and remove "http: //" in the request. It actually matches http :/
RewriteRule ^ http:/(. *) $/cache/$ 1.ico [L]

# Consistent condition judgment with nginx. To prevent the API homepage from being pseudo-static
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_URI }! (\. |/) $
RewriteCond % {REQUEST_URI }! Cache

# Rewrite all requests except the conditions to/cache/domain name. ico
RewriteRule ^ (. *) $/cache/$ 1.ico [L]

# If the file does not exist, it is thrown to index. php for processing.
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $/index. php? Url = $1 [L]

RewriteEngine on
RewriteBase/
 
# Overwrite and remove "http: //" in the request. It actually matches http :/
RewriteRule ^ http:/(. *) $/cache/$ 1.ico [L]
 
# Consistent condition judgment with nginx. To prevent the API homepage from being pseudo-static
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_URI }! (\. |/) $
RewriteCond % {REQUEST_URI }! Cache
 
# Rewrite all requests except the conditions to/cache/domain name. ico
RewriteRule ^ (. *) $/cache/$ 1.ico [L]
 
# If the file does not exist, it is thrown to index. php for processing.
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $/index. php? Url = $1 [L]

The article is very written ?? Close? What is the cause of this attack? What is the fading path ?? To match http: // in the request url in gsung or Apache, you must match http: //, that is, write a slash less! Bold guesses match other multiple slashes should also be less than one slash...

Well, I have written so many articles, and the website icon API has been successfully built on the HiChina free virtual host. The address is http://seo.zgboke.com/geticon/, although it is dedicated to the Chinese blog alliance, but if you have the need to call the icon, you can also use the premise of reasonable use of free play.

In addition, it is easy to check whether browser cache is implemented. You can access an ico address at will, for example:

Http://seo.zgboke.com/geticon/zhangge.net

Then press F12 to enter the development mode, locate the network (network tab), and refresh the page one more time to see the 304 status:

Apache/Nginx pseudo-static rule matching http: // Problems and Solutions

304 indicates that the current file is cached by the browser. Because the requested file is the same as the file in the service segment, you do not need to retrieve it again!

Of course, the pseudo-static rules written in this article are only a part. If you want to implement CDN acceleration, you have to add corresponding rules. However, this is all the end words, the next time I share the API source code of this website icon on the blog of Zhang Ge, it will be pasted together, so stay tuned!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.