Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to remove index.php? on NGINX?

October 05, 2008 6:11pm

Subscribe [3]
  • #1 / Oct 05, 2008 6:11pm

    MediaGirl Inc.'s avatar

    MediaGirl Inc.

    186 posts

    I’m needing help removing index.php? completely from the URL on a server running NGINX instead of Apache.

    I found this as it relates to removing it for a CI isntall.
    http://ellislab.com/forums/viewthread/90231/#470327

    Does anyone have how-to steps for an Expression Engine install?

    Thank you, Anna

  • #2 / Apr 04, 2009 9:53am

    ColinD

    7 posts

    Hi I don’t have the steps for a how to but I’m running nginx.
    I’ve posted my conf file for this vhost. It appears to run but I’m very new to expression engine so any feedback would be appreciated. This is also only for a single ee site.

    Force URL query is off. (was on with urls like index.php?/

    Once done, blank out the home page script name, in general conf. For me the links in templates where I’d used; {path='news/index'}, needed that to be blank to finally get no index.php in generated links.

    Nginx conf file, note I’m using vhosts and a params file.

    server {
      listen 80;
      server_name domain.co.uk domain2.com domain3.org;
      index index.html index.php;
      root /home/user/public_html;
     #Support non-www
     if ($host ~* www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*)$ <a href="http://domain.co.uk$1">http://domain.co.uk$1</a> permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
      }
    #handles the tld issue.
      if ($host != 'domain.co.uk' ) {
        rewrite  ^/(.*)$  <a href="http://domain.co.uk/$1">http://domain.co.uk/$1</a>  permanent;
      }
    
    #Maintenance mode from capistrano deploy
      if (-f $document_root/maintenance.html) {
        rewrite  ^(.*)$  /maintenance.html last;
        break;
      }
    
      # If the file exists as a static file serve it directly without
      # running all the other rewite tests on it
      # serve static files directly
      location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        access_log        off;
        expires           30d;
      }
      
      #If the file physically exists, serve it… ifs are bad in nginx though, there is a better way of doing this.
      if (-f $request_filename) {
        break;
      }
    
      #EE customised rule, skip system folder and images folder, by rights images and system should of been served by now.
    #Note the question mark still…
      if ($request_filename !~ (system|images|robots\.txt|index\.php.*) ) {
         rewrite ^/(.*)$ /index.php?/$1 last;
      }
    
      #Attempt to fix path_info, leaving the question mark works and covers testing domain.
      location /index.php {
        set  $script     $uri;
        set  $path_info  "";
    
        if ($uri ~ "^(.+\.php?)(/.+)") {
         set  $script     $1;
         set  $path_info  $2;
        }
      }
    
      #Finally mop up with anything left sent to the php-fpm engine.
    error_page 404 /index.php;
    
      location ~* .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /usr/local/nginx/conf/fastcgi_params;
        
        #Added for EE
        fastcgi_param PATH_INFO       $path_info;
      }
    
    }

    Path info setting from this post: http://forum.slicehost.com/comments.php?DiscussionID=2950

    I hope that helps you or anyone else get EE running on nginx. I’ll also be doing the same for litespeed, but suspect no issues arise there.

    Cheers,

    Colin

  • #3 / May 08, 2009 6:17pm

    Lost Cause's avatar

    Lost Cause

    9 posts

    And here’s how I do mine (simple example):

    server {
             listen  80;
             server_name  yourdomain.com;
             rewrite  ^/(.*) <a href="http://www.yourdomain.com/$1">http://www.yourdomain.com/$1</a> permanent;
           }
    
    server {
             listen  80;
             server_name  <a href="http://www.yourdomain.com">http://www.yourdomain.com</a>;
    
             access_log  /home/username/public_html/yourdomain.com/log/access.log;
             error_log  /home/username/public_html/yourdomain.com/log/error.log;
    
             root /home/username/public_html/yourdomain.com/public;
    
             gzip  on;
             gzip_http_version  1.1;
             gzip_vary  on;
             gzip_min_length  1000;
             gzip_buffers  16 8k;
             gzip_comp_level  1;
             gzip_proxied  any;
             gzip_disable  "MSIE [1-6]\.";
             gzip_types  text/plain text/css application/x-javascript text/javascript application/xml+rss application/json;
    
             # set 404 page to an EE error template page
             error_page  404  /site/404;
    
             location ~* \.(jpg|jpeg|gif|png|ico)$ {
                                                     # give missing images a simple plain 404 error page
                                                     error_page  404  /404.html;
                                                     access_log        off;
                                                     expires           30d;
                                                   }
    
             location / {
                          index  index.php index.html;
    
                          # send url to index.php and append the requested uri to the end unless the file/directory exists
                          if (!-e $request_filename){
                            rewrite ^/(.*)$ /index.php?/$1? last;
                          }
                          
                        }
                          
             # This location is for our EE index.php gateway
             location /index.php {
                                   include /usr/local/nginx/conf/fastcgi_params;
                                   set $script     $uri;
                                   set $path_info  $uri;
                                   # this will set the path_info when it exists as query string: /index.php?/something/here
                                   if ($args ~* "^(/.+)$") {
                                      set $path_info  $1;
                                   }
                                   fastcgi_pass 127.0.0.1:9000;
                                   fastcgi_index index.php;
                                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                                   fastcgi_param PATH_INFO $path_info;
                                 }
             
             # This will process any other php files as usual
             location ~* \.php$ {
                                  include /usr/local/nginx/conf/fastcgi_params;
                                  fastcgi_pass 127.0.0.1:9000;
                                  fastcgi_index index.php;
                                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                                  fastcgi_param PATH_INFO $fastcgi_script_name;
                                }
           }

ExpressionEngine News

#eecms, #events, #releases