# Rewriting and proxy problem

**URL:** <https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005>\
**Category:** NGINX\
**Created:** [May 24, 2013, 7:40pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005 "2013-05-24T19:40:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [May 24, 2013, 7:40pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/1 "2013-05-24T19:40:47Z")

</div>

H folks!

I am completeley newbie to nginx

I have the following config

```
    # Forward request to /demo to tomcat. This is for
     # the BigBlueButton api demos.
   location /demo {
       rewrite ^ /upvc;
       proxy_pass http://127.0.0.1:8080;
       proxy_redirect default;
       proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;

```
    # Allow 30M uploaded presentation document.
       client_max_body_size 30m;
       client_body_buffer_size 128k;

       proxy_connect_timeout 90;
       proxy_send_timeout 90;
       proxy_read_timeout 90;

       proxy_buffer_size 4k;
       proxy_buffers 4 32k;
       proxy_busy_buffers_size 64k;
       proxy_temp_file_write_size 64k;

       include fastcgi_params;
   }

```

location /upvc {  
alias /var/lib/tomcat6/webapps/demo;  
index demo3.jsp;  
expires 1m;  
}

Rewrite is working but nginx is not. proxying to tomcat, because of  
that  
returns the jsp file as a plain text file.

Please could you help me?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![sbayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sbayona](https://www.ruby-forum.com/u/sbayona)\
**Post date:** [May 24, 2013, 9:07pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/2 "2013-05-24T21:07:14Z")

</div>

Let’s start by a huge RTFM?

> **[nginx documentation](http://nginx.org/en/docs/)**

This ML is no customer service for lazy people, I guess.  
You may up for services to make other people do your job:

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

## Best regards,

_B. R._

---

<div class="post-metadata">

**Author:** ![sbayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sbayona](https://www.ruby-forum.com/u/sbayona)\
**Post date:** [May 25, 2013, 11:27am UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/3 "2013-05-25T11:27:40Z")

</div>

On Fri, May 24, 2013 at 02:39:58PM -0300, Sergio B. wrote:

Hi there,

> I am completeley newbie to nginx

Welcome.

The nginx config follows its own logic, which may not match your  
previous  
experiences. When you understand that, you’ll have a much better chance  
of knowing the configuration you are looking for.

One important feature is that one request is handled in one  
location. Another is that one http request does not necessarily  
correspond  
to one nginx request.

In this case…

you make the request for /demoX, and the best-match location is  
“location  
/demo”, and so that is the one that is used.

> ```
> location /demo {
> rewrite ^ /upvc;
> 
> ```

Once that happens, you are using the new internal-to-nginx request  
“/upvc”, so a new choice for best-match location happens, and the rest  
of this location{} block is not relevant.

> ```
> proxy_pass http://127.0.0.1:8080;
> 
> ```

> ```
> include fastcgi_params;
> 
> ```

Aside: the fastcgi\_params file will typically have content relevant for  
when fastcgi\_pass is used, not for when proxy\_pass is used.

So, the http request for /demoX leads to the nginx request for /upvc,  
which matches this location:

> location /upvc {  
> alias /var/lib/tomcat6/webapps/demo;  
> index demo3.jsp;  
> expires 1m;

And here, you say “serve it from the filesystem”, so that’s what it  
does.

(I suspect that you actually get a http redirect to /upvc/, which then  
returns the content of /var/lib/tomcat6/webapps/demo/demo3.jsp. Using  
“curl” as the browser tends to make clear what is happening.)

> }

> Rewrite is working but nginx is not. proxying to tomcat, because of that  
> returns the jsp file as a plain text file.
> 
> Please could you help me?

The hardest part of nginx config that I find, it working out what  
exactly  
you want to have happen for each request.

From the above sample config, I’m not sure what it is that you want.

Perhaps putting the proxy\_pass in the “location /upvc” block will work?  
Or  
perhaps removing the rewrite?

If you can describe what behaviour you want, then possibly the nginx  
config to achieve it will become clear.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sbayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sbayona](https://www.ruby-forum.com/u/sbayona)\
**Post date:** [May 27, 2013, 4:45pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/4 "2013-05-27T16:45:16Z")

</div>

2013/5/27 Sergio B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > to one nginx request.  
> > “/upvc”, so a new choice for best-match location happens, and the rest  
> > which matches this location:  
> > “curl” as the browser tends to make clear what is happening.)  
> > you want to have happen for each request.
> 
> > –  
> > –  
> > –

Anyway it’s obvious that results in “not found” tomcat upvc directory  
does  
not exist,. Shame on me 😢

---

<div class="post-metadata">

**Author:** ![sbayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sbayona](https://www.ruby-forum.com/u/sbayona)\
**Post date:** [May 27, 2013, 4:32pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/5 "2013-05-27T16:32:24Z")

</div>

2013/5/25 Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> of knowing the configuration you are looking for.

Yup, I’ve began to read the documentation 🙂

> > ```
> > rewrite ^ /upvc;
> > 
> > ```
> 
> when fastcgi\_pass is used, not for when proxy\_pass is used.
> 
> (I suspect that you actually get a http redirect to /upvc/, which then  
> returns the content of /var/lib/tomcat6/webapps/demo/demo3.jsp. Using  
> “curl” as the browser tends to make clear what is happening.)

> }

> Rewrite is working but nginx is not. proxying to tomcat, because of that  
> returns the jsp file as a plain text file.
> 
> Please could you help me?

The hardest part of nginx config that I find, it working out what  
exactly

> you want to have happen for each request.
> 
> From the above sample config, I’m not sure what it is that you want.

> Perhaps putting the proxy\_pass in the “location /upvc” block will work? Or  
> perhaps removing the rewrite?

I did it, and tried using curl, tomcat complains that it cannot find  
/upvc.

> If you can describe what behaviour you want, then possibly the nginx  
> config to achieve it will become clear.

I’d want that when you type [http://example.com/upvc](http://example.com/upvc) proxies the  
/var/lib/tomcat6/webapps/demo/  
demo3.jsp file to tomcat

Thanks for your nice explanation

---

<div class="post-metadata">

**Author:** ![sbayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sbayona](https://www.ruby-forum.com/u/sbayona)\
**Post date:** [May 27, 2013, 9:17pm UTC](https://www.ruby-forum.com/t/rewriting-and-proxy-problem/235005/6 "2013-05-27T21:17:39Z")

</div>

On Mon, May 27, 2013 at 11:31:37AM -0300, Sergio B. wrote:

Hi there,

> I’d want that when you type [http://example.com/upvc](http://example.com/upvc) proxies the  
> /var/lib/tomcat6/webapps/demo/  
> demo3.jsp file to tomcat

Just for clarity, “proxy\_pass” proxies to a url, not to a file. So you  
probably want it to proxy to [http://127.0.0.1:8080/demo/demo3.jsp](http://127.0.0.1:8080/demo/demo3.jsp)

If I’ve got the mapping of url/filename wrong, adjust it as necessary.

You haven’t said what any _other_ requests to nginx should do, so I will  
show an example for only exactly what you said.

There is not an obvious mapping from /upvc to /demo/demo3.jsp, so I  
won’t include any kind of generic replacement here.

You will find very useful information on all of this at

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

So, in the appropriate server block, you want something like:

location = /upvc {  
rewrite ^ /demo/demo3.jsp break;  
proxy\_pass [http://127.0.0.1:8080](http://127.0.0.1:8080);  
}

This will handle requests for /upvc and /upvc?something. The  
documentation  
should explain why it works.

All of the other directives can be added back, when you know why they  
are needed here.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
