Rewrite -- failure

Francis Daly francis at
Tue Apr 14 22:39:39 UTC 2020

On Tue, Apr 14, 2020 at 04:38:51PM -0400, Paul wrote:

Hi there,

> My problem is that I need to split to two physical servers
> (both fully functional on LAN). The first ( serving static
> https works fine. But I cannot "rewrite" (redirect, re-proxy?) to the second
> server (, Perl cgi) where the request comes in as
> and I need to get rid of "foo" -- proxy_pass can (probably) do what
you want, without rewrites. The documentation phrase to look for is
"specified with a URI".

> 	"rewrite ^(.*serv1\.example\.com\/)foo\/(.*) $1$2 permanent;" (tried
> permanent, break, last and no flags)

"rewrite" ( works on the "/foo" part, not the
"https://" or the "" parts of the request, which is why
that won't match your requests.

>     location /foo {           # big db server, perfect on LAN, PERL, cgi
>         # rewrite ^/foo(.*) /$1 break;   #tried permanent, break, last and
> no flags

That one looks to me to be most likely to work; but you probably need
to be very clear about what you mean when you think "it doesn't work".

In general - show the request, show the response, and describe the response
that you want instead.

>         # rewrite ^/foo/(.*)$ /$1 last;   #tried permanent, break, last and
> no flags
>         rewrite ^(.*serv1\.example\.com\/)foo\/(.*) $1$2 permanent; #tried
> permanent, break, last and no flags
>         proxy_pass;
>         proxy_set_header Host $host;
>         proxy_http_version 1.1;
>         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
>    }

I suggest trying

    location /foo/ {

(note the trailing / in both places) and then seeing what else needs to
be added.

Note also that, in any case, if you request /foo/one.cgi which is really
upstream's /one.cgi, and the response body includes a link to /two.png,
then the browser will look for /two.png not /foo/two.png, which will
be sought on the other server. That may or may not be what you want,
depending on how you have set things up.

That is: it is in general non-trivial to reverse-proxy a service at a
different places in the url hierarchy from where the service believes
it is located. Sometimes a different approach is simplest.

> server {
> # automatically sets to https if someone comes in on http
>     listen 80;
>     listen 8084;

Hmm. Is this 8084 the same as above? If so, things
might get a bit confused.

Good luck with it,

Francis Daly        francis at

More information about the nginx mailing list