2
votes

I have two directories, /home/php/www/public/ and /home/php/www/private/ and I would like to serve which one depending on the first part of the uri.

I would like /foo/ to act 'normally' in that it should serve which ever file is in the location, for example mysite.com/about would serve /public/about.php. However /private/ would always serve a single file no matter the request, for example mysite.com/private/foo, mysite.com/private/bar and mysite.com/private/foo/test would all serve /private/app.php.

I am probably in the 100s of different variations from what I have seen here and in other googles but being very new to all of this can't seem to piece together exactly what I need. After a few days of trial and error I am close to what I am after, mysite.com/about serves /public/about.php correctly and mysite.com/private/whatever gets /private/app.php but it doesn't execute it, it serves it as a download instead.

Here is what I have so far:

server {                                                                                 

        listen 80;                                                                       
        listen [::]:80;                                                                  

        root /home/php/www/public;                                                       

        index index.php;                                                                 

        server_name mysite.com;                                        

        location /private/ {                                                                
                alias /home/php/www/private;                                                 
                try_files /app.php =404;                                              

                location ~ [^/]\.php(/|$) {                                              
#               location ~ \.php$ {                                                      
#                       fastcgi_split_path_info ^(.+\.php)(/.+)$;                        
                        include snippets/fastcgi-php.conf;                               
                        fastcgi_pass unix:/run/php/php7.2-fpm.sock;                      

#                       fastcgi_index app.php;                                        
#                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#                       include fastcgi_params;                                          

#                       return 302 https://google.com;                                   
                }                                                                        
        }                                                                                

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

        location ~ \.php$ {                                                              
                include snippets/fastcgi-php.conf;                                       
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }

        location @extensionless-php {
                rewrite ^(.*)$ $1.php last;
        }

        location ~ /\.ht {
                deny all;
        }
}

If I replace everything inside the location /private/ block with just return 302 https://google.com it redirects successfully to google.com, so I know this location block is being returned but putting the same thing inside the location ~ \.php$ inside location /private/ nothing happens so to me it looks like it is not hitting this block, what am I missing? There aren't any errors in /var/log/nginx/error.log relating to this.

1

1 Answers

1
votes

Try:

location /private/ {                                                                
    root /home/php/www/private;
    try_files /app.php =404;

    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

Use root rather than alias as you are not trying to alias the original request. The file was downloaded because of the unnecessary nested location block.