简体   繁体   中英

Nginx block access to php files

I've created a simple php file to display info fetched from my MYSQL database. Right after that i use a rewrite rule in Nginx to make the link seo friendly and to mask the .php file that fetches it.

Ex: http://localhost/myfile.php?seo=how-to-install-linux After rewrite rule the i can access it like:

http://localhost/how-to-install-linux

My rewrite rules are:

location / {
rewrite ^/([a-zA-Z0-9_$\-]+)$ /myfile.php?seo=$1 last;
rewrite ^/(.*)/$ /$1 permanent; <- just to block trailing slash
}

My problem is that i also want to block any direct access to my php file and i want only the seo friendly url to work.

location = /myfile.php {
deny all;
}

This rule blocks complete access to my php file, including through seo friendy url. Is there a way to make it work for the seo friendly version using NGINX? My other settings are:

location ~ \.php$ {
try_files $uri =404;
include fcgi.conf;
fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;

}

I only use Nginx, no Apache installed.

You can use the internal directive to prevent a location from being accessed directly. See this document for details.

For example:

location = /myfile.php {
    internal;
    include fcgi.conf;
    fastcgi_pass unix:/var/run/ajenti-v-php7.0-fcgi-drnou-php7.0-fcgi-0.sock;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM