Welcome! Log In Create A New Profile

Advanced

Nginx as caching proxy for apache

Posted by andrewsuk 
Nginx as caching proxy for apache
April 08, 2011 10:20AM
Hi, I'm new to using Nginx and I am stumped, here is my problem;

I have setup Nginx as a reverse proxy for apache (debian) and everything is working fine, my server load has gone down and response times too.

I currently have Nginx serving my static content / images and passing anything else back to apache.

Now comes the problem:

Some of the content being generated via php/apache is very intensive and needs caching. The pages I need to be cached are being generated using apache rewrites and they are being mapped to .html

e.g. /all-pages-to-cache-are-like-this.html

and all none-html extension pages to be handled normally by apache.

However I have tied unsuccessfully to set this up, I tried using a regex / location block, resulting in errors due to proxy_ options not allowed in a location/regex block.

Also Apache will be sending no-cache headers along with the PHP content (as it should) which would cause Nginx proxy module to leave it uncached anyway..

so in short: I am trying to selectively (any url with .html extension) cache PHP generated content for a short period (5m)

Any help/advice on the issue would be much appreciated! :-D



Edited 1 time(s). Last edit at 04/08/2011 10:22AM by andrewsuk.
Re: Nginx as caching proxy for apache
April 14, 2011 04:55AM
Try the following in a server {} block

[code]
proxy_cache_path /data/nginx/cache/one levels=1:2 keys_zone=myCache:10m;

# this directive is inherited, you can also set it in the location part if you want to cache into different paths per location
proxy_cache myCache;

location ~ ^/cached_documents_regex\.html$ {
# cache this document for 48h when it returns a HTTP 200
proxy_cache_valid 200 48h;
proxy_pass http://yourApacheBackend;
}

location / {
# Pass all requests to your backend without caching
proxy_pass http://yourApacheBackend;
}
[/code]
Re: Nginx as caching proxy for apache
April 14, 2011 11:42AM
This results in the error I mentioned above, and is similar to what I have already tried:

Restarting nginx: [emerg]: "proxy_pass" may not have URI part in location given by regular expression, or inside named location, or inside the "if" statement, or inside the "limit_except" block in ...
Re: Nginx as caching proxy for apache
April 14, 2011 12:18PM
I eventually got the problem solved, Nginx didn't like a trailing slash when I was using a regex:

proxy_pass http://127.0.0.1:8090/; FAILED

proxy_pass http://127.0.0.1:8090; WORKED
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 153
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready