服务器配置

Nginx只允许Cloudflare IP访问获取真实IP

我的站长站 2023-08-12 人阅读

Nginx设置只允许Cloudflare IP访问后获取真实访客IP,跟普通套Cloudflare获取真实访客IP的方法不一样。

要使用map指令将客户端的 IP 存储到变量(ie$real_client_ip)中,并在日志中使用该变量:

# 这些放在配置文件头部---
map $http_x_forwarded_for $real_client_ip {
    ~^(\d+\.\d+\.\d+\.\d+) $1;
    default $http_cf_connecting_ip;
}
# replace the default '$remote_addr' with the '$real_client_ip'
log_format custom_log_format '$real_client_ip - $remote_user [$time_local] '
                             '"$request" $status $body_bytes_sent '
                             '"$http_host" "$upstream_response_time"'
                             '"$http_referer" "$http_user_agent"';
# 这些放在配置文件头部---
server {
  listen 80;
  listen [::]:80;
  server_name example.com;
  include /etc/nginx/allow-cloudflare-only.conf;
  #在生成日志文件后面加上custom_log_format
  access_log /var/log/nginx/access.log custom_log_format;
  #...the rest of your configs here...
}


相关专题
Cloudflare
Cloudflare
2023-08-07 37

Cloudflare是一款世界级热门CDN,Cloudflare支持免费使用,无限CDN防御,是全球许多网站防御的必备cdn工具.学好使用Cloudflare,可以帮助你的服务器免受网络攻击.我...

相关推荐
  • nginx教程
  • nginx限制并发数和限制下载速度方法

    分享一篇最近我的站长站刚学的nginx限制并发数和限制下载速度方法,可以限制一个IP同一时间只允许发起一个连接,如果发起多个链接,会提示503。另外还支持限制的下载速度,用户在开始的下载500m之前,下载速度可以飙到最大,但是一旦下载的文件大小超过了500m,下...

    服务器配置 63 1年前
  • nginx新手入门-认识location匹配规则

    我们在配置nginx时,会看到默认的nginx配置规则内有很多的location,这些location每一段就代表一个规则,location的编写方法需要会一点正则,不会的可以继续看我的站长站为大家带来的location新手入门教程。完整nginx配置规则如下:server{listen 80;server_n...

    服务器配置 11 1年前
  • Nginx与Apache有什么区别?

    Apache & NginxApache,指的应该是 Apache 软件基金会下的一个项目——Apache HTTP Server Project;Nginx 同样也是一款开源的 HTTP 服务器软件(当然它也可以作为邮件代理服务器、通用的TCP 代理服务器)。HTTP 服务器本质上也是一种应用程序——它通常运...

    服务器配置 127 4年前
最新更新