如何使用nginx获取url参数?

时间:2016-03-01 14:52:13

标签: php nginx

我已经编写了一个应用程序,在apache上,当我在php中回显时,获取url.com/home/index/test的参数并返回/home/index/test没有问题。在我转到url.com/?something=test时,在nginx上返回test

然而,当我在nginx上放置相同的应用程序时,它只返回一个空白页面。网络标签显示200,因此它不会为我返回任何错误。这是我的nginx块。我认为这与代码无关。我认为它是服务器端。

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /var/www/c/public;
index index.php index.html index.htm;

server_name server_domain_or_IP;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

编辑:添加php代码

这是我处理网址的应用文件

<?php

class App
{
protected $controller = 'home';
protected $method     = 'index';
protected $params     = [];

public function __construct()
{
   return print_r($this->parseUrl());
}

public function parseUrl()
{
    if(isset($_GET['url']))
    {
        return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
    }
}

}

这是控制器

<?php
class Home extends Controller
{
    public function index()
    {
        echo 'home/index';
    }
}

您可以在此处查看完整的回购https://github.com/CircleFramework/Circle/tree/0.1-dev

这是我的fastcgi_params

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   SCRIPT_FILENAME         $request_filename;
fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
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/$nginx_version;
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   HTTPS                   $https if_not_empty;
fastcgi_param   REDIRECT_STATUS         200;

1 个答案:

答案 0 :(得分:0)

更新nginx后,我的网站上出现了同样的错误。 替换

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

fastcgi_split_path_info上的某些内容触发了此错误。