Question

I have a very annoying problem, i use nginx to proxy a apache server(http://internalip.com:18080), the config is like this:

location /svn {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://internalip.com:18080;
}

It is ok most-timely, but sometimes nginx just redirect user to internal address, so the user will be prompt error. I don't know what's wrong, it just is being happening. The nginx version is 1.4.4-4~precise0. Could anybody know this? Thanks in advance!

Was it helpful?

Solution

I have found out the problem.
The key point is the Apache DirectorySlash, if I visit https://outipaddress.com/theurl, apache will redirect to http://internalip.com:18080/theurl/ even if the X-Forword-* headers are set. I think it is a bug of apache httpd.
The workaround is to perform the redirect on nginx side.

`location /svn/ {
    if ($request_uri ~ "/[a-zA-Z0-9-_]+$") {
        rewrite ^ https://$server_name$request_uri/;
    }
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://internalip.com:18080;
}`

Now nginx will redirect all urls that are not ended with slash and seem like a directory(contain only symbol characters).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top