nginx rewrite with php-fpm

5,181

Make sure you've catched all other locations with nginx rules and add this to location /

    rewrite ^/(.*)/(.*)/$ /$1/$2.php last;
Share:
5,181
Anton Boritskiy
Author by

Anton Boritskiy

Updated on September 18, 2022

Comments

  • Anton Boritskiy
    Anton Boritskiy over 1 year

    I have debian server with nginx + php-fpm on board.

    nginx version: nginx/1.0.15
    
    PHP 5.3.10-1~dotdeb.1 with Suhosin-Patch (cli) (built: Feb  3 2012 00:21:57)
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
        with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator
        with Suhosin v0.9.33, Copyright (c) 2007-2012, by SektionEins GmbH
    

    this server is for facebook applications. server will host several applications, each application has a php file as an entry point, like history.php, collection.php etc.

    the question is how to change server config in order to process the URIs like this

    domain.com/facebook/history/
    

    processes like

    domain.com/facebook/history.php
    

    but browser url stays the same.

    here is my nginx config

    server {
        listen                  80;
        keepalive_timeout       70;
        server_name             domain.com;
        root                    /var/www/public;
        index                   index.php index.html;
    
        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }
        location ~* ^.+.(js|css|png|jpg|jpeg|gif|ico)$ {
                access_log        off;
                expires           1d;
        }
    
        location ~ .php$ {
                fastcgi_pass    unix:/var/run/php5-fpm.sock;
                fastcgi_index   index.php;
                fastcgi_param   SCRIPT_FILENAME  /var/www/public$fastcgi_script_name;
                include fastcgi_params;
        }
    
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
    
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
    
        ## There is not apache on server but still
        ## Disable viewing .htaccess & .htpassword
        location ~ /\.ht {
                deny  all;
        }
    }
    
  • Anton Boritskiy
    Anton Boritskiy about 12 years
    this is a straightforward solution, I appreciate your help, but I want to make a universal rule for all pages like that. Aiming no config changes in future.
  • Dmitry Verkhoturov
    Dmitry Verkhoturov about 12 years
    Check now, please.
  • Anton Boritskiy
    Anton Boritskiy about 12 years
    Well, it's almost ok! rewrite ^/(.*)/(.*)/$ /$1/$2.php last; leading slash is required
  • Anton Boritskiy
    Anton Boritskiy about 12 years
    there is a new feature buy now: it somehow conflicts with try_files... I hasn't understood the exact misbehavior by the moment...
  • Dmitry Verkhoturov
    Dmitry Verkhoturov about 12 years
    After answering to this question to yourself you can answer this question:) I'd suggest you to try to move try_files and rewrite around, like moving rewrite into location / or coping try_files outside.