1

This is driving me nuts and I dont understand how this works automatically. I have nginx as my web server and I have installed Joomla that has URL Rewriting which removes the index.php from the URL. Before, when I was using Apache, I have to enable the .htaccess with RewriteEngine On in order for this to work. But with Nginx, when I enable "Use URL Rewriting" it works automatically. I only use Nginx that passes the php files to php-fpm. Thats all. I havent added any special rewrite rule other than what was given in Joomla docs. I dont understand how 'Use URL Rewriting' just works automatically when I enable it since there is no .htaccess for Nginx.

The Joomla docs on this topic didint help neither. In second step it says

Enable the Use Apache mod_rewrite/URL rewriting option and Save: This option uses the Apache mod_rewrite function to eliminate the "index.php" portion of the URL.

.....

If this option causes errors, please see How to check if mod rewrite is enabled on your server. If it is not enabled and you have access to the file apache/conf/httpd.conf, open that file and check if the line LoadModule rewrite_module modules/mod_rewrite.so is uncommented. If necessary, uncomment the line and restart the Apache web server.

No idea why this is added in an Nginx configuration since there is no mod_rewrite in Nginx. The URL Rewriting in Joomla backend says this:

Use URL rewriting Select to use a server's rewrite engine to catch URLs that meet specific conditions and rewrite them as directed. Available for IIS 7 and Apache. Apache users only! Rename htaccess.txt to .htaccess before activating. Rename web.config.txt to web.config and install IIS URL Rewrite Module before activating.

It says nothing about Nginx but still it works. I am scratching my head here. Can someone tell me how the Joomla's index.php is removed so easily in Nginx? This is my Nginx Vhost configuration:

server {

        listen 80;
        server_name example.com;
        root /var/www/example/public_html;
        index  index.php index.html index.htm default.html default.htm;

        access_log /var/log/nginx/accn_access.log;
        error_log /var/log/nginx/accn_error.log;

        ##
        # JOOMLA SEF
        ##

        location / {
              try_files   $uri $uri/ /index.php?q=$request_uri;
        }

        ##
        # PHP scripts to FastCGI 
        ##
        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;

        }

}

See.. its a pretty straight forward configuration. Where is the magic happening?

Neel
  • 1,441
  • 7
  • 21
  • 35

1 Answers1

2

The magic happens here:

try_files $uri $uri/ /index.php?q=$request_uri;

This means that nginx checks first if the requested file or directory exists on the filesystem. If a file does not exist, it passes the request to Joomla, and passing the original URI to the q parameter.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • okay..that make sense. But how is the `index.php` removed from the url leaving just the query parameter? Is that completely controlled by Joomla and no need for special rewrite rule in nginx config? – Neel Nov 10 '14 at 20:21
  • `index.php` isn't really removed at all. The processing is passed to `index.php` (first part of URI) like a normal web request would be. The `index.php` then receives the `q` parameter. – Tero Kilkanen Nov 10 '14 at 20:26
  • aha.. so thats why it works straight away. Make sense now. It also seems a lot easier than what I used to do with Apache. Also the Joomla docs is wrong then.. thats what started confusing me more... Thank you @TiroKilkanen for explaining. :) – Neel Nov 10 '14 at 20:31