且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 nginx 配置文件保护 wordpress 中的 wp-login.php

更新时间:2022-04-18 22:30:01

我终于解决了这个问题的神话,而且我的做法有所不同.我没有依赖 IP 来设置 wp-login.php 页面的访问权限,而是在 Ubuntu 20.04 上使用 Nginx 配置了密码身份验证.我已经使用 apache 实用程序创建了密码文件.按照 本教程安装它.安装后,打开链接到您的 WordPress 站点的 Nginx 配置文件:
sudo nano/etc/nginx/sites-enabled/default
或此配置文件,如果您使用块:
sudo nano/etc/nginx/sites-enabled/example.com

添加此代码段:

I have finally solved the myth of this question, and I did it differently. Instead of relying on the IP to set access permissions to the wp-login.php page, I have configured a Password Authentication with Nginx on Ubuntu 20.04. I have created the password file using the apache utilities. Follow this tutorial to install it. After you install it, open the Nginx configuration file that is linked to your WordPress site:
sudo nano /etc/nginx/sites-enabled/default
or this configuration file, if you use blocks:
sudo nano /etc/nginx/sites-enabled/example.com

Add this snippet:

## Protect my wordpress login page and directory with a password 
    location = /wp-login.php {
    auth_basic  "Login";
    include snippets/fastcgi-php.conf;
    auth_basic_user_file /etc/nginx/.htpasswd;
    fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }

请注意,fastcgi_pass unix:/run/php/php8.0-fpm.sock; 必须与您在网站上运行的当前 PHP 版本相匹配.上面的代码片段对我有用,可以保护 wp-admin 和 wp-login.php.我很难想出这个,因为所有其他版本都用于将登录页面作为文件下载或在没有 wp-login.php 的情况下保护 wp-admin 目录

请不要犹豫,为此主题添加更多内容以完善它.

Please note that fastcgi_pass unix:/run/php/php8.0-fpm.sock; must match your current PHP version which you are running on your website. The above snippet worked for me to protect wp-admin AND wp-login.php. I struggled to come up with this because all other versions used to download the login page as a file or to protect the wp-admin directory without the wp-login.php

Please don't hesitate to add more to this topic to refine it.