8

I have a Nginx server running on 80 port working as a proxy to Apache 2.2 which is listening to 127.0.0.1:8080

When I access http://hostname/subfolder/ it works great.
When I access http://hostname/subfolder it redirects me to http://hostname:8080/subfolder/ which is wrong.

As far as I see the wrong redirect is returned by Apache but UseCanonicalName and UseCanonicalPhysicalProxy are both set to Off

Any Ideas on how to fix that?

2
  • The problem appears to be with your nginx configuration considering that your access to port 80 without a trailing slash results in a redirect to port 8080. UNLESS your nginx is proxy-ing to Apache and Apache is ordering a redirect.
    – PP.
    Aug 25, 2010 at 11:50
  • I use nginx as a proxy
    – quark
    Aug 25, 2010 at 14:07

5 Answers 5

5

I ran into this too, and I was able to fix it with a proxy_redirect directive right after my proxy_pass directive in my nginx config:

proxy_redirect http://example.com:8080/ http://example.com/ 

This is my full nginx config (In my case, Apache is on port 81 and hosting two sites. I added two site-specific proxy_redirect lines because I'm not sure how to add a single generic one.)

server {
    listen 80;

    access_log /var/log/nginx/apache-proxy.access.log;

    location / {
        proxy_pass http://localhost:81;

        #fix for apache redirects that include the port number
        proxy_redirect http://nfriedly.com:81/ http://nfriedly.com/;
        proxy_redirect http://misticflame.com:81/ http://misticflame.com/;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        client_max_body_size 10m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 6000;
        proxy_send_timeout 6000;
        proxy_read_timeout 6000;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        send_timeout 6000;
        proxy_buffering off;
        proxy_next_upstream error;

    }
}

Note: This was for a pre-1.0 version of nginx 5+ years ago. Here's the docs for proxy_redirect for the current version: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

3
  • adding proxy_redirect gives the syntax error
    – TomSawyer
    Dec 26, 2016 at 8:20
  • This was on a fairly old version of nginx. I'm no longer using nginx (just static content on github pages for my personal site), but I updated the answer with a link to the current proxy_redirect docs. Dec 26, 2016 at 12:21
  • By this way, you still get it slower because of the redirecting
    – TomSawyer
    Dec 28, 2016 at 15:46
3

If your ServerName directive on Apache is set to "hostname:8080", remove ":8080" or change it to "hostname:80". You could also add "proxy_set_header Host $host:80"

1
  • I had a similar issue with https URLs without trailing slash. This solution worked perfectly for me, although in case of https the ServerName directive has to be set to https://rootdomain:443. Apr 13, 2021 at 10:33
1

I had this issue long time ago.. As i remember it had to do with the HTTP RFC, slash at the end denotes a directory (/test/) , no slash at the end its a file (/test)

Long story short, add a rewrite rule that will add a trailing slash to the request, if there is none.

look at Solved:trailing slash issue with Nginx server

HTP

1

I ran into this exact problem recently. While the suggested solutions will work, Nginx offers a built-in solution:

proxy_redirect default;

Documentation at http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

Here's a full example of how to use it:

server {
  listen 80;
  location / {
    proxy_pass http://localhost:8080
    proxy_redirect default
  }
}
0

Perhaps nginx isn't setting the proxy headers to tell apache what the original request looked like.

In nginx:

proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

See http://wiki.nginx.org/LikeApache

Not the answer you're looking for? Browse other questions tagged or ask your own question.