# Nginx add trailing slash with wrong port

**URL:** <https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974>\
**Category:** NGINX\
**Created:** [May 24, 2014, 8:54pm UTC](https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974 "2014-05-24T20:54:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [May 24, 2014, 8:54pm UTC](https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974/1 "2014-05-24T20:54:03Z")

</div>

nginx rewrites :

> [http://localhost:8081/files](http://localhost:8081/files)

to :

> [http://localhost:81/files/](http://localhost:81/files/) # wrong port !

while apache rewrites :

> [http://localhost:8082/files](http://localhost:8082/files)

to

> [http://localhost:8082/files/](http://localhost:8082/files/)

is it possible to configure nginx to behave like apache ?

/etc/nginx/conf.d/test.conf :

> server {  
> listen 81;  
> location /files {  
> alias /home/luc2/files;  
> autoindex on;  
> }  
> }

/etc/httpd/conf.d/test.conf :

> \<VirtualHost \*:82\>  
> Alias /files /home/luc2/files  
> \<Directory /home/luc2/files\>  
> Options indexes  
> Allow from all

Posted at Nginx Forum:

> **[nginx add trailing slash with wrong port](https://forum.nginx.org/read.php?2,250338,250338#msg-250338)**
>
> nginx rewrites : \> http://localhost:8081/files to : \> http://localhost:81/files/ # wrong port ! while apache rewrites : \> http://localhost:8082/files to \> http://localhost:8082/files/

---

<div class="post-metadata">

**Author:** ![luc2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@luc2](https://www.ruby-forum.com/u/luc2)\
**Post date:** [May 24, 2014, 10:28pm UTC](https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974/2 "2014-05-24T22:28:45Z")

</div>

A listen to port 81 should not invoke a response from 8081, check to see  
who  
is listening on 8081.

Posted at Nginx Forum:

> **[Re: nginx add trailing slash with wrong port](https://forum.nginx.org/read.php?2,250338,250339#msg-250339)**
>
> nginx rewrites : \> http://localhost:8081/files to : \> http://localhost:81/files/ # wrong port ! while apache rewrites : \> http://localhost:8082/files to \> http://localhost:8082/files/

---

<div class="post-metadata">

**Author:** ![luc2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@luc2](https://www.ruby-forum.com/u/luc2)\
**Post date:** [May 24, 2014, 10:36pm UTC](https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974/3 "2014-05-24T22:36:07Z")

</div>

On May 24, 2014 2:53 PM, “luc2” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > server {  
> > Alias /files /home/luc2/files  
> > \<Directory /home/luc2/files\>  
> > Options indexes  
> > Allow from all
> 
> Posted at Nginx Forum:  
> [http://forum.nginx.org/read.php?2,250338,250338#msg-250338](http://forum.nginx.org/read.php?2,250338,250338#msg-250338)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Aliases (and any other configurable sharing a name with something in  
Apache) generally don’t do the same thing as in Apache. Just use “root”  
inside the location block.

Dustin

---

<div class="post-metadata">

**Author:** ![luc2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/45deac/32.png) [@luc2](https://www.ruby-forum.com/u/luc2)\
**Post date:** [May 29, 2014, 1:01am UTC](https://www.ruby-forum.com/t/nginx-add-trailing-slash-with-wrong-port/229974/4 "2014-05-29T01:01:00Z")

</div>

On Sat, May 24, 2014 at 02:53:21PM -0400, luc2 wrote:

Hi there,

> nginx rewrites :
> 
> > [http://localhost:8081/files](http://localhost:8081/files)
> 
> to :
> 
> > [http://localhost:81/files/](http://localhost:81/files/) # wrong port !  
> >   
> > is it possible to configure nginx to behave like apache ?

No.

If your use case is restricted to one of the two mentioned below, then  
you might be able to fake it adequately.

nginx does not have a config option to do what you seem to want, which  
is “use the incoming http Host: header value in any generated Location:  
response header”.

Using “port\_in\_redirect”, you can auto-include either no port at all,  
or whichever port the connection actually came to nginx on (which will  
be one of the ports listed or implied in the “listen” directives).

If you don’t want to patch the code to add your use case, then

- if you have a fixed list of redirections, you could add a number of  
locations of the form

- or if there is exactly one host/port that you will always want to  
return  
(e.g. server:8081), then you could

But otherwise, I don’t think it can be done.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
