WordPress 禁止访问核心 PHP 文件教程(小白代码篇)

20 篇文章 4 订阅
5 篇文章 0 订阅

Nginx 的配置文件为例,禁用某些目录执行 PHP。

宝塔控制面板—点击网站—配置设置

#注:横杠上面是本站默认代码,复制横杠以下代码,

server {
 
listen 80;
server_name website.com;
# Redirect non-www to www (httple.net -> www.httple.net)
return 301 http://www.$server_name$request_uri;
}
 
server {
 
    listen 80;
    server_name www.website.com;
    access_log /var/www/httple.net/logs/access.log main;
    error_log /var/www/httple.net/logs/error.log warn;
    root /var/www/httple.net/public/htdocs;
    index index.html index.htm index.php;
 
# 日志不记录 robots.txt
    location = /robots.txt {
        log_not_found off;
        access_log off;
    }
 
# 如果没有 favicon 文件则退出并返回 204 (没有错误内容)
 
    location ~* /favicon\.ico$ {
        try_files $uri =204;
        expires max;
        log_not_found off;
        access_log off;
    }
 
# 以下格式文件日志不需要记录
 
    location ~* \.(js|css|png|jpg|jpeg|bmp|gif|ico)$ {
        expires max;
        log_not_found off;
        access_log off;
        # Send the all shebang in one fell swoop
        tcp_nodelay off;
        # Set the OS file cache
        open_file_cache max=1000 inactive=120s;
        open_file_cache_valid 45s;
        open_file_cache_min_uses 2;
        open_file_cache_errors off;
    }
 
    # http://wiki.nginx.org/WordPress
    # 设置静态地址必须要添加的配置
    # 如果你后台添加了固定链接,则需要添加以下配置
 
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
 
#—————————————————————————————————————————————————————————————————————————————————————————
#注:上面是本站默认代码,复制以下代码,


# 禁止访问 htaccess 文件
 
    location ~ /\. {
        deny  all;
    }
 
# nocgi cgi等可执行的,不允许
 
    location ~* \.(pl|cgi|py|sh|lua)\$ {
            return 444;
    }
 
#禁止访问 wp-config.php install.php 文件
 
    location = /wp-config.php {
            deny all;
    }
 
    location = /wp-admin/install.php {
        deny all;
    }
 
# 禁止访问 /wp-content/ 目录的 php 格式文件 (包含子目录)
 
    location ~* ^/wp-content/.*.(php|phps)$ {
        deny all;
    }
 
# 允许内部分  wp-includes 目录的 .php 文件 
 
    location ~* ^/wp-includes/.*\.(php|phps)$ {
        internal;
    }
 
 
# 禁止访问 /wp-content/ 目录的以下文件格式 (包含子目录)
 
    location ~* ^/wp-content/.*.(txt|md|exe)$ {
        deny all;
    }
 
# 禁止uploads、images目录下面的所有php、jsp访问
    location ~ ^/(uploads|images)/.*\.(php|php5|jsp)$ {
        deny all;
        #return 403;
    }
 
# 禁止访问目录 /conf/*
 
    location ^~ /conf/ {
        deny all;
    }
 
    # 注意:上述/conf/后面的斜杠不能少,否则所有以conf开头的目录或文件都将禁止访问。
 
 
 
## 禁止访问任何目录下的.sql文件,禁止浏览器访问
 
 
    location ~.*\.sql {
        deny all;
    }
 
    # 这样,任一目录的sql文件都不会被用户访问到了。 
 
 
# 处理 .php 文件
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include /etc/nginx/fastcgi_params;
        fastcgi_connect_timeout 180s;
        fastcgi_send_timeout 180s;
        fastcgi_read_timeout 180s;
        fastcgi_intercept_errors on;
        fastcgi_max_temp_file_size 0;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
    }
 
# 限制登陆和管理IP地址
 
    location ~ ^/(wp-admin|wp-login\.php) {
        allow 1.2.3.4;
        deny all;
 
        ## 下面是fastcgi 方式
        index               index.php index.html index.htm;
        fastcgi_index       index.php;
        fastcgi_pass        127.0.0.1:9000;
        include             fastcgi.conf;
 
        ## 下面是代理方式的设置
        proxy_pass  http://apachebackend;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
 
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 
# wordpress 重写规则
 
    rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
 
    # Add trailing slash to */wp-admin requests
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
 
 
# 403页面配置
 
    error_page 403 http://httple.net/404.html;      # 指定CDN页面
 
    error_page 403 404.html;      # 指定当前项目根目录下的404.html文件 
}
#—————————————————————————————————————————————————————————————————————————————————————————

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云博客-资源宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值