使用 php-fpm 重写 nginx

使用 php-fpm 重写 nginx

我有一台搭载有 nginx + php-fpm 的 Debian 服务器。

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

该服务器用于 Facebook 应用程序。服务器将托管多个应用程序,每个应用程序都有一个 php 文件作为入口点,例如 history.php、collection.php 等。

问题是如何更改服务器配置以便处理这样的 URI

domain.com/facebook/history/

类似这样的过程

domain.com/facebook/history.php

但浏览器网址保持不变。

这是我的 nginx 配置

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

答案1

确保你已经用 nginx 规则捕获了所有其他位置,并将其添加到location /

    rewrite ^/(.*)/(.*)/$ /$1/$2.php last;

相关内容