1

nginx 是否能够重写像“/submit.php”这样的 URL 以便能够从 index.php 处理它们?由于站点结构,我们有很多 404 not found 错误,并且所有以前的 URL 都像 '/addrate.php'、'/my_settings.php'、'/profile.php' --> 有超过 50 个这样的文件和为这些函数中的每一个创建一个单独的 .php 文件,而不是通过 index.php 解析它们并使用所需的类,就像我们对其他重写所做的那样,这将是非常不专业和代码不明智的。

您今天能找到解决方案/给我们一个建议吗?

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

4

1 回答 1

1

此配置允许您使用一个 php 脚本处理所有 URL(文件系统中不存在的文件)/var/www/example.org/htdocs/index.php

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;
    }
}
于 2013-05-20T13:13:19.353 回答