Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I redirect/handle requests from search engines via nginx using location regex

I have developed an ajax-based web application with hash bang urls.

I am trying to redirect requests from search engines to another server which generates HTML snapshots and send the response. I am trying to achieve this in nginx with the location directive as mentioned below:

      location ~ ^(/?_escaped_fragment_=).*$ {
         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-Proto $scheme;

          client_max_body_size    10m;
          client_body_buffer_size 128k;
          proxy_connect_timeout   60s;
          proxy_send_timeout      90s;
          proxy_read_timeout      90s;
          proxy_buffering         off;
          proxy_temp_file_write_size 64k;
          proxy_pass      http://x1.x2.x3.x4:8080;
          proxy_redirect      off;
      }

But I am not able to get this working. Can someone correct the regex I am using (or) provide me an alternative solution to achieve this.

Thanks in advance.

like image 669
Ravi Teja Avatar asked Apr 24 '12 21:04

Ravi Teja


1 Answers

You cannot check for arguments in the location name (nginx docs):

Note that locations of all types test only a URI part of request line without arguments. This is done because arguments in the query string may be given in several ways, for example:

/index.php?user=john&page=1
/index.php?page=1&user=john

You could setup a rewrite rule in the server section (found here):

if ($args ~ “_escaped_fragment_=(.+)”) {
    set $real_url $1;
    rewrite ^ /crawler$real_url;
}

and define a "/crawler" location, which can be redirected to your HTML generating server.

like image 141
artkoenig Avatar answered Nov 15 '22 04:11

artkoenig