2

I have a server which is serving a simple application. When I navigate to the server's IP address http://XXX.XXX.X.X it displays the index.php file in the root directory of the application. When I click a link to go to any other page, it automatically redirects or adds a slash to the end of the page's URL. For instance, if a link's href is /about, the browser is taken to http://XXX.XXX.X.X/about/. If I manually try to go to http://XXX.XXX.X.X/about, I am also redirected to http://XXX.XXX.X.X/about/. I would like to get rid of the trailing slash. I am not sure where this slash is coming from.

I have tried adding this line to the server block of configuration as suggested here: http://www.nginxtips.com/nginx-remove-trailing-slash/. It did not work and I could no longer even access any page except the home page.

rewrite ^/(.*)/$ /$1 permanent;

I have looked at the Nginx access log in /var/log/nginx/access.log and it seems the requests are being redirected from what I am requesting (http://XXX.XXX.X.X/about for instance) to the same page with an added slash (http://XXX.XXX.X.X/about/) via 301. Here is the code from the log:

192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /why HTTP/1.1" 301 178 "http://192.168.1.7/services/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" 192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /why/ HTTP/1.1" 200 3086 "http://192.168.1.7/services/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" 192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /contact HTTP/1.1" 301 178 "http://192.168.1.7/why/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0" 192.168.1.2 - - [06/May/2015:14:53:20 -0500] "GET /contact/ HTTP/1.1" 200 2325 "http://192.168.1.7/why/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0"

But I am not sure why they are being redirected or what is causing it.

Details of the web application:

The web application is structured much like a static website, each page is a directory with a index.php file in it. The server's OS is Ubuntu server, Nginx is the server, and PHP5-fpm is installed. The Nginx configuration files are below.


nginx.conf

user  nginx;
worker_processes  1;

pid /run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

sites-enabled default

server {
        listen 80 default_server;

        root /var/www/html/;

        index index.php index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /var/www/html/;
        }

        location ^~ /files/  {
          root /var/www/html/;
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
}
JacksonHunt
  • 582
  • 6
  • 21
  • Not sure, but try removing `$uri/` from `try_files`. – Siguza May 05 '15 at 16:51
  • Thanks, but that does not work. I then get a 404 no matter what I try to access. – JacksonHunt May 05 '15 at 17:56
  • Oh... so, where is the actual routing done? When you request `.../about/`, what file does actually get served? Because apparently `$uri` does not match it, but `$uri/` does. – Siguza May 05 '15 at 18:42
  • You must be onto something, I have a feeling this is something really simple. If I visit .../about I am trying to request `/var/www/html/about/index.php`. If I go to `.../about` in my browser, I get served the correct file, but the URL is changed/rerouted to `.../about/`. I am quite new to this and am not really sure where the routing is done. I have tried playing with some of the PHP and Nginx configuration files but so far just ended up causing more problems and not affecting my actual problem. – JacksonHunt May 05 '15 at 21:02
  • Does `try_files $uri $uri/index.php $uri/index.html $uri/index.htm $uri/ =404;` work (in `location /`)? – Siguza May 06 '15 at 06:20
  • No, it does not. Then I am prompted to download the web page as a file by the browser. But hey, I think I discovered something. In the Nginx access log, it looks like it is 301 redirecting the requests. I cannot fit the information in this comment so I will add it to the question, please check it out. – JacksonHunt May 06 '15 at 20:10
  • And as a side note, changing to the `try_files` to `$uri/index.php` not only prompts me to download he file for viewing, but when I download the file I am given the PHP code, not the supposed-to-be client-side code. – JacksonHunt May 06 '15 at 20:49
  • So I found [this](http://stackoverflow.com/a/19753605/2302862)... according to it, you should remove the blocks `location = /50x.html` and `location ^~ /files/`, wanna give it a try? – Siguza May 07 '15 at 06:52
  • Hmmm, I do not think that is exactly my issue. the `location = /50x.html` is for the location of the 50x error page, which I think only matters if a 50x error code is returned, and `location ^~ /files/` is for serving static content, and that is working perfectly for JS, images, etc. I reworded the question [here](http://stackoverflow.com/questions/30088763/how-do-you-fix-nginx-automatically-301-redirecting-to-the-same-url-with-a-traili) to see if anyone else would understand and someone explained it very well, but I am still not sure how to get rid of that slash and avoid ".php" at the end. – JacksonHunt May 08 '15 at 11:14
  • You were right about it being the try_files directive! – JacksonHunt May 08 '15 at 11:15
  • Care to write a full answer? I'm quite curious what it really was now. ^^ – Siguza May 08 '15 at 11:16
  • I wish I could! kyl191 made it clear what is causing the situation, but I do not know what the solution is. I'm toying with it now, any ideas? – JacksonHunt May 08 '15 at 11:26

0 Answers0