首页
登录 | 注册

NGINX rewrite rules for PATH_INFO and SCRIPT_NAME

We use nginx as our web server on all our servers here at Em Space as it does a great job of serving web sites with very little overhead, essential on low memory virtual servers.

We have a stock virtual host file which serves Drupal sites very nicely (I'll post that sometime) but Josh needed to install a PHP Harvester PHP based OAI2 harvester application which uses the server variable PATH_INFO for its query handling. Our default Drupal based rewrite and fastcgi rules didn't work, so we set about figuring out how to fix them for this site.

This is what we came up with:

server {
    listen 80;
    server_name doaih.urbits.com;
 
    access_log	/home/swin/oaih/dev/logs/access.log;
    error_log /home/swin/oaih/dev/logs/error.log;
 
    root   /home/swin/oaih/dev/public;
    index  index.php index.html index.htm;
 
    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
        }
    }
 
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 
        include /etc/nginx/fastcgi_params;
    }
 
    location ~ \.php($|/) {
 
        set $script     $uri;
        set $path_info  "";
 
        if ($uri ~ "^(.+\.php)(/.+)") {
            set $script     $1;
            set $path_info  $2;
        }
 
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$script;
        fastcgi_param  SCRIPT_NAME        $script;
        fastcgi_param  PATH_INFO          $path_info;
 
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;
 
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
 
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx;
 
        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;
        fastcgi_param  REDIRECT_STATUS    200;
 
    }
 
}

The first location directive sets up the base options while the second location directive matches a request to index.php on its own and passes it to fastcgi to handle. This was needed as requests for the domain without index.php were not being handled correctly by the next block.

The key section in the whole config file for dealing with our problem is this:

    location ~ \.php($|/) {
 
        set $script     $uri;
        set $path_info  "";
 
        if ($uri ~ "^(.+\.php)(/.+)") {
            set $script     $1;
            set $path_info  $2;
        }
 
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME    $document_root$script;
        fastcgi_param  SCRIPT_NAME        $script;
        fastcgi_param  PATH_INFO          $path_info;
    }

This location directive matches any PHP requests not previously matched and sets the $script variable to the full uri. It also empties the $path_info variable. The if statement matches any requests like the following: http://mysite/index.php/foo and puts index.php into the $script variable and /foo into the $path_info variable.

$script and $path_info are inserted into the server variables by the fastcgi parameters SCRIPT_NAME and PATH_INFO and allow PHP to properly process the requests.


相关文章

  • 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在自己的机器上使用了,本配置文件摘录自 ...
  • 负载均衡使用nginx sticky实现session共享
    nginx sticky 模块工作流程图 下载nginx sticky 执行./configure的过程中可能会遇到以下错误: ./configure: error: the HTTP rewrite module requires the ...
  • ref: https://www.jb51.net/article/78746.htm 就普通的反向代理来讲 Nginx的配置还是比较简单的,如: 1 2 3 4 location ~ /* { proxy_pass http://127. ...
  • rewrite模块介绍 nginx的重写模块是一个简单的正则表达式匹配与一个虚拟堆叠机结合.依赖于PCRE库,因此需要安装pcre.根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可 ...
  • 1.安装pcre tar -zxvf pcre-7.8.tar.gz ./configure 2.安装Nginx tar -zxvf nginx-0.7.26.tar.gz ./configure --prefix=/usr/local/n ...
  • 最近准备设计和开发一套日志收集平台,进而后续进行实时的日志分析.业务监控和预警等.在此之前,需要制定日志的格式规范,当然还有其他的约束性规范,才能良好的实现日志搜集.数据分拣.数据分析等特性.       制定日志格式规范的方式与目的:   ...

2020 unjeep.com webmaster#unjeep.com
12 q. 0.013 s.
京ICP备10005923号