0

I am trying to run a PHP app using Nginx. Rewriting URLs work properly, however, query strings are not passed to the PHP files. Am I doing something wrong in the below configuration? I would appreciate any help!

nginx-site.conf :

server {
    root    /var/www/html;

    include /etc/nginx/default.d/.conf;

    index index.php index.html index.htm;

    client_max_body_size 30m;

    server_tokens  off;

    location / {
        index index.html index.htm index.php;
        try_files $uri $uri/ @extensionless-php;
    }

    location ~* .(?:ico|css|js|gif|jpe?g|png|svg|woff)$ {
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public";
    }

    location ~ \.php$ {
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    
    location @extensionless-php {
        if ( -f $document_root$uri.php ) {
            rewrite ^ $uri.php last;
        }
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi.conf;
    }

    location ~* /includes/(.+)\.php$ {
        deny all;
    }
}

2 Answers2

1

Instead of:

location / {
    index index.html index.htm index.php;
    try_files $uri $uri/ @extensionless-php;
}

I would use:

location / {
    index index.html index.htm index.php;
    try_files $uri $uri.php =404;
}

If query arguments don't work with this, try:

try_files $uri $uri.php$is_args$args =404;

The location @extensionless-php should also be removed.

Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
  • I tried as you suggested but it did not work but it actually got worse :) When I tried your first suggestion, I started to get 404. Then I tried your second suggestion and this time instead of routing to the URL, the browser is downloading the pages right now. – Gürkan Güran Jul 29 '20 at 20:32
0

Actually, that was my mistake. I saw that my solution is already working properly. All the query string is passed successfully.