0

I'm running nginx-1.14.0 with php-fpm7.2. When I access php files that are resident in the root, they are properly sent through fastcgi and their php gets properly evaluated. However, when I access php files -- even with the same contents -- from within secodary site directories, they are sent back as BIN data and not sent through fastcgi to be evaluated as php.

Here are the pertinent items in my nginx config file ...

server {

    ... etc. ...

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

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ ^/(fm)(/?$|/.+\.php) {
        alias /usr/share/nginx/$1$2;
        # I get the same results with "alias $1$2;"
        include phpsite_params;
    }

    location ~ ^/(css|static|img|js)($|/.*) {
        alias $1$2;
    }

    location ~ \.php$ {
        include phpsite_params;
    }
}

The /usr/share/nginx/html/index.php file gets properly passed through fastcgi and evaluated as php when I send this URL: http://example.com.

However, the /usr/share/nginx/fm/index.php file just gets sent back to me as plain text without the php being evaluated when I send this URL: http://example.com/fm, and also this URL: http://example.com/fm/, and also this URL: http://example.com/fm/index.php.

Here are the contents of phpsite_params ...

fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 600;
include fastcgi_params;

... and here are the contents of 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;

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

What am I doing wrong in my configuration?

Thank you in advance for any help or suggestions.

UPDATE: I added the following section to my nginx configuration file, and it works properly. But this location doesn't use PHP, and therefore, it seems to indicate that the error is specifically related to fastcgi.

    location ~ ^/(junk)($|/.*) {
        alias $1$2;
        autoindex on;
    }

Can anyone think of anything within fastcgi or php-fpm which could cause the problem that I'm having?

HippoMan
  • 222
  • 1
  • 10
  • Do you have a `location` for statics under the `/fm` folder? – Richard Smith Sep 29 '18 at 08:42
  • No, I don't. That folder contails `index.php`, `readme.txt`, and a few subdirectories, none of which are named `statics`. And I have no `location` directive in the nginx config file which points to any `statics`. Is there something incorrect about the way I have the `fm` folder set up, or about the lack of a statics `location`? – HippoMan Sep 29 '18 at 13:34

1 Answers1

0

I got it working through lots of trial and error. I'm not sure why I couldn't get the original to work, but the following works properly and returns the same result for all three of these url's ... http://example.com/fm, http://example.com/fm/, and http://example.com/fm/index.php ...

rewrite ^/(fm)$ /$1/ permanent;

location ~* ^/(fm)/(.*)$ {
    index index.php;
    alias /usr/share/nginx/$1/$2;
    try_files "" /$1/index.php;
    include phpsite_params;
}

I needed the rewrite, because I couldn't figure out how to get this to work for http://example.com/fm if the location pattern was this: ^/(fm)(/?.*)$

The /fm/ site has subdirectiores such as icon, and so relative url's such as icon/icon1.jpg have to work, in addition to the /fm/index.php url.

If anyone can suggest a way to make all of this work without the rewrite, I will be grateful.

HippoMan
  • 222
  • 1
  • 10