0

I'm rather new to Nginx, but got it working w/ PHP5-FPM ... more or less.

What I want to do: Have Nginx call the PHP5-FPM parser for every PHP file located above the /var/www/example.com/ directory on the server. This sort of works, with one major flaw.

The http://example.com or https://example.com (with or without using i.e. index.php or test.php), the file just downloads as plain code. If I fiddle with the config a bit, I can get it to parse as plain text/html.

So it seems the PHP5-FPM parser isn't called for the url http(s)://example.com at all, while it seems to work as intended with http(s)://www.example.com

The issue seems rather nonsensical to me, but maybe there's something really obvious I've just missed - or that the NGinx PHP regexp is botched.

So, any tips/hints/recommendations would be much appreciated.

upstream php {
  server unix:/var/run/example.com.socket;
}

server {
    # none-SSL php/fastcgi
    listen       80;
    server_name  example.com www.example.com;
    access_log   /var/log/nginx/example.com.access.log;
    root         /var/www/example.com/;
    index index.php;

    location ~ \.php$ {
      fastcgi_pass   php; #127.0.0.1:1025;
    }

    location / {
       try_files $uri $uri/ /index.php?$args;
    }
    return 301  https://www.example.com$request_uri; # Redirect to SSL
}

server {
    # SSL php/fastcgi
    server_name  example.com www.example.com;
    root         /var/www/example.com/;
    listen 443 ssl default_server;
    index index.php;

    ssl    on; # Force SSL

    ssl_certificate        /etc/ssl/example.com/fullchain.pem; # cert.pem + chain.pem bundle
    ssl_certificate_key    /etc/ssl/example.com/privkey.pem;

    access_log  /var/log/nginx/example.com.log;
    error_log /var/log/nginx/example.com.error.log;

    location ~ \.php$ {
        fastcgi_pass php;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param   HTTPS              on;
        fastcgi_param   HTTP_SCHEME        https;
    }


    location / {
       try_files $uri $uri/ /index.php?$args;
    }
}
3
  • I've had a fairly good look at this and nothing jumps out at me. Try removing the line with "SCRIPT_NAME" on it, I think it's redundant because of the line above. I don't use the fastcgi_param HTTP or HTTP_SCHEME arguments either, remove them for debugging. Your forwarding setup is also non-optimal, grab my sample Nginx files from the link below and set up your servers to forward to a single URL - example.com for example. photographerstechsupport.com/tutorials/…
    – Tim
    Dec 22, 2016 at 19:36
  • Yeah, at this point the config files are far from optimal as I've been trying literally everything to make the PHP parsing work properly. I'm actually worried that this may happen anywhere on the web server "randomly" and that it may cause serious security issues later down the line... I removed the 3 lines for debugging and the result remains the same. I find it really peculiar that www . example . com loads and parses index.php correclty while example . com doesn't ... And I'll definitely have a look at your sample .conf files tomorrow and even try them out, see if it helps.
    – user391796
    Dec 23, 2016 at 1:41
  • Actually I think I've (at the very end of the day) found the culprit - it seems an additional example.com-ssl.conf file had been added under the nginx/conf.d directory. I can't say how/when this has happened, but it looks like bot work (i.e. the letsencrypt bot) from a previous installation. I removed the entry there, and the example.com/index.php no longer downloads and actually parses properly :)
    – user391796
    Dec 23, 2016 at 2:06

0

You must log in to answer this question.

Browse other questions tagged .