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;
}
}