Nginx block access to php files

白昼怎懂夜的黑 提交于 2019-12-24 00:24:05

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/47379091/nginx-block-access-to-php-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!