# Rewrite rule for subrequest

**URL:** <https://www.ruby-forum.com/t/rewrite-rule-for-subrequest/188439>\
**Category:** NGINX\
**Created:** [May 12, 2010, 12:05am UTC](https://www.ruby-forum.com/t/rewrite-rule-for-subrequest/188439 "2010-05-12T00:05:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterjanovsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@peterjanovsky](https://www.ruby-forum.com/u/peterjanovsky)\
**Post date:** [May 12, 2010, 12:05am UTC](https://www.ruby-forum.com/t/rewrite-rule-for-subrequest/188439/1 "2010-05-12T00:05:23Z")

</div>

when implementing a rewrite rule for a subrequest which is the proper  
way to tackle the issue

1. write the rewrite rule in the main location (location /), the  
location where the subrequests are generated
2. write the rewrite rule in the subrequest location

i am using two subrequests to concatenate the document’s contents  
residing at the requested URL

example

main request

GET /path/to/files/document\_1.html

i generate two subrequests allowing me to chunk the response to the  
client as the document’s contents are large

subrequest1

echo\_subrequest\_async GET /sub1 -q $request\_filename;

- where request\_filename = /path/to/files/document\_1.html

subrequest2

echo\_subrequest\_async GET /sub2 -q $request\_filename;

- where  
request\_filename = /path/to/files/document\_1.html before rewrite  
rule  
and  
request\_filename = /path/to/files/document\_2.html after rewrite  
rule

here is the rewrite rule i’m using

rewrite ^(\_.\*)$ $1\_2.html break;

- so subrequest1 would go to /path/to/files/document\_1/html and  
subrequest2 would go to /path/to/files/document\_2.html

i’ve attempted putting the rewrite rule in the default location and also  
the subrequest2 location. on my backend i am able to trace the request  
is coming from the subrequest2 location but the URL passed through is  
the original URL and not my modified URL.

i know i must be missing something in the documentation, but i’m not  
sure what it may be. how do you use a modified version of the URL from  
subrequest1?

peter

Posted at Nginx Forum:

> **[rewrite rule for subrequest](https://forum.nginx.org/read.php?2,85547,85547#msg-85547)**
>
> when implementing a rewrite rule for a subrequest which is the proper way to tackle the issue 1. write the rewrite rule in the main location (location /), the location where the subrequests are generated 2. write the rewrite rule in the subrequest...

---

<div class="post-metadata">

**Author:** ![peterjanovsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@peterjanovsky](https://www.ruby-forum.com/u/peterjanovsky)\
**Post date:** [May 12, 2010, 12:11am UTC](https://www.ruby-forum.com/t/rewrite-rule-for-subrequest/188439/2 "2010-05-12T00:11:50Z")

</div>

clarification to above question

i know parameter “-q” passes query string parameters

what i’m looking to do is

1. create a variable within the conf
2. assign it request\_filename
3. parse the new variable using a regular expression or basic string  
manipulation

Posted at Nginx Forum:

> **[Re: rewrite rule for subrequest](https://forum.nginx.org/read.php?2,85547,85548#msg-85548)**
>
> when implementing a rewrite rule for a subrequest which is the proper way to tackle the issue 1. write the rewrite rule in the main location (location /), the location where the subrequests are generated 2. write the rewrite rule in the subrequest...

---

<div class="post-metadata">

**Author:** ![peterjanovsky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/47e85d/32.png) [@peterjanovsky](https://www.ruby-forum.com/u/peterjanovsky)\
**Post date:** [May 12, 2010, 3:41am UTC](https://www.ruby-forum.com/t/rewrite-rule-for-subrequest/188439/3 "2010-05-12T03:41:57Z")

</div>

On Wed, May 12, 2010 at 6:04 AM, peterjanovsky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> i’ve attempted putting the rewrite rule in the default location and also the subrequest2 location. on my backend i am able to trace the request is coming from the subrequest2 location but the URL passed through is the original URL and not my modified URL.

Could you try out the $echo\_request\_uri variable?

```
http://wiki.nginx.org/NginxHttpEchoModule#.24echo_request_uri

```

Hope this helps 🙂

-agentzh
