Nginx – PHP Not working through Symlinks (CentOS, Nginx, PHP-FPM, PHP7)

centosnginxPHPphp-fpm

When I use this config, PHP files work in the base folder (/var/www/dmcblue.com/www). But I added a symlink there as data from /usr/share/phpMyAdmin. Files are accessible (like http://[IP_ADDRESS]/data/themes/dot.gif) but PHP files just return 404.

# /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  [IP_ADDRESS];

    # note that these lines are originally from the "location /" block
    #root   /var/www/dmcblue.com/www;
    #index index.php index.html index.htm;

    location / {
        root /var/www/dmcblue.com/www/;
        try_files $uri $uri/ =404;
        index index.php index.html index.htm;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        include fastcgi_params;
    }
}

The weirder part is that if I add the following nonsense, it works:

location /data/ {
        root /var/www/dmcblue.com/www/;
        try_files $uri $uri/ =404;
        index index.php index.html index.htm;

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            include fastcgi_params;
        }
    }

It doesn't make sense and I'm not sure how I arrived at that.

All the files are part of the nginx group, including the symlink.

Basically, I just want to use symlinks to create subfolders and its not working for PHP files. How do I do that correctly?

Best Answer

I still don't understand why but I adjusted a config from DigitalOcean and it seems to work. If anyone can explain why the original wasn't working, I'd appreciate it greatly.

server {
        listen 80;
        listen [::]:80 ipv6only=on;
        root /var/www/dmcblue.com/www;
        index index.php index.html index.htm;
        server_name [IP_ADDRESS];
        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
        }
        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
      }