• [交流分享] CentOS7安装Nginx
    #化鲲为鹏,我有话说#安装所需环境yum install gcc-c++yum install pcre pcre-develyum install zlib zlib-develyum install openssl openssl-devel下载.tar.gz安装包https://nginx.org/en/download.htmlwget https://nginx.org/download/nginx-1.16.0.tar.gz解压tar zxvf nginx-1.16.0.tar.gzcd nginx-1.16.0编译安装./configuremake && make install建立软链接cd /usr/bin/ln -n /usr/local/nginx/sbin/nginx ./
  • [中间件] 【华为云鲲鹏云服务最佳实践】【Web篇】第004期 nginx 1.8.0 安装配置指南
    1、简介      Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。      官方链接:http://nginx.org/       类别:代理服务器2、基础环境类别子项版本获取地址(方法)华为云虚拟机RC3(916)--OSCentOS7.5 Kernel4.14 软件包Nginx1.8.0https://kojipkgs.fedoraproject.org/packages/nginx/1.8.0/10.fc22/src/nginx-1.8.0-10.fc22.src.rpm3、依赖安装无4、组件编译安装此步骤建议在chroot环境下进行,一来对chroot环境可以随意修改,不会影响系统,二来多人共用系统环境,免不了对系统有更改,chroot环境更纯净。执行后/root/zmm目录下就是个可用的chroot环境,chroot进去即可进行任意操作。[root@arm-team-0009 ~]# yum install --installroot=/root/xxx/ -y basesystem yum rpm-build[root@arm-team-0009 ~]# mount --bind /dev/ /root/xxx/dev[root@arm-team-0009 ~]# mount --bind /proc/ /root/xxx/proc/Chroot环境创建后,进去发现很多基础命令没有,比如vi,需要自行安装对应的rpm包。如下图所示:已可用。其它命令如遇到不可用的,请参考上图的方法添加。推荐所有reo源修改为华为开源镜像站,速度更快更稳定。配置参考如下图所示:bash-4.2# grep "huawei" -ri /etc/yum.repos.d//etc/yum.repos.d/CentOS-EPEL.repo:baseurl=https://mirrors.huaweicloud.com/epel/7/aarch64//etc/yum.repos.d/euleros.repo:baseurl=http://mirrors.huaweicloud.com/euler/2.3/os/aarch64//etc/yum.repos.d/euleros.repo:gpgkey=http://mirrors.huaweicloud.com/euler/2.3/os/RPM-GPG-KEY-EulerOS/etc/yum.repos.d/CentOS-Base.repo:baseurl=https://mirrors.huaweicloud.com/centos-altarch/7/os/aarch64//etc/yum.repos.d/CentOS-Base.repo:baseurl=https://mirrors.huaweicloud.com/centos-altarch/7/updates/aarch64//etc/yum.repos.d/CentOS-Base.repo:baseurl=https://mirrors.huaweicloud.com/centos-altarch/7/extras/aarch64//etc/yum.repos.d/CentOS-Base.repo:baseurl=https://mirrors.huaweicloud.com/centos-altarch/7/centosplus/aarch64/SPEC文件是rpmbuild编译出rpm包的核心,其指定了编译期依赖及运行时依赖,也指定了此包的能力。从fedora下载nginx的src rpm包安装进行编译,可以大大简化编译及避免运行时可能遇到的问题。bash-4.2# yum install -y which wget git sudo makebash-4.2#wget https://kojipkgs.fedoraproject.org/packages/nginx/1.8.0/10.fc22/src/nginx-1.8.0-10.fc22.src.rpm   或者,我们可以使用fedora的git库(其中不包含tar包,下载较快,但错误较多,请做好准备)bash-4.2# git clone https://src.fedoraproject.org/rpms/nginx.gitbash-4.2# git checkout f22说明:git库的源码没有1.8.0的,最接近的是1.8.1,相比1.8.0修复了3个CVE漏洞。安装源码包:  Spec文件里要求的编译依赖如下:BuildRequires:     GeoIP-develBuildRequires:     gd-develBuildRequires:     libxslt-develBuildRequires:     openssl-develBuildRequires:     pcre-develBuildRequires:     perl-develBuildRequires:     perl(ExtUtils::Embed)BuildRequires:     zlib-develBuildRequires:     systemdSpec文件里要求的安装依赖如下:Requires:          nginx-filesystem = %{epoch}:%{version}-%{release}Requires:          GeoIPRequires:          gdRequires:          opensslRequires:          pcreRequires:     perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))Requires(pre):     nginx-filesystemRequires(post):    systemdRequires(preun):   systemdRequires(postun):  systemdRequires(pre):     shadow-utils找到依赖的对应rpm包安装即可。bash-4.2# yum install -y GeoIP-devel gd-devel libxslt-devel openssl-devel pcre-devel perl-devel zlib-devel systemd perl-ExtUtils-Embedbash-4.2# rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec编译后的结果如下:安装:卸载:5、系统配置无6、测试无7、参考信息无8、FAQ无
  • [中间件] 【鲲鹏翱翔】应用中间件02-Nginx移植安装指南
    1. 简介Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。2. 部署环境Packet NameVersion版本DetailCentOSCentOS   7.5 with Armarm通用计算增强型 | rc3.xlarge.4 | 4vCPUs | 14GBNginx1.6none3. 安装部署yum源安装Nginxyum install nginx查看nginx版本nginx -v编译安装Nginx安装常用的系统组件,安装相应的库:yum   install unzip gcc gcc-c++ openssl    openssl-devel glibc-static libstdc++-static lzip libffi-devel zlib   zlib-devel libtool安装PCREPCRE 作用是让 Nginx 支持 Rewrite 功能。从 http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz获取软件安装包: wget  http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz       解压pcre-8.35.tar.gztar   zxvf pcre-8.35.tar.gz进入解压路径,进行编译安装:cd   pcre-8.35./configure   –-prefix=/usr/local/pcre #/usr/local/pcre安装路径make &&   make install 查看pcre版本:[pcre-config --version安装nginx从http://nginx.org/download/获取软件安装包: wget  http://nginx.org/download/nginx-1.6.2.tar.gz       解压nginx-1.6.2.tar.gztar  -zxvf nginx-1.6.2.tar.gz进入解压路径,进行编译安装:cd   nginx-1.6.2./configure   --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module   --with-pcre=/usr/local/pcre-8.35 #pcre的源码所在目录make &&   make install 查看nginx版本:/usr/local/nginx/sbin/nginx -v 4.示例测试创建Nginx运行用户www/usr/sbin/groupadd www /usr/sbin/useradd -g www www配置nginx.conf ,将/usr/local/nginx/conf/nginx.conf替换为以下内容#  cat /usr/local/nginx/conf/nginx.conf---------------------------------------------------user www www;worker_processes 2; #设置值和CPU核心数一致error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 65535;events{  use epoll;  worker_connections 65535;}http{  include mime.types;  default_type application/octet-stream;  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '               '$status $body_bytes_sent "$http_referer" '               '"$http_user_agent" $http_x_forwarded_for';  #charset gb2312;       server_names_hash_bucket_size 128;  client_header_buffer_size 32k;  large_client_header_buffers 4 32k;  client_max_body_size 8m;       sendfile on;  tcp_nopush on;  keepalive_timeout 60;  tcp_nodelay on;  fastcgi_connect_timeout 300;  fastcgi_send_timeout 300;  fastcgi_read_timeout 300;  fastcgi_buffer_size 64k;  fastcgi_buffers 4 64k;  fastcgi_busy_buffers_size 128k;  fastcgi_temp_file_write_size 128k;  gzip on;  gzip_min_length 1k;  gzip_buffers 4 16k;  gzip_http_version 1.0;  gzip_comp_level 2;  gzip_types text/plain application/x-javascript text/css application/xml;  gzip_vary on;   #limit_zone crawler $binary_remote_addr 10m; #下面是server虚拟主机的配置 server  {    listen 80;#监听端口    server_name localhost;#域名    index index.html index.htm index.php;    root /usr/local/nginx/html;#站点目录      location ~ .*\.(php|php5)?$    {      #fastcgi_pass unix:/tmp/php-cgi.sock;      fastcgi_pass 127.0.0.1:9000;      fastcgi_index index.php;      include fastcgi.conf;    }、    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$    {      expires 30d;  # access_log off;    }    location ~ .*\.(js|css)?$    {      expires 15d;   # access_log off;    }    access_log off;  }}检查配置文件是否正确/usr/local/nginx/sbin/nginx  -t 启动nginx服务/usr/local/nginx/sbin/nginx访问站点http://EIP/可以看到网页                                              5.参考信息     nginx官网:https://nginx.org/     github地址:https://github.com/nginx/nginx 6.FAQ     暂无
  • Ubuntu Linux配置Nginx+MySQL+PHP+phpMyAdmin详细步骤
    sudo apt-get updatesudo apt-get install nginxsudo killall apache2usrshare/nginx/html,我们将其修改为/var/www,当然可以根据个人习惯灵活更改。etcnginx/sites-available/default文件root /usr/share/nginx/html;root /var/wwwindex index.html index.htm;index index.html index.php index.htm;sudo service nginx restartvarwww目录下新建index.html文件,写入一些测试文字。sudo apt-get install php5 php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-tidy php5-xmlrpc php5-sqlite php5-xslsudo apt-get install mysql-server mysql-clientsudo apt-get install phpmyadminsudo ln -s /usr/share/phpmyadmin/ /var/www/phpmyadminvarwww,链接名为phpmyadmin的文件,指向/usr/share/phpmyadminsudo apt-get install spawn-fcgietcnginx/fastcgi_params文件,增加下面一行fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;etcphp5/cgi/php.ini文件,将下面一行取消注释cgi.fix_pathinfo=1;etcnginx/sites-available/default文件,我们之前修改的目录地址是/var/www,将1234567891011        #location ~ \\.php$ {    #    fastcgi_split_path_info ^(.+\\.php)(/.+)$;     #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini     #     #    # With php5-cgi alone:     #    fastcgi_pass 127.0.0.1:9000;     #    # With php5-fpm:     #    fastcgi_pass unix:/var/run/php5-fpm.sock;     #    fastcgi_index index.php;     #    include fastcgi_params;    #}    location ~ \\.php$ {     #    fastcgi_split_path_info ^(.+\\.php)(/.+)$;     #    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini     #     #    # With php5-cgi alone:         fastcgi_pass 127.0.0.1:9000;     #    # With php5-fpm:     #    fastcgi_pass unix:/var/run/php5-fpm.sock;         fastcgi_index index.php;         fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;         include fastcgi_params;     }sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pidspawn-fcgi: child spawned successfully: PID: 11775etcrc.local文件,添加下面一行/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pidsudo service nginx restartvarwww目录下新建index.php文件测试运行,发现已经可以运行PHP文件了。phpmyadmin便可以轻松加愉快地访问了。来源: Ubuntu Linux配置Nginx+MySQL+PHP+phpMyAdmin详细步骤
  • [视频点播] HTTP流媒体播放技术发展以及nginx点播源站
    本帖最后由 leewei 于 2018-3-2 14:51 编辑互联网多媒体内容传输从大方向上可以分为下载传输和流式传输,而流式传输又可以分为顺序流式传输和实时流式传输,换句话来说就是点播(Video on Demand)和直播(Live Streaming),顾名思义,前者的媒体内容是提前存储在服务器上供客户端请求播放,而后者是实时产生并分发给客户端播放。   本文主要是基于HTTP的流媒体点播技术入门,讲述了一些基本的概念以及这一技术的变革演进。   互联网上传播音视频内容最早从上世纪90年代开始,RTP等专有协议的提出就是定义音视频的包格式以获得更低的网络传递开销。不过现如今的互联网世界,CDN扮演着非常重要的角色,而绝大部分CDN厂商是不支持RTP等协议的。基于HTTP的流媒体点播技术则占据着主流,其有几点优势:1、防火墙友好;2、客户端控制媒体流的访问,服务端不需要为每一个客户端连接维护媒体session状态;3、采用标准的HTTP服务器即可,支撑大规模客户端访问不需要额外的服务器技术资源开销,最重要的就是CDN技术的良好支持优化。   HTTP流媒体技术的变革大致经过了以下四个阶段:   最初,我们只能先下载完整视频文件到本地磁盘后,然后才能播放观赏。这意味着你必须等待视频下载操作的完成,严格来讲,根据我们第一段的划分,这不能称作为流媒体传输,不过我们还是列出来,以作对比。    在上面简单下载文件的基础上,一个明显的提升就是渐进式下载技术。这种情况下不需要完整下载视频文件,可以边下载边播放,这就要求视频的元数据信息得放在视频文件的开头。但是,我们只能在已经下载的那一部分自由拖拽播放,而未下载的部分是不能播放的。   在渐进式下载的基础上,pseudo-streaming(伪流)出现了,其增强了seek播放功能,也就是支持直接切到未下载的地方进行观看,所以也可以称之为渐进式播放。    其实,到pseudo-streaming 这一步,流媒体播放的技术已经很成熟了,而且目前绝大多数的视频网站都是这种技术,比如Youtube、优酷、腾讯视频等。如果,要在这个基础上再一步的优化,可以从哪些方面呢?比较容易想到的就是带宽优化,能够链路感知,这就是下面的自适应比特率流。其原理就是,同一视频内容会有不同的码率版本,客户端播放器会动态地根据网络质量切换请求带宽匹配的的视频片段。    自适应码率流媒体技术,业内比较常见的实现是Apple的HTTP Live Streaming (HLS)以及Adobe的HTTP Dynamic Streaming (HDS),下图简单展示了HLS的文件结构,可以看到master playlist包含了两种不同码率的播放列表,当然如果你只有一种版本,那么就不需要master playlist了。基于HTTP的自适应码率流媒体是有国际标准的,那就是3GPP组织和MPEG小组所提出的MPEG-DASH(Dynamic Adaptive Streaming over HTTP)。    最后,我们来说说一个简单的点播流媒体服务器是怎么工作的。   目前,很常见的一种搭建方式就是基于nginx,而nginx本身对音视频媒体的处理就有一定的支持,官方就有flv和mp4的插件,即ngx_http_flv_module和ngx_http_mp4_module。前者支持以字节偏移的渐进式seek播放,后者支持以时间偏移的渐进式seek播放。   要想达到这种渐进式播放的目的,大部分情况下,我们是要对服务端的媒体文件进行一定的处理的。首先播放之前,服务端需要先返回一定的视频元数据信息,其次我们需要知道媒体时间和媒体数据的映射关系。为什么呢?   目前大部分的视频编码都采用H264标准,而H264的不同视频帧采用了不同的压缩编码方式,其中I帧的作用很大,其通常是每个 GOP(Group of Picture)的第一个帧,我们简单点说,I帧依靠自身就可以解码出完整图像,而其后面帧的解码则需要依赖I帧,这意味着什么?   是的,这决定了我们在拖拽播放的时候,播放器会seek到最近的I帧处,所以有时候会有一个现象,就是你拖到一个精确的时间点,但是播放器却在另一个靠近的时间点开始播放,这就是因为那个时间点是I帧所在之处。   播放器的seek播放通常是让用户选择时间偏移,而服务端最终对文件的请求处理只能是字节偏移,所以问题来了,我们要有一个时间偏移和字节偏移的映射表,严格一点说,是每一个I帧的时间偏移与字节偏移映射。   问题又来了,这个映射是放在客户端还是服务端处理呢?当然是都可以。如果客户端发出字节偏移请求,那么服务端就很轻松,只需要提供HTTP range访问的功能,但是client端需要提前知道时间与字节的映射关系,比如flv格式的metadata就提供了这个信息,有的flv文件没有加入metadata,这时候就需要用相应的flv工具(比如flvmeta)去处理成这样的格式,这样客户端的播放器就可以直接发送字节偏移量了。MP4则不需要这么处理,因为MP4格式本身就要求带这样的信息,其记录metadata的Movie Box (moov)中的Media Information Box (minf)就存储了媒体时间和媒体数据的映射关系。如果客户端只发出时间请求,那么服务端就得将时间偏移请求转换为字节偏移请求,然后处理并响应请求。
  • 【干货分享】获取用户IP的正确姿势
    如何获取用户的IP,这个需求简直是太常见了,像登录入口,注册入口,投票,日志记录,api接口中判断同一个ip单位时间内的请求数,可是怎么去获取用户的真实IP呢?网上的代码很多,好多人直接拿来就用,却没有想到带来了很大的安全问题。1 代码示例[table=98%,none] 1 2 3 4 5 6 7 8 9 10[color=rgb(248, 248, 242) !important]?php [color=rgb(102, 217, 239) !important]if(![color=rgb(102, 217, 239) !important]empty($_SERVER[[color=rgb(230, 219, 93) !important]'HTTP_CLIENT_IP')){ [color=rgb(0, 111, 224) !important] $myip[color=rgb(0, 111, 224) !important] =[color=rgb(0, 111, 224) !important] $_SERVER[[color=rgb(230, 219, 93) !important]'HTTP_CLIENT_IP'; }[color=rgb(102, 217, 239) !important]else[color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]if(![color=rgb(102, 217, 239) !important]empty($_SERVER[[color=rgb(230, 219, 93) !important]'HTTP_X_FORWARDED_FOR')){ [color=rgb(0, 111, 224) !important] $myip[color=rgb(0, 111, 224) !important] =[color=rgb(0, 111, 224) !important] $_SERVER[[color=rgb(230, 219, 93) !important]'HTTP_X_FORWARDED_FOR'; }[color=rgb(102, 217, 239) !important]else{ [color=rgb(0, 111, 224) !important] $myip=[color=rgb(0, 111, 224) !important] $_SERVER[[color=rgb(230, 219, 93) !important]'REMOTE_ADDR'; } [color=rgb(102, 217, 239) !important]echo[color=rgb(0, 111, 224) !important] $myip; ?> 这是网上的一个示范例子,我们很多同事也这么写,上面这个例子是php实现的,由于HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR,HTTP_X_FORWARDED,HTTP_X_CLUSTER_CLIENT_IP,HTTP_FORWARDED_FOR,HTTP_FORWARDED,HTTP_VIA (经过的 Proxy)这些以HTTP打头的server变量都是用户可控的,由此可导致xss,认证绕过等缺陷。下面我们看下python的例子:[table=98%,none] 1 2 3 4 5 6 7 8[color=rgb(248, 248, 242) !important][color=rgb(249, 38, 80) !important]def [color=rgb(249, 38, 80) !important]get_ip(request): [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]try: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]return[color=rgb(0, 111, 224) !important] request.META[[color=rgb(230, 219, 93) !important]'HTTP_X_FORWARDED_FOR' [color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]except KeyError: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]try: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]return[color=rgb(0, 111, 224) !important] request.META[[color=rgb(230, 219, 93) !important]'HTTP_X_REAL_IP' [color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]except KeyError: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]return[color=rgb(0, 111, 224) !important] request.META.[color=rgb(249, 38, 80) !important]get([color=rgb(230, 219, 93) !important]'REMOTE_ADDR',[color=rgb(0, 111, 224) !important] None) 也是由于客户端变量可控导致获取的ip可为任意值。在此例中,X-Real-IP是nginx特有的,通过配置proxy_set_header X-Real-IP $remote_addr;从REMOTE_ADDR中取值。2 X-Forwarded-For和 REMOTE_ADDR的区别REMOTE_ADDR代表着客户端的IP,但是这个客户端是相对服务器而言的,也就是实际上与服务器相连的机器的IP(建立tcp连接的那个),这个值是不可伪造的,如果没有代理的话,这个值就是用户实际的IP值,有代理的话,用户的请求会经过代理再到服务器,这个时候REMOTE_ADDR会被设置为代理机器的IP值。 正如前面所说,有了代理就获取不了用户的真实IP,由此X-Forwarded-For应运而生,它是一个非正式协议,在请求转发到代理的时候代理会添加一个X-Forwarded-For头,将连接它的客户端IP(也就是你的上网机器IP)加到这个头信息里,这样末端的服务器就能获取真正上网的人的IP了。 假设用户的请求顺序如下: 网民电脑ip->代理服务器1–>代理服务器2–>目标服务器 REMOTE_ADDR:代理服务器2的IP值 X-Forwarded-For就是:网民电脑IP,代理1的IP,代理2的IP 在这里只有REMOTE_ADDR是可信的,其他从客户端获取的数据都是不可信的,都是可伪造的。下面简单示例下一个篡改X-Forwarded-For的情况:[table=98%,none] 1 2 3 4 5 6 7 8 9 10[color=rgb(248, 248, 242) !important]GET[color=rgb(0, 111, 224) !important] /[color=rgb(0, 111, 224) !important] HTTP/[color=rgb(174, 129, 255) !important]1.1 Host:[color=rgb(0, 111, 224) !important] www.myip.[color=rgb(249, 38, 80) !important]cn Cache-Control:[color=rgb(0, 111, 224) !important] max-age=[color=rgb(174, 129, 255) !important]0 Upgrade-Insecure-Requests:[color=rgb(0, 111, 224) !important] [color=rgb(174, 129, 255) !important]1 User-Agent:[color=rgb(0, 111, 224) !important] Mozilla/[color=rgb(174, 129, 255) !important]5.0[color=rgb(0, 111, 224) !important] ([color=rgb(249, 38, 80) !important]Windows NT[color=rgb(0, 111, 224) !important] [color=rgb(174, 129, 255) !important]6.1;[color=rgb(0, 111, 224) !important] Win64;[color=rgb(0, 111, 224) !important] x64)[color=rgb(0, 111, 224) !important] **WebKit/[color=rgb(174, 129, 255) !important]537.36[color=rgb(0, 111, 224) !important] (KHTML,[color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]like Gecko)[color=rgb(0, 111, 224) !important] Chrome/[color=rgb(174, 129, 255) !important]51.0.2704.106[color=rgb(0, 111, 224) !important] Safari/[color=rgb(174, 129, 255) !important]537.36 Accept:[color=rgb(0, 111, 224) !important] text/html,application/xhtml+xml,application/xml;q=[color=rgb(174, 129, 255) !important]0.9,image/webp,*/*;q=[color=rgb(174, 129, 255) !important]0.8 Accept-Encoding:[color=rgb(0, 111, 224) !important] gzip,[color=rgb(0, 111, 224) !important] deflate,[color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]sdch Accept-Language:[color=rgb(0, 111, 224) !important] en-US,en;q=[color=rgb(174, 129, 255) !important]0.8,zh-CN;q=[color=rgb(174, 129, 255) !important]0.6,zh;q=[color=rgb(174, 129, 255) !important]0.4,zh-TW;q=[color=rgb(174, 129, 255) !important]0.2 Cookie:[color=rgb(0, 111, 224) !important] Hm_lvt_380ffd3c2225d34ca2087c6970395366=[color=rgb(174, 129, 255) !important]1473755162;[color=rgb(0, 111, 224) !important] Hm_lpvt_380ffd3c2225d34ca2087c6970395366=[color=rgb(174, 129, 255) !important]1473755299;[color=rgb(0, 111, 224) !important] sc_is_visitor_unique=rx4067297.[color=rgb(174, 129, 255) !important]1473755300.43C8C2ACB3CA4FAAEB8885235516D36A.1.1.1.1.1.1.1.1.1 X-Forwarded-[color=rgb(102, 217, 239) !important]For:[color=rgb(0, 111, 224) !important] [color=rgb(174, 129, 255) !important]127.0.0.111111 返回信息是:[table=98%,none] 1[color=rgb(248, 248, 242) !important][color=rgb(249, 38, 80) !important]font style=[color=rgb(230, 219, 93) !important]"font-family:Arial,Helvetica,Sans Serif;font-size: 24pt;"[color=rgb(0, 111, 224) !important] color=[color=rgb(230, 219, 93) !important]"#0066CC">您的IP地址:[color=rgb(0, 111, 224) !important] [color=rgb(174, 129, 255) !important]127.0.0.111111 3 正确的代码示例在X-Forwarded-For信息头中可以提取真实的用户IP,但是这个IP是可以伪造的,如果从X-Forwarded-For提取IP作为用户的IP对于存在登录次数,api速率限制等一些接口是致命的缺陷,因为任意构造出无数的合法或者非法IP地址。而REMOTE_ADDR只是服务器前端的IP地址,如果没有代理就是用户的真实地址。这个是不可伪造的,而且代理是有限的,可以基于此来获取IP。在wordpress中,获取客户的IP地址代码如下:[table=98%,none] 1[color=rgb(248, 248, 242) !important]$remote_ip[color=rgb(0, 111, 224) !important] =[color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]preg_replace([color=rgb(0, 111, 224) !important] [color=rgb(230, 219, 93) !important]'/[^0-9a-fA-F:., ]/',[color=rgb(0, 111, 224) !important] [color=rgb(230, 219, 93) !important]'',[color=rgb(0, 111, 224) !important] $_SERVER[[color=rgb(230, 219, 93) !important]'REMOTE_ADDR'[color=rgb(0, 111, 224) !important] ); 如果是python代码的话:remote_ip = request.META.get(‘REMOTE_ADDR’, None) 当然上述代码也存在缺陷,就是服务器端开了nginx反向代理的时候,每次获取的都是反向代理的IP,这不是我们的预期,需要nginx在配置反向代理的时候做一定设置并且修改代码。如:[table=98%,none] 1 2 3[color=rgb(248, 248, 242) !important][color=rgb(249, 38, 80) !important]proxy_set_header Host[color=rgb(0, 111, 224) !important] $host; proxy_set_header[color=rgb(0, 111, 224) !important] X-Real-IP[color=rgb(0, 111, 224) !important] $remote_addr; proxy_set_header[color=rgb(0, 111, 224) !important] X-Forwarded-[color=rgb(102, 217, 239) !important]For[color=rgb(0, 111, 224) !important] $proxy_add_x_forwarded_for; 或者采用realip模块,配置如下:[table=98%,none] 1 2[color=rgb(248, 248, 242) !important]set_real_ip_from[color=rgb(0, 111, 224) !important] [color=rgb(174, 129, 255) !important]10.1.10.0/[color=rgb(174, 129, 255) !important]24; real_ip_header[color=rgb(0, 111, 224) !important] X-Forwarded-[color=rgb(102, 217, 239) !important]For; 在存在反向代理的情况下,如果直接获取REMOTE_ADDR,得到的是反向代理IP的值,从上面的配置也可以看出,在反向代理nginx的配置中将REMOTE_ADDR赋给了X-Real-IP,那么也是从X-Real-IP中来获取用户的IP,如下才是正确的获取用户IP的方式:[table=98%,none] 1 2 3 4 5[color=rgb(248, 248, 242) !important][color=rgb(249, 38, 80) !important]def [color=rgb(249, 38, 80) !important]get_ip(request): [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]try: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]return[color=rgb(0, 111, 224) !important] request.META[[color=rgb(230, 219, 93) !important]'HTTP_X_REAL_IP' [color=rgb(0, 111, 224) !important] [color=rgb(249, 38, 80) !important]except KeyError: [color=rgb(0, 111, 224) !important] [color=rgb(102, 217, 239) !important]return[color=rgb(0, 111, 224) !important] request.META.[color=rgb(249, 38, 80) !important]get([color=rgb(230, 219, 93) !important]'REMOTE_ADDR',None) 4 总结 X-Forwarded-For可被用户伪造,不应该被信任;REMOTE_ADDR是使用“REMOTE_ADDR”机器的前一个建立tcp连接的机器的地址,是不可伪造的,在无代理时可以理解为用户的IP地址,有反向代理时,先将REMOTE_ADDR赋给X-Real-IP,最后可以从X-Real-IP中获取用户的IP。参考文献: http://gong1208.iteye.com/blog/1559835 http://devco.re/blog/2014/06/19/client-ip-detection/ http://blog.pengqi.me/2013/04/20/remote-addr-and-x-forwarded-for/如果您需要了解更多内容,可以 加入QQ群:486207500、570982169 直接询问:010-68438880-8669文章分类: 安全分享 文章关键词: api接口, X-Forwarded-For REMOTE_ADDR 区别, 服务器获取IP, 用户伪造, 直接获取REMOTE_ADDR, 获取用户的IP, 获取用户真实IP, 认证绕过 转载请注明:“转自绿盟科技博客”: 原文链接.
  • LB环境搭建
    本帖最后由 小白 于 2017-9-20 16:22 编辑1 环境搭建1.1 LB服务器测试环境中LB与CMS一般搭建在一台主机上。 1. 获取Nginx和PHP的安装包,SVN地址如下:https://dggsvn08-ex:3690/svn/AS_RBT_E_SVN/trunk/版本管理/云点社区项目/doc/云点社区相关资料/01_环境搭建/1.相关软件包/nginx+php 相关配置文件在config目录下 2. 创建LB用户groupadd -r LBuseradd -r -M -g LB -s/sbin/nologin LB3. 使用ROOT用户安装LB的nginx以root用户登录服务器,将app.tar.gz解包到/usr/local/目录,复制nginx+php\config\nginx\LB目录下conf.tar.gz到/usr/local/app/nginx下解压覆盖,复制config/nginx/LB下的nginx.conf配置文件到安装目录下替换,
总条数:276 到第 页
上滑加载中