Nginx+redis implement JSP page cache, increase system throughput rate

Source: Internet
Author: User
Tags apache php nginx server redis server

Recently, in the development, found that the previous app client part of the page with WebView interaction, these pages request a lot, open a set of test paper, will produce 100+ request volume, resulting in system performance degradation. Consider doing a redis cache on the Nginx server closest to the client. Integrated the next web for the PHP cache data, after some changes, finally build success. Because of the internet is for PHP, and did not say, for me this completely non-operational operation of the people, the study is still very painful. So tidy up a more complete, for everyone's reference.

In the following configuration, there may be a problem that is not suitable or written. Please leave a message to indicate, thank you!

After the final cache, the entire project structure is shown below (picture copied, please auto brain Supplement memcache for redis,php for Tomcat):

Reference article Address:

1.srcache_nginx+redis building a cache system http://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/

2.httpsrcachemodule Wiki Http://wiki.nginx.org/HttpSRCacheModule#srcache_response_cache_control

1. Installing Nginx

First download the Nginx installation package, tar zvxf extracted to the/USR/LOCAL/SRC directory;

Download Modules Ngx_devel_kit, Set-misk-nginx-module, Srcache-nginx-module, Echo-nginx-module, Ngx-http-redis, Redis2-nginx-module;

Unzip these modules into the/usr/local/src/modules/below;

Enter the/usr/local/src/nginx-1.8.0/directory and execute the following command:

./configure--add-module=. /modules/echo-nginx-module-0.57--add-module=. /modules/ngx_http_redis-0.3.7  --add-module=. /modules/ngx_devel_kit-0.2.19--add-module=. /modules/set-misc-nginx-module-0.29  --add-module=. /modules/srcache-nginx-module-master--add-module=. /modules/redis2-nginx-module-master

then execute make;make install;

Installed to the/usr/local/nginx/directory by default, the installation is successful;

2. Configure Nginx

First declare the upstream in the HTTP body (this command does not study, only guess the next), the code is as follows:

Upstream redis{    server 127.0.0.1:6379;    KeepAlive 512;}

The server is a Redis server ip+port,keepalive is the number of connections maintained, the number of connections is online, for my project should be too big. You have to revise it yourself.

Configure location snooping in the server

location/test/{#这三个命令参考srcache document, Http://wiki.nginx.org/HttpSRCacheModule srcache_store_private on;    Srcache_methods GET;    Srcache_response_cache_control off;    #匹配自己的路径, because Nginx does not support nested if, so write if ($uri ~/test/index\.jsp$) {Set $flag "${flag}1";    } if ($arg _id ~ [0-9]+$) {set $flag "${flag}1"; if ($flag = "011") {#这里我用普通的请求参数来作为缓存的键值, online is MD5, but there are more actions to update the cache.        We adjust according to the business.        Set $key $arg _id;        Set_escape_uri $escaped _key $key;        #请求过来会先查询这个 Srcache_fetch Get/redis $key;        #过期时间 Srcache_default_expire 60;        Srcache_store put/redis2 key= $escaped _key&exptime= $srcache _expire;        #添加头信息 add_header x-cached-from $srcache _fetch_status;        Add_header X-cached-store $srcache _store_status;        Add_header X-key $key;        SET_MD5 $MD 5key $key;        Add_header X-md5-key $MD 5key;        Add_header x-query_string $query _string;    Add_header X-expire $srcache _expire; } #网上都是用fast_cgi to agent, did not get, with the original, looks like fast_cgi is Apache PHP under the Proxy_pass http://192.168.1.102:8080;}    #redis模块location =/redis {internal;    Set $redis _key $args; Redis_pass Redis;}     #redis2模块location =/redis2 {internal;    Set_unescape_uri $exptime $arg _exptime;     Set_unescape_uri $key $arg _key;    Redis2_query set $key $echo _request_body;    Redis2_query expire $key $exptime; Redis2_pass Redis;}

When you get here, the configuration is complete.

3. Operation effect

Do not cache state:

There is a cache status:

Response header Information

First time Request:

Request again:

The effect of the operation is obviously x-cached-from the change of the head information.

4. Unresolved issues

Do not understand why the use of Httpredis2 to introduce Httpredis, query the document after the result is said Redis2 is Httpredis upgrade version, but the wiki says:

Also, you need both Httpredismodule and httpredis2module. The former is used in the Srcache_fetch subrequest and the latter are used in the Srcache_store subrequest.

Which means all two of them.

In addition, for/redis this location, there is no redis get such code, how to get the return information. Originally only installed Httpredsi2, with Redis2_pass Redis does not work. Then add redis2_query get $redis _key, still not works. Helpless had to follow the document on the.

There are more familiar with this hero please note that the deployment of online environment before the optimization!! Thank you!!

Http://www.cnblogs.com/luochengqiuse/p/4677027.html

Nginx+redis implement JSP page cache, increase system throughput rate

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.