我有一个正在运行的网站(基于 php),该网站位于 nginx 后面,直接提供服务,而不是反向代理。基本的 nginx 如下:

server {

    listen 80;

    server_name my.server.com;

    index index.php;

    root /var/www/myserver/public; 

    location /client {
        root /var/www/myserver; 
        autoindex off;

        location ~* ^.+.(js|css|png|jpg|svg|svgz|jpeg|gif|ico|tpl)$ {
            access_log off;
            expires max;
        }
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }

    location /api/v1/ {
        if (!-e $request_filename){
            rewrite ^/api/v1/(.*)$ /api/v1/index.php last; break;
        }
    }
}

当用户导航到网站登录时,在向他们显示允许登录的 index.php 之前,我想先验证他们的 IP 地址,然后再提供 index.php;如果访问不合适,则拒绝访问;如果访问合适,则继续正常处理。黑名单和白名单不起作用,因为每个人都至少有一次尝试访问的机会。

我希望会话开始时在主页上运行类似下面的内容:

location = / {
    proxy_pass http://localhost:9017; # ip gate service
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

做这个的适当方法是什么?

1

  • 最好使用防火墙,它更安全,性能更好。我推荐该工具fail2ban,它使用 iptables 来阻止不适当的访问。您可以自行配置不适当的访问违规。阅读其文档。


    – 

0