3

I'm using Laravel 5.2 and Nginx and works fine on my development server.

For example:

http://example.com/results?page=2

Development Server

LengthAwarePaginator::resolveCurrentPage(); // returns 2
Paginator::resolveCurrentPage(); // returns 2
$request->input('page'); // returns 2

But in production server

LengthAwarePaginator::resolveCurrentPage(); // returns 1
Paginator::resolveCurrentPage(); // returns 1
$request->input('page'); // returns null

Production Server Configuration

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

        root /usr/share/nginx/html/laravel/public;
        index  index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name example.com www.example.com;

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

        location @rewrite {
            rewrite ^(.*)$ /index.php?_url=$1;
        }

        location ~ \.php$ {
                try_files $uri $uri/ /index.php$is_args$args;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                include /etc/nginx/fastcgi_params;
        }
}

Development Server Configuration

server {
    listen 80;
    server_name example.com;
    client_max_body_size 50M;
    root /var/www/html/laravel/public;

    index       index.html index.htm index.php;

    try_files $uri $uri/ @rewrite;

    add_header 'Access-Control-Allow-Origin' '*';

    location @rewrite {
        rewrite ^(.*)$ /index.php?_url=$1;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php5.6-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  PHP_VALUE "upload_max_filesize=52428800 \n post_max_size=53477376 \n memory_limit=536870912";
        fastcgi_param  APPLICATION_ENV development;
        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include        fastcgi_params;
    }
}

What changes should I make in server nginx configuration? Any help appreciated!

1 Answer 1

6

Resolved!

I needed to make some corrections as there were two fastcgi_pass in location ~ \.php$ {} so had to change

location ~ \.php$ {
    try_files $uri $uri/ /index.php$is_args$args;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include /etc/nginx/fastcgi_params;
}

To

location ~ \.php$ {
    try_files $uri $uri/ /index.php$is_args$args;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    include /etc/nginx/fastcgi_params;
}

and modify try_files in location / {} hence had to change

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

To

location / {
    try_files $uri $uri/ /index.php?$query_string;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. By using try_files $uri $uri/ /index.php?$query_string; in the location / block solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.