【问题标题】:Nginx Rewrite Rule for all .PHP files/requests to be parsed by index.php由 index.php 解析的所有 .PHP 文件/请求的 Nginx 重写规则
【发布时间】:2013-05-20 13:00:33
【问题描述】:

Nginx 是否有任何神奇的重写(就像在 Apache 上所做的那样)能够重写像 '/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/

【问题讨论】:

    标签: php nginx rewrite


    【解决方案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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2012-03-30
      • 1970-01-01
      相关资源
      最近更新 更多