nginx + php-fpm。如何改写?

时间:2012-11-03 04:14:06

标签: nginx rewrite

我有以下部分主机配置文件。

location / {
    try_files           $uri /frontend.php;
}

location /backend/ {
    try_files           $uri /backend.php;
}

location ~ \.php$ {
    include             fastcgi_params;
    fastcgi_pass        127.0.0.1:9000;
    fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

如何仅使用nginx规则从$ _SERVER ['REQUEST_URI']中删除“/ backend”?

1 个答案:

答案 0 :(得分:0)

使用map指令创建一个新的请求uri变量,然后使用这个新变量设置fastcgi_param REQUEST_URI。经过测试并且有效。

### update REQUEST_URI
map $request_uri $new_request_uri {
  default $request_uri;               # for frontend request, still use $request_uri
  ~^/backend(?P<suffix>.*)$ $suffix;  # for backend request, only use the suffix after "/backend"
}

server {
  ...

  location ~ \.php$ {
    include        fastcgi_params;
    fastcgi_pass        127.0.0.1:9000;
    fastcgi_param       SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $new_request_uri;
  }
}