nginx + php-fpm-我的$ _GET参数在哪里?


34

我这里有一个奇怪的问题。我只是从apache + mod_php移到了nginx + php-fpm。除了这个问题,一切都进行得很好。

我有一个网站,例如example.com。当我访问它时,就像example.com?test=get_param $_SERVER['REQUEST_URI']/?test=get_param,还有一个$_GET['test']

但是,当我访问example.com/ajax/search/?search=get_param $_SERVER['REQUEST_URI']/ajax/search/?search=get_param仍然没有$_GET['search'](有没有$_GET在所有的阵列)。

我正在使用Kohana框架。哪个路由/ajax/search到控制器,但是我已经投入了phpinfo()index.php所以我要$_GET在框架执行任何操作之前检查变量(这意味着消失的get参数不是框架的错)。

我的nginx.conf是这样的

worker_processes  4;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    index index.html index.php;
    autoindex on;
    autoindex_exact_size off;
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    error_log   logs/error.log   debug;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     off;

    keepalive_timeout  2;

    gzip  on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml     application/xml+rss text/javascript;


    include sites-enabled/*;
}

和example.conf是这样的

server {
  listen 80;
  server_name www.example.com;
  rewrite ^ $scheme://example.com$request_uri? permanent;
}




server {
    listen   80;
    server_name example.com;
    root /var/www/example/;

    location ~ /\. {
        return 404;
    }

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        /usr/local/nginx/conf/fastcgi_params;
    }

    location ~* ^/(modules|application|system) {
        return 403;
    }

        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
                access_log        off;
                expires           30d;
        }
}

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_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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;


fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;


fastcgi_param QUERY_STRING    $query_string;
fastcgi_param PATH_INFO       $fastcgi_path_info;

这里有什么问题?

顺便说一句,在同一台服务器上几乎没有其他站点,无论是基于Kohana的站点还是纯php,都可以正常运行。

Answers:


73

您没有在try_files调用中传递get参数

在问题中:

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

应该:

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

您可以使用$ query_string$ args,它们是等效的-除了$query_string是只读的(或者,$args可以通过您希望添加的任何其他逻辑来更新)


3
+1 Slim自己的文档是错误的
布罗迪

^同上; 花了几个小时试图将可怜的东西归类到另一个项目中。谢谢ad7six。
Rijndael 2014年

2
来自最新的Slim3文档:try_files $uri $uri/ /index.php$is_args$args;
mixdev

如果我们使用的是php-fpm而不是fastcgi怎么办?我们无法使用fastcgi.conf中的配置来检索变量名称“ $
query_string

2
location / {
   try_files $uri $uri/ /index.php$is_args$args;
}

1

尝试我的规则:

location / {
    try_files $uri $uri/ @koh;
}

location @koh {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/example/index.php;
    include        /usr/local/nginx/conf/fastcgi_params;
}

还要删除fastcgi_params中重复的“ fastcgi_param QUERY_STRING”。


这行不通。在ajax/search它给example.com上它会弹出下载窗口500错误,而下载的文件是index.php文件
EGIS

1
无论如何,我想出了一个答案,try_files $uri $uri/ index.php;必须是try_files $uri $uri/ /index.php?$args; #or /index.php?$query_string, mind the '?',但是有人解释了这将是一件很不错的事:)
egis

顺便说一句,您的配置的错误日志,alvosu说could not find named location @koh
egis

抱歉,我犯了一个错误。我更正了“位置”。
alvosu 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.