Nginx重写规则,用于由index.php解析的所有.PHP文件/请求

时间:2013-05-20 13:00:34

标签: php nginx rewrite

是否有任何魔术重写(就像在Apache上一样),nginx能够重写像'/submit.php'这样的URL,以便能够从index.php处理它们吗?我们有很多404未找到的错误,因为网站结构和所有以前的网址都像'/addrate.php','/ my_settings.php','/ profile.php' - >有50多个像这样的文件,为每个函数创建一个单独的.php文件是非常不专业和代码不明智的,而不是通过index.php解析所有这些文件,并像使用其他函数一样使用所需的类重写。

您能否找到解决方案/今天就此提出建议?

我认为有关此的一些信息在这里,但我想要完全相反的结果: http://www.nullis.net/weblog/2011/05/nginx-rewrite-remove-file-extension/

1 个答案:

答案 0 :(得分:1)

此配置允许您使用/var/www/example.org/htdocs/index.php

中的一个php脚本处理所有URL(文件系统中的非现有文件)
server {

    listen   80; ## listen for ipv4

    server_name  example.org www.example.org;
    root            /var/www/example.org/htdocs;
    access_log      /var/log/nginx/example.org.access.log;
    error_log       /var/log/nginx/example.org.error.log;

    location / {
            index  index.php index.html index.htm;
            try_files $uri $uri/ @php;
    }

    # enable running php files under php fastcgi
    location ~ \.php {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME /var/www/example.org/htdocs$fastcgi_script_name;
            #fastcgi_param  QUERY_STRING $uri;
            include fastcgi_params1;
    }

    location @php {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/example.org/htdocs/index.php; # this script catches all non existing URLs
            fastcgi_param  QUERY_STRING $uri;
            include fastcgi_params1;
    }
}
相关问题