<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi,<br>
On 18/08/2010 22:31, hendrik wrote:
<blockquote
cite="mid:46bcd5ba9a4300b2a7b2128dc5d10e6a.NginxMailingListEnglish@forum.nginx.org"
type="cite">
<pre wrap="">Hi everyone,
I am using nginx as a proxy for custom URLs. I call URLs like
/proxy/www.example.com/favicon.ico and nginx acts as a proxy for
<a class="moz-txt-link-abbreviated" href="http://www.example.com/favicon.ico">www.example.com/favicon.ico</a>. This works fine as long as the response
from the upstream server is not a 301. If it is a 301, this gets
forwarded to the client which then calls the target location directly.
Is there a way nginx can resolve these 301s and deliver the contents to
the client? You can find the config below.
</pre>
</blockquote>
I think you should be able to do this by compiling the headers-more
module (<a
href="http://github.com/agentzh/headers-more-nginx-module">http://github.com/agentzh/headers-more-nginx-module</a>)
and have a config like :<br>
<br>
proxy.conf<br>
--------------<br>
<pre wrap="">resolver         8.8.8.8;
proxy_pass <a class="moz-txt-link-freetext" href="http://$1">http://$1</a>;
more_set_headers -s '301 302 303' 'Location: /proxy/$upstream_http_location'; # you might want to add more 30x's here</pre>
<br>
nginx.conf<br>
-------------<br>
location ~ ^/proxy/<a class="moz-txt-link-freetext" href="http://(.*)$">http://(.*)$</a> {<br>
<span class="Apple-style-span" style="border-collapse: separate;
color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style:
normal; font-variant: normal; font-weight: normal; letter-spacing:
normal; line-height: normal; orphans: 2; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; font-size: medium;"><span
class="Apple-style-span" style="font-family:
helvetica,arial,freesans,clean,sans-serif; font-size: 13px;
line-height: 18px;"></span></span>
<pre wrap="">        include        proxy.conf;
</pre>
}<br>
<br>
location ~ ^/proxy/(.*)$ {<br>
<span class="Apple-style-span" style="border-collapse: separate;
color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style:
normal; font-variant: normal; font-weight: normal; letter-spacing:
normal; line-height: normal; orphans: 2; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; font-size: medium;"><span
class="Apple-style-span" style="font-family:
helvetica,arial,freesans,clean,sans-serif; font-size: 13px;
line-height: 18px;"></span></span>
<pre wrap="">        include        proxy.conf;
</pre>
}<br>
<br>
though I've not tested it. I think that $upstream_http_location
will be set after the response from the source server, but I'm not
certain.<br>
<br>
A few notes :<br>
<br>
- Having a proxy without some kind of security check is probably not
a good idea. You could do this with your proxy, but could also be
done with iptables.<br>
- There's probably not much point in trying to enforce 'domain/url'
in the regex. It will slow things down, and you don't really gain
anything. Same with case-insensitive regexes.<br>
- Nginx buffers responses until the request has been completed
before passing to the client. On small files, this shouldn't be a
problem, but on big ones it might. On continual audio/video streams,
it will block (as far as I know, and it did the last time I tested
it).<br>
- Depending on what you're using it for, you might be better off
using Apache Trafficserver as a high-performance forward proxy. The
performance is good, and I think it'll be able to do what you want,
but the config files are really ugly.<br>
<br>
Cheers,<br>
<br>
Marcus.<br>
</body>
</html>