36

I have a strange problem here. I just moved from apache + mod_php to nginx + php-fpm. Everything went fine except this one problem.

I have a site, let's say example.com. When I access it like example.com?test=get_param $_SERVER['REQUEST_URI'] is /?test=get_param and there is a $_GET['test'] also.

But when I access example.com/ajax/search/?search=get_param $_SERVER['REQUEST_URI'] is /ajax/search/?search=get_param yet there is no $_GET['search'] (there is no $_GET array at all).

I'm using Kohana framework. which routes /ajax/search to controller, but I've put phpinfo() at index.php so I'm checking for $_GET variables before framework does anything (this means that disapearing get params aren't frameworks fault).

My nginx.conf is like this

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/*;
}

and example.conf is like this

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 is like this

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;

What is the problem here?

By the way there are few more sites on the same server, both Kohana based and plain php, that are working perfectly.

egis
  • 507
  • 1
  • 4
  • 8

3 Answers3

75

You are not passing get arguments in your try_files call

in the question:

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

Should be:

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

You can use either $query_string or $args, they are equivalent - except $query_string is readonly (or alternatively, $args can be updated by any other logic you may wish to add)

hobodave
  • 2,800
  • 2
  • 23
  • 33
AD7six
  • 2,810
  • 2
  • 20
  • 23
3
location / {
   try_files $uri $uri/ /index.php$is_args$args;
}
Smokie
  • 101
  • 1
  • 7
1

Try my rules:

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

Also delete duplicated "fastcgi_param QUERY_STRING" in fastcgi_params.

alvosu
  • 8,357
  • 24
  • 22
  • This doesn't work. On `ajax/search` it gives 500 error, on example.com it pops up download window and downloaded file is index.php – egis Feb 05 '11 at 16:49
  • 1
    Anyway, I've come up with an answer, `try_files $uri $uri/ index.php;` must be `try_files $uri $uri/ /index.php?$args; #or /index.php?$query_string, mind the '?'`, but it would be nice that someone explained this :) – egis Feb 05 '11 at 16:51
  • btw, error log for your config, alvosu, says `could not find named location @koh`. – egis Feb 05 '11 at 16:52
  • Sorry, I was made a mistake. I corrected "location". – alvosu Feb 05 '11 at 20:56