简体   繁体   English

Nginx阻止对php文件的访问

[英]Nginx block access to php files

I've created a simple php file to display info fetched from my MYSQL database. 我创建了一个简单的php文件来显示从我的MYSQL数据库获取的信息。 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. 之后,我在Nginx中使用了重写规则,使链接seo友好并掩盖了获取它的.php文件。

Ex: http://localhost/myfile.php?seo=how-to-install-linux After rewrite rule the i can access it like: 例如: http://localhost/myfile.php?seo=how-to-install-linux重写规则后,我可以像这样访问它:

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. 我的问题是我也想阻止对我的php文件的任何直接访问,而我只希望seo友好的url起作用。

location = /myfile.php {
deny all;
}

This rule blocks complete access to my php file, including through seo friendy url. 该规则阻止对我的php文件的完全访问,包括通过seo friendlyy url。 Is there a way to make it work for the seo friendly version using NGINX? 有没有办法使用NGINX使它适用于seo友好版本? 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. 我只使用Nginx,没有安装Apache。

You can use the internal directive to prevent a location from being accessed directly. 您可以使用internal指令来防止直接访问位置。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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