skip to Main Content

Say a website has this folder structure

/index.php
/<public>
   dikpic.jpeg

And when someone visits the website I want the physical web root to point to /public,
like mywebsite.com/dikpic.jpeg

(without url rewrites).

This can be achieved with the root /myuser/public; command.

But I also want to load the index.php file from outside this directory:

index /myuser/index.php;

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~* .php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

The problem is with the location try files block that assumes / is the web root 🙁

Any way around this?


I should also point out that the index.php is the only script on the website.
So I don’t really care if any other requests for .php files are ignored. The index.php handles url rewriting stuff…

2

Answers


  1. You can create symlinks of your index.php here is how to do that.

    And as result you will have single index.php to all your websites

    Login or Signup to reply.
  2. You can use an additional location block for that, although it doesn’t seems an elegant solution to me:

    location ~* .php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
    
    location = /index.php {
        fastcgi_pass unix:/run/php/php7.4-fpm-myuser.sock;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    /full/path/to/your/index.php;
        fastcgi_param   SCRIPT_NAME        /index.php;
    }
    

    Exact matching locations have a priority over regex matching ones, so the first block will be used for any PHP file request except the /index.php. You don’t even need to define a root for the second location, setting right SCRIPT_FILENAME FastCGI parameter value will be enough.

    Update

    I didn’t notice the very last sentence of your question, but if you didn’t care for any other PHP files, only the second block location = /index.php { ... } will be enough.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search