Nginx forward slash issues

I've googled a lot and found a few workarounds, but you have to define each directory.

  • In Apache: example.com/hi -> example.com/hi/
  • In nginx: example.com/hi -> Firefox cannot connect to server at example.com:8888

where 8888 is what Apache is listening on (nginx: 80 -> localhost: 8888)

Any ideas how to fix this and make it just forward normally as a folder?

+2


a source to share


4 answers


I had a similar problem with varnish and nginx (varnish on the 80th proxy to listen for nginx on 8080) and needed to add "port_in_redirect off"; ... server_name_in_redirect to stay, so nginx knew which host it was handling.



+2


a source


The following should do the trick, but it needs more thinking / working because only one location block will be used at a time:

location ~ ^(.*[^/])$ {
  if (-d $document_root/$1) {
    rewrite ^(.*)$ $1/ permanent;
  }
}

      



(not verified)

+1


a source


You can set "server_name_in_redirect off" in your server section

server{
  listen 80 default;
  server_name localhost;
  server_name_in_redirect off;

 ...
 ...
}

      

This will do the trick; -)

NTN.

Edit: just a format.

+1


a source


This is the magic that works best for me:

try_files $uri $uri/ @redirect;
location @redirect {
    if ($uri !~ '/$') {
        return 301 $uri/$is_args$args;
    }
}

      

The if statement can be safe: http://wiki.nginx.org/IfIsEvil

0


a source







All Articles