nginx多层反向代理获取客户端真实ip
访问路径:
用户 --> www.chinasoft.cn(nginx反向代理) --> www.chinasoft.com(nginx反向代理) --> python服务端程序 经过多层代理 第一层代理:
# cat /usr/local/nginx/conf/vhost.d/www.chinasoft.cn.conf
server {
listen ;
server_name www.chinasoft.cn chinasoft.cn;
access_log /data/www/logs/nginx_log/access/chinasoft.cn_access.log main ;
error_log /data/www/logs/nginx_log/error/chinasoft.cn_error.log ;
#root /data/www/vhosts/chinasoft.cn/httpdocs ;
index index.html index.shtml index.php ;
#include rewrite.d/chinasoft.cn.conf ;
error_page /.html; rewrite ^/(.*)$ https://www.chinasoft.cn/$1 permanent; #跳转到Https location /favicon.ico{
proxy_pass https://www.chinasoft.com;
} location ~ ^/(middle|app|files|static|back)/ {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_cookie_domain www.chinasoft.com www.chinasoft.cn;
proxy_pass https://www.chinasoft.com;
} location /cn {
rewrite ^/cn/(.*) /$ permanent;
} #注释原来的location
#location / {
# proxy_cookie_domain www.chinasoft.com www.chinasoft.cn;
# proxy_pass https://www.chinasoft.com/cn/;
#} #开启新的配置
location / {
if (-d $request_filename){
rewrite (.*) $ break;
}
if (-f $request_filename.html){
rewrite (.*) $.html break;
}
try_files $uri /index.html @;
} } server {
listen ;
server_name www.chinasoft.cn chinasoft.cn; ssl on;
ssl_certificate /usr/local/nginx/cert/geo-chinasoft.cn.crt;
ssl_certificate_key /usr/local/nginx/cert/geo-chinasoft.cn.key; ssl_protocols TLSv1 TLSv1. TLSv1.; ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AE
S256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!AES128-GCM-SHA256:!AES256-GCM-SHA384:!AES128-SHA256:!AES256-SHA256:!AES128-SHA:!AES256-SHA:AES:!CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:EDH-RSA-DES-CBC3
-SHA:!KRB5-DES-CBC3-SHA";
ssl_prefer_server_ciphers on;
ssl_session_timeout 10m; access_log /data/www/logs/nginx_log/access/chinasoft.cn_access.log main ;
error_log /data/www/logs/nginx_log/error/chinasoft.cn_error.log ;
root /data/www/vhosts/chinasoft/chinasoft_web_html/converter_middle/templates/cn;
index index.html index.shtml index.php ;
#include rewrite.d/chinasoft.cn.conf ;
error_page /.html; location /favicon.ico{
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass https://www.chinasoft.com;
} location ~ ^/(middle|app|files|back)/ {
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cookie_domain www.chinasoft.com www.chinasoft.cn;
proxy_pass https://www.chinasoft.com;
} location /cn {
rewrite ^/cn/(.*) /$ permanent;
} location /static {
root /data/www/vhosts/chinasoft/chinasoft_web_html/converter_middle;
} #注释原来的location
#location / {
# proxy_cookie_domain www.chinasoft.com www.chinasoft.cn;
# proxy_pass https://www.chinasoft.com/cn/;
#} #开启新的配置
location / {
if (-d $request_filename){
rewrite (.*) $ break;
}
if (-f $request_filename.html){
rewrite (.*) $.html break;
}
try_files $uri /index.html @;
} } 第二层代理:
[server02:~]# more /usr/local/nginx/conf/vhost.d/www.chinasoft.com.conf
server {
listen ;
server_name chinasoft.com www.chinasoft.com ;
access_log /data/www/logs/nginx_log/access/www.chinasoft.com_access.log main ;
error_log /data/www/logs/nginx_log/error/www.chinasoft.com_error.log;
root /data/www/vhosts/chinasoft/chinasoft_web/web;
index index.html index.php ;
include rewrite.d/chinasoft.com.conf ;
error_page /.html; location ^~ /middle/file/test-oss-callback {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header HTTP_AUTHORIZATION $http_authorization;
proxy_pass_header Server;
proxy_redirect off;
proxy_pass http://1.1.1.1:7980/middle/file/oss-callback;
} rewrite ^/(.*)$ https://www.chinasoft.com/$1 permanent; #跳转到Https } server {
listen ;
server_name www.chinasoft.com chinasoft.com; ssl on;
ssl_certificate /usr/local/nginx/conf/cert2016/chinasoft_com.crt;
ssl_certificate_key /usr/local/nginx/conf/cert2016/chinasoft_com.key;
ssl_dhparam /usr/local/nginx/conf/cert2016/dh_2048.pem; ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1. TLSv1.; ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AE
S256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!AES128-GCM-SHA256:!AES256-GCM-SHA384:!AES128-SHA256:!AES256-SHA256:!AES128-SHA:!AES256-SHA:AES:!CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:EDH-RSA-DES-CBC3
-SHA:!KRB5-DES-CBC3-SHA";
ssl_prefer_server_ciphers on; gzip on;
gzip_min_length 1k;
gzip_buffers 16k;
gzip_comp_level ;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript; access_log /data/www/logs/nginx_log/access/www.chinasoft.com_access.log main ;
error_log /data/www/logs/nginx_log/error/www.chinasoft.com_error.log ;
root /data/www/vhosts/chinasoft/chinasoft_web/web;
index index.html index.php ; include rewrite.d/chinasoft.com.conf ;
error_page @error404;
location /cn { include rewrite.d/chinasoft.cn.conf ; } location @error404 {
rewrite ^/(fr|de|it|es|pt|nl|hi|jp|ru|kr|id|ar|cn) /$/.html last;
rewrite ^ /.html last;
}
location ~ /(fr|de|it|es|pt|nl|hi|jp|ru|kr|id|ar|vn|tr|th|ro|zh-tw|cn)$ {
rewrite ^/(.*)$ /$/ permanent;
} location ^~ /middle/file/test-oss-callback {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header HTTP_AUTHORIZATION $http_authorization;
proxy_pass_header Server;
proxy_redirect off;
proxy_pass http://127.0.0.1:7980/middle/file/test-oss-callback;
} location ~ ^/(middle|app)/ {
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header REMOTE-HOST $remote_addr;
#proxy_set_header HTTP_AUTHORIZATION $http_authorization;
#proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
expires 1d;
include proxy_params;
if (!-d $request_filename){
set $flag $flag;
}
if (!-f $request_filename){
set $flag $flag;
}
if ($flag = ""){
rewrite ^(.*)$ /index.php last;
}
} location ~ \.php$ {
#fastcgi_pass 127.0.0.1:;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_read_timeout ;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
expires -;
}
location /static {
root /data/www/vhosts/chinasoft/chinasoft_web_html/converter_middle;
}
location / {
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header REMOTE-HOST $remote_addr;
#proxy_set_header HTTP_AUTHORIZATION $http_authorization;
#proxy_pass_header Server; proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr; expires -10d;
add_header Cache-Control no-cache;
root /data/www/vhosts/chinasoft/chinasoft_web_html/converter_middle/templates;
index index.html;
if (-d $request_filename){
rewrite (.*) $ break;
}
if (!-f $request_filename){
rewrite (.*) $.html break;
}
try_files $uri /index.html @error404;
} } nginx多层代理获取客户端的真实ip总结: 、编译Nginx时,添加http_realip_module模块 、在nginx.conf文件中 proxy_pass xxxxxx添加下面三行
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 、在每一层nginx日志中的打印的"$http_x_forwarded_for"就是真实客户端的ip地址。
、后台服务器获取真实的客户端ip地址: headers中的X-Forwarded-For选项中逗号前第一个ip就是真实客户端ip 日志中获取真实ip: $http_x_forwarded_for 就是获取真实ip的变量 log_format main '$remote_addr $http_x_forwarded_for - - [$time_local] - - "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time ';
# more /usr/local/nginx/conf/rewrite.d/chinsoft.com.conf
if ($request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /$1 permanent;}
nginx多层反向代理获取客户端真实ip的更多相关文章
- nginx 多级反向代理获取客户端真实IP
set_real_ip_from ; set_real_ip_from ; set_real_ip_from ; set_real_ip_from ; set_real_ip_from 127.0.0 ...
- 关于nginx多层uptstream转发获取客户端真实IP的问题
因为公司有个需求需要获取客户端的真实IP,前端是haproxy,后面是nginx,本来这个需求不难完成,但是难就难在是https请求也就是ssl 由于个人水平有限,在网上爬了很多资料,刚开始的ha是通 ...
- nginx反向代理获取用户真实ip
nginx做反向代理时,默认的配置后端获取到的ip都是来自于nginx,如何转发用户的真实ip到后端程序呢?如是是java后端,用request.getRemoteAddr();获取到的是nginx的 ...
- 关于.net core使用nginx做反向代理获取客户端ip的问题
1.正常情况下.net core获取客户端ip是比较简单的 /// <summary> /// 获取客户Ip /// </summary> /// <param name ...
- 如何取得nginx做反向代理时的真实IP?
1. 编译 对于client -> nginx reverse proxy -> apache, 要想在程序中取得真实的IP,在执行nginx的configure时,必须指定参数" ...
- Nginx反向代理后应用程序获取客户端真实IP
Nginx反向代理后,Servlet应用通过request.getRemoteAddr()取到的IP是Nginx的IP地址,并非客户端真实IP,通过request.getRequestURL()获取的 ...
- nginx 代理模式下,获取客户端真实IP
最近做博友推荐,发现个小问题,用$_SERVER['REMOTE_ADDR'];得到的都是服务器的地址192.168.96.52,搜索了一下,发现问题,改为$_SERVER['HTTP_X_REAL_ ...
- 多层代理获取用户真实IP
1. 几个概念remote_addr:如果中间没有代理,这个就是客户端的真实IP,如果有代理,这就是上层代理的IP.X-Forwarded-For:一个HTTP扩展头,格式为 X-Forwarded- ...
- 获取客户端真实IP地址
Java-Web获取客户端真实IP: 发生的场景:服务器端接收客户端请求的时候,一般需要进行签名验证,客户端IP限定等情况,在进行客户端IP限定的时候,需要首先获取该真实的IP. 一般分为两种情况: ...
随机推荐
- CF: Long Number
题目链接 #include<iostream> #include<string> ...
- 1206 BBS注册
目录 昨日内容 BBS项目 1.项目开发流程 2.表设计 用户表 个人站点表 文章标签表 文章分类表 文章表 文章的点赞点踩表 文章的评论表 项目 昨日内容 昨日内容 基于django中间件实现功能的 ...
- 团队作业Beta冲刺--第二天
团队作业Beta冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件 ...
- tensorflow 运行效率 GPU memory leak 问题解决
问题描述: Tensorflow 训练时运行越来越慢,重启后又变好. 用的是Tensorflow-GPU 1.2版本,在GPU上跑,大概就是才开始训练的时候每个batch的时间很低,然后随着训练的推进 ...
- FailOver的机制
package util import ( "fmt" "hash/crc32" "math/rand" "sort" ...
- Tomcat 部署多个web项目
1.若Tomcat的端口设置为10000,则http://localhost:10000访问的目录是 webapps 2.Service.xml中host内配置Context标签,path+docba ...
- centos7.3 安装 mysql-5.7.13
系统环境: [root@localhost ~]# cat /etc/RedHat-release CentOS release 6.7 (Final)[root@localhost tools]# ...
- AJAX备忘
基础 AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJAX ...
- input file标签限制上传文件类型
用 input 的file类型标签上传文件,有时需要限制上传文件类型,添加accept属性可以实现 <input type="file" accept="image ...
- 洛谷P1162(自我感觉思路还算巧妙的一道题)
P1162 填涂颜色 题目描述 由数字0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向.现要求把闭合圈内的所有空间都填写成2.例如:6X6的方阵(n=6),涂色前和 ...