when accessing: http://example.com/somefolder --> http://example.com:8080/somefolder

I have tried this:

 http {
    port_in_redirect off;

Any ideas?

  • Can you provide your configuration? Are you proxying to another service behind nginx? – Shane Madden Jan 18 '12 at 20:46
  • I believe we are are proxying to apache. Which configuration files do you want? – Chris Muench Jan 18 '12 at 21:37
  • 3
    The nginx configuration file that defines the server block which contains the proxy_pass directive. What you're probably going to need will be a proxy_redirect directive - seeing your config should confirm. – Shane Madden Jan 18 '12 at 22:22
  • Note that some browsers cache redirects so when changing port_in_redirect from on to off you may need to restart it. – emersion Apr 11 '16 at 21:30
  • @emersion port_in_redirect doesn't work at all – TomSawyer Dec 26 '16 at 8:08

10 Answers 10

I just ran into the same problem and port_in_redirect off; actually worked for me, just make sure you use it inside the server {} block.

server {
  listen 8080;
  server_name example.com;

  port_in_redirect off;
  autoindex on;

  location / {
    root /var/www/example.com;
    index index.html;
  }
}
  • Or even use port_in_redirect inside location. – wqw May 19 '16 at 12:49
  • worked perfectly for hosting nginx on 8080 behind a load balancer. thank you – xxstevenxo Dec 17 '17 at 21:47

This should fix problem. Add proxy_redirect directive right after proxy_pass directive

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

If someone is still experiencing this problem while having apache behind nginx reverse proxy setup, you could try the ff:

  location / {
    proxy_redirect off;
    proxy_set_header Host $host:$server_port; # <-- this one solved mine
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8083;
  }
  

My setup is that I let Apache listen to 127.0.0.1:8083 and let nginx proxy requests to it.

I had the same problem with my nginx + Apache setup. Apache seems to be redirecting to it's own port (running on 8080), while nginx is on port 80.

In my setup, this made infinite redirect loop for normal urls:

proxy_set_header Host $host:80; # Force port 80

Instead bind the returning data to port 80, like this:

proxy_bind $host:80; # Bind to port 80

Here is my nginx server block:

server {
    listen 80;
    listen [::]:80 ipv6only=on;

    server_name _; # Wildcard server

    location / {
        proxy_bind $host:80; # Bind to port 80 << THIS IS THE MAGIC
        proxy_pass http://localhost:8080;
        proxy_set_header Host            $host; # Pass host header
        proxy_set_header X-Real-IP       $remote_addr; # Preserve client IP
        proxy_set_header X-Forwarded-For $remote_addr;
    }
}

With this wildcard setup, all requests nginx does not have a server block for is passed on to Apache.

  • Doesn't work at all – TomSawyer Dec 26 '16 at 8:44

In case anyone else was having this issue - the article here: http://www.linuxquestions.org/questions/linux-server-73/strange-nginx-redirects-without-trailing-slash-930876/ solved the problem for me.

I had to add an extra header in the proxy.

Make sure you have the Host set to $http_host and also that you have set the X-Forwarded-Host to $http_host like below:

proxy_redirect off;
proxy_set_header Host $http_host;             # <-- make sure this is $http_host
proxy_set_header X-Forwarded-Host $http_host; # <-- make sure you set this
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 32 8k;
proxy_busy_buffers_size 64k;
  • It doesn't work at all – TomSawyer Dec 26 '16 at 8:17

I think this simple example is enough:

location = /somefolder {
    return 302 http://$host:8080/somefolder;
}

http://wiki.codemongers.com/NginxHttpCoreModule#port_in_redirect

syntax: port_in_redirect [ on|off ]
default: port_in_redirect on
context: http, server, location

Hope this is what you are looking for.

  • I have tried this in the http context with no luck – Chris Muench Jan 18 '12 at 20:18

I had a similar problem.

Maybe you should use directive proxy_set_header to set the headers' host and location. In my configuration I added proxy_set_header Location $host:80; and all the requests return port 80 now.

  • I believe this will break redirects beyond "/" - every redirect would point to $host:80 – perrierism Aug 24 '15 at 17:48

This issue is caused by Nginx auto translate relative path to absolute path, right?
There is a way to tell Nginx not translate, you add a space before slash: ngx.redirect(" /foo")

  • Please note that this is a question from 3 years ago. – Deer Hunter Apr 9 '15 at 1:07

I would recommend instead adding this to your server block:

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

This was when you go to example.com/wp-admin it will go to example.com/wp-admin rather than going to example.com:8080/wp-admin/ and if you are on CloudFlare DNS for instance it will say the site is down.

I'm using Ubuntu, Nginx and Varnish and this works perfectly now rather than disabling port_in_redirect.

To add, be sure to to restart nginx after:

sudo service nginx restart

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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