
背景:我運行了幾個 WordPress 網站,我注意到大量的分散式暴力登入嘗試。 fail2ban 在這裡沒有幫助我,因為攻擊者小心翼翼地只嘗試每個 IP 最多 5 次登錄,然後切換到另一個 IP。
我的問題不是擔心密碼被正確猜出。我只擔心這會對伺服器造成巨大的負載。
我的想法很簡單:我住在瑞士一些(並非所有)網站只能從瑞士登錄,因此我想阻止來自其他國家/地區的這些網站的登入嘗試。聽起來很容易?好吧,nginx 不想同意我的觀點。
我將以下程式碼片段設定為snippets/php.conf
:
index index.html index.htm index.php;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_read_timeout 300;
}
我將大多數文件中的這段程式碼包含在/etc/nginx/sites-enabled
.我已經啟用並配置了 nginx geoip 模組,它似乎工作正常(此處未顯示配置)。許多網站配置如下所示:
server {
listen 80;
server_name mydomain.com;
location / {
# common WordPress config
try_files $uri $uri/ /index.php;
}
location ^~ /wp-login.php {
# CH is the country code for Switzerland
if ($geoip_country_code != CH) {
return 444;
}
include snippets/php.conf; # <-- #1
}
root /var/www/mydomain.com;
include snippets/php.conf;
}
我到處都讀到 nginx 只匹配一位置塊,因為這就是它的工作原理。因此它只能匹配 的位置塊wp-login.php
或匹配 的塊.php
,但不能同時匹配兩者。
所以如果我消除#1
標有“然後事情看起來不錯”的行:在瑞士我可以訪問wp-login.php
它,而在瑞士之外我無法訪問它。偉大的!但是,當我可以訪問它時,它只是下載wp-login.php
檔案而不是執行它。這似乎是預料之中的,因為執行的位置區塊.php
不匹配,因此 fastcgi 的東西不起作用。
這讓我相信我實際上確實需要添加標有 的行#1
。但如果我這樣做,那麼我突然可以從所有 IP 訪問,wp-login.php
無論它們來自哪個國家。
我究竟做錯了什麼?我發現了大量關於如何使用 nginx 設定 PHP 的文章,以及大量關於如何阻止某些國家的文章。但沒有一個能同時做到這兩點。
編輯:
以下是內容,snippets/fastcgi-php.conf
以防萬一,我相信其中一個來自 Debian Stretch 上 nginx 的預設安裝:
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi.conf;