1

My nginx server is not allowing for the php files on subfolder giving a 403

I have a apache that is running ok with a .htaccess

{
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?path=$1 [QSA,L]
RewriteRule ^(.*)\.[\d]{10}\.(css|js)$ $1.$2 [L]
}

The folder structure is like this :

-root
-- Classes
-- Controllers
-- Includes
-- Models
-- Views
--- Home
---- home-view.php
-Index.php

The index is run and open on the browser, but in a button with a link that points to \views\home\ and should load the home-view.php by a controller it's giving me a 403 forbidden, on apache all is working.

the nginx conf is :

root /var/www/html;
index index.php index.html index.htm;
server_name cityguide.com wwwcityguide.com

location / {
  if (!-e $request_filename){
    rewrite ^(.+)$ /index.php?path=$1 break;
  }
  rewrite "^/(.*)\.[\d]{10}\.(css|js)$" /$1.$2 break;
  try_files $uri $uri/ =404
}
1
  • have you installed and provided the correct path of php in your Nginx configuration? Commented Jul 15, 2019 at 11:37

1 Answer 1

1

If you have php-fpm simple provide path in Nginx just like below example. If not you can install php-fpm use following command

sudo apt-get install phpx-fpm

where x is version of php for example for php7.3 it should be

sudo apt-get install php7.3-fpm

Then add following line in your configuration file

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

Then you file looks like(considering you are using php7.3)

server {
    listen 80;
    listen [::]:80;

    . . .

    root /var/www/your folder path;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

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

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

Also, don't forget to add try_files $uri $uri/ /index.php?$query_string; this line your location otherwise other routes of MVC not work for you.

Sign up to request clarification or add additional context in comments.

6 Comments

location ~ \.php$ { include snippets/fastcgi-php.conf fastcgi_pass unix:/run/php/php7.1-fpm.sock fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name}
Also, if you want you can achive the same using fastcgi as well for that you need to add following line fastcgi_pass 127.0.0.1:9000;
just tried everything and still i get the 403 forbidden on the sub folder, on the root all seems to be working
Please check your php-fpm path and updated your configure file accordingly
nothing seems to work, i've check the fpm path all seems to be correct..if i remove the $uri / it doens give the 403, but it still doesnt load the file as apache does, maybe the htaccess that i need and did convert to nginx doesnt get run ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.