Welcome! Log In Create A New Profile

Advanced

Caching a location based on user-agent

Posted by opechancano 
Caching a location based on user-agent
February 06, 2013 10:53AM
Hi All:

How can I caching a location/files just for specific user-agents?

I mean something like:


if ($http_user_agent ~ (googlebot|bingbot|badbot) ) {

location ~* \.(html)$ {

proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_ignore_headers Set-Cookie Cache-Control Expires;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_cache_valid 200 304 30m;
proxy_cache_key $host$uri$is_args$args;
expires 336h;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 120;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_pass http://myapachecluster;
}


}


But it seems that it is not possible to use proxy_* directives with this structure (if/location)

Any ideas?

Thank you very much.

Regards,



Edited 1 time(s). Last edit at 02/06/2013 10:55AM by opechancano.
Re: Caching a location based on user-agent
February 12, 2013 04:15AM
Hi!

Update. I have solved as follows:

if ( $http_user_agent !~* (Wget|Googlebot|bingbot|YandexBot|msnbot) ) {
set $not_bot "1";
}

location ~* \.(html)$ {

proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_ignore_headers Set-Cookie Cache-Control Expires;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_cache_bypass $not_bot; #skip caching
proxy_no_cache $not_bot; #skip caching

proxy_cache_valid 200 304 30m;
proxy_cache_key $host$uri$is_args$args;
expires 336h;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 120;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $http_host;
proxy_pass http://myapachecluster;
}

I hope it helps someone ;-)

Thank you.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 94
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