拒绝负载均衡器后面的Nginx请求

问题描述

我在Nginx上的AWS负载均衡器后面有我的网站。我想阻止某些IP来访问我的网站。我做了很多尝试,但是Nginx无法识别原始客户端IP,但是我无法阻止IP。请帮助我实现这一目标。我的Nginx虚拟主机文件如下:

我使用自定义日志格式在日志中找到了客户端IP以及负载均衡器IP,但无法阻止该客户端IP。

fastcgi_cache_path /mnt/cache/example.com/cache levels=1:2 keys_zone=example.com:100m inactive=30m;

log_format main '$http_x_forwarded_for - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent "$http_referer" '
    '"$http_user_agent"' ;

server {
        server_name example.com;
        root /var/www/website;
        index index.PHP;

include modsecurity.conf;
############ Skip Cache #########
set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}   
if ($query_string != "") {
    set $skip_cache 1;
}   

# Don’t cache uris containing the following segments
if ($request_uri ~* "/wp-admin/xmlrpc.PHP|wp-.*.PHP|/Feed/|index.PHP|sitemap(_index)?.xml") {
    set $skip_cache 1;
}   

# Don’t use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

####################################

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                try_files $uri $uri/ /index.PHP?$args;
        }

        location ~ \.PHP$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
            fastcgi_pass wpfpmpool;
            fastcgi_index index.PHP;
            include fastcgi_params;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache divyahimachal.com;
        fastcgi_cache_valid 30m;

        }


       location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
               expires max;
               log_not_found off;
        }
    
    # Logging –
    access_log  /var/log/Nginx/example.com_custom.access.log;
    access_log  /var/log/Nginx/example.com_custom.access.log main;
    error_log  /var/log/Nginx/example.com_error.log;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)