Apache file does not exist jump URL (to achieve the specified size of the picture does not exist, the image is automatically generated)

Source: Internet
Author: User

Preface

When we do a lot of systems, we may have the need to:

Only one picture is maintained in the CMS, and this image needs to be displayed in many parts of the site, and the size of the display is different.

The general practice is to use this image directly, to set the display size of the image in different places using width and height.


Scenario Requirements

In general, this can actually be done. The following scenario, if done by doing so, may not be appropriate.

Scenario: CMS has a poster management function, each big poster is a picture of 1000*2000px, and the clarity of the poster has certain requirements, quality can not be too poor, assume a poster 1MB.

Then there is a list of posters on the site, showing 20 posters per page.

In this case, if we also use a picture, width and height to set the image display size, it is too inappropriate, a list page down, more than 20 MB No. The page will certainly be very slow to display.


Solution Ideas

Our general practice is to:

1, upload a large poster in the CMS, and then upload a small poster map.

2, in the CMS upload a poster large map, save the time, the system automatically generated a poster small map.

3, in the CMS upload a poster large image, in the Web request picture, with the width and height of the demand picture, the program automatically determine whether the needs of the picture exists, if not exist then use the corresponding large map to generate a picture response to the client.


Above three kinds of practice, 1, 22 kind of situation is not flexible, if the system still have more place need to display such picture of different size? or system two, phase three ... Is it possible to have more size picture requirements when expanding?

As for the third method, it is relatively flexible and only generated when we need it.


We now need to use Apache to process our static picture resources. In general, Apache will not take care of your picture is not there, when we visit a new size picture, the server does not have a picture file, Apache directly responds to 404.

Now all we have to do is let Apache determine if the file exists, and if it doesn't exist, ask us to request a picture.

In this way, each size of the picture, in demand, the first time Apache will give our program to generate pictures, later this picture exists, all by Apache own management.


Apache Configuration

Here's how Apache is configured to "forward requests to handlers when a file doesn't exist."

1. Open Rewrite module

Remove the "#" number in front of the #LoadModule rewrite_module modules/mod_rewrite.so in the httpd.conf file

2, add the configuration in the HTTPD.CNF (no location requirements, generally we put in the last file)

I am using Apache 2.4 VirtualHost, image processing program is used Tomcat, so also to Apache added MOD_JK module (This article does not explain, you can view the post http://blog.csdn.net/catoop/ article/details/47974773)

The configuration you need to add is:

        <ifmodule mod_rewrite.c> rewriteengine on # '-S ' (is regular file, with size) # '-l ' (is symbolic link) # '-d ' (is directory) # ' Ornext|or ' (OR next Condit ION) # ' nocase| NC ' (no case) # ' last|                L ' (Last rule) # ' QSA ' append request string Rewritecond%{document_root}%{request_filename}!-s Rewritecond%{document_root}%{request_filename}!-l rewritecond%{document_root}%{request_filename} !-f #RewriteRule ^ (. *) \. [jpg|png|bmp]$%{http_host}/createimage.jsp?path=%{request_uri} [nc,l] #RewriteRule ^ (. *) \. (jpg|png|bmp) $ createimage.jsp?path=%{request_uri} [nc,l] Rewriterule ^ (. *) \. (jpg|png|bmp) $ http://shanhyweb.example.com/createimage.jsp?path=%{request_uri} [nc,l] #RewriteRule ^ (. *) \. (jpg|png|bmp) $/createimage.jsp?path=%{request_uri} [nc,l] </IfModule>

The configuration can be added directly in httpd.cnf, or it can be configured in VirtualHost because I am using VirtualHost so my configuration is in apache2/conf/extra/httpd-vhosts.conf, as follows:

####################################################### #LoadModule jk_module modules/mod_jk.soinclude conf/mod_ Jk.conf########################################################<virtualhost *:80> #ServerAdmin [email  Protected] DocumentRoot "/svcroot/runtime/webstatic/shanhyweb" ServerName shanhyweb.example.com #ServerAlias www. shanhyweb.example.com errorlog "Logs/shanhyweb-error_log" Customlog "Logs/shanhyweb-access_log" Common <if                Module mod_jk.c> #日志输出文件 (other configurations can also override the configuration inside mod_jk.conf) Jklogfile Logs/mod_jk_shanhyweb.log #指URL指向如果有servlet, let Worker_web handle the jkmount/servlet/* worker_web #指URL为/*.jsp page Face, let Worker_web to deal with Jkmount/*.jsp worker_web #指URL为/*.do page, let Worker_web to deal with JKMO UNT/*.do worker_web #指URL为/*.json page, let Worker_web go handle jkmount/*.json worker_web </ Ifmodule><ifmodule Mod_rewrite.c>  Rewriteengine on# '-S ' (is regular file, with size) # '-L ' (is symbolic link) # '-d ' (is directory) # ' Ornext|or ' (OR Next condition) # ' nocase| NC ' (no case) # ' last| L ' (Last rule) # ' QSA ' append request string Rewritecond%{document_root}%{request_filename}!-s rewritecond%{document_root}%{request _filename}!-l rewritecond%{document_root}%{request_filename}!-frewriterule ^ (. *) \. (jpg|png|bmp) $ http://shanhyweb.example.com/createimage.jsp?path=%{request_uri} [nc,l]</ifmodule> < Directory "/svcroot/runtime/webstatic/shanhyweb" > Options followsymlinks allowoverride No NE Require all granted </Directory></VirtualHost>

About the processing of pictures, we recommend that you can get to know the next GraphicsMagick processing pictures.


===========================================

This way, you can also do other requirements, such as the static Web page: Index.do used to generate index.html, Apache judge if there is an HTML to directly respond to the browser, if it does not exist forward to index.do processing generation index.html


Note: This process of generating pictures, to the same image processing that piece of code to do a lock or synchronous processing, to prevent high concurrency, read and generate pictures because of the problem of resource usage.



------------------------------------

Finish





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Apache file does not exist jump URL (to achieve the specified size of the picture does not exist, the image is automatically generated)

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.