Access denied on nginx and php

Using nginx and php web server. nginx works, I see "Welcome to nginx!" but I get "access denied" when I try to access the php page. I also installed php-fastcgi.

Here is my default nginx config:

# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP s to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP s to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /usr/share/nginx/html; fastcgi_index index.php; fastcgi_pass unix:/var/run/php5-fpm.sock; include fastcgi_params; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache document root # concurs with nginx one # location ~ /\.ht { deny all; } 

I activated security.limit_extensions = .php .php3 .php4 .php5 .html and listen = /var/run/php5-fpm.sock in / etc / php -fpm.d / www.conf and cgi.fix_pathinfo = 0 in / etc / php 5 / fpm / php.ini

I restarted nginx and php5-fpm.

Thanks for the help.

+7
source share
4 answers

Do this if you have a secondary location

 location / { try_files $uri $uri/ =404; root /path/to/your/www; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 

These 2 parameters are magic sauce:

 fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
+9
source

I know some possible scenarios when nginx and php cannot access files:

  • Most likely, the php-fpm started by a user who does not have read permission in the corresponding .php files. This gives a simple Access denied. error Access denied.

  • nginx The process does not have permission to read and move to the root directory containing the site files. This gives a 403 Forbidden error.

  • php-fpm process cannot go through the absolute path to the root directory. This gives a File not found error.

Since the author mentions that the problem only occurs when accessing php files, I would say that the first scenario applies here.

I suppose the fact is that nginx runs as one user and php-fpm as another, only the php-fpm been forgotten to provide read access.

+1
source

Please check the fastcgi_params file and modify it according to this post https://askubuntu.com/questions/164627/nginx-php-fpm-access-denied-error

I solved my problem using the above method.

+1
source

In case you are trying to use NGINX to parse HTML like PHP, and you get the " Access denied in Access denied " error message, you need to change the PHP configuration setting.

On Ubuntu 16, the file to be updated is located in /etc/php/7.0/fpm/pool.d/www.conf .

Go to the line where it says ;security.limit_extensions =.php.php3.php4.php5.php7

Replace this with security.limit_extensions =.php.html . Note that the leading semicolon has been removed.

Then restart PHP sudo systemctl restart php7.0-fpm . The problem must be fixed.

See this more detailed guide for more information: The "Access Denied" error has been fixed when parsing HTML as PHP with Nginx.

0
source

All Articles