1、proxy_set_header

1.1、$http_host与$host区别

1、在使用Nginx做反向代理的时候,proxy_set_header功能可以设置反向代理后的http header中的host,
那么常用的几个设置中$proxy_host, $host,$http_host又都表示什么意思呢?
Nginx的官网文档中说下面这两条是做反代时默认的,所以$proxy_host 自然是 proxy_pass后面跟着的host了

proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;

如果客户端发过来的请求的header中有’HOST’这个字段时,
$http_host和$host都是原始的’HOST’字段
比如请求的时候HOST的值是www.csdn.net 那么反代后还是www.csdn.ne

1.1.1、 不设置 proxy_set_header Host 时

浏览器直接访问 nginx,获取到的 Host 是 proxy_pass 后面的值,即 $proxy_host 的值,参考 http://nginx.org/en/docs/http...

server {
    listen 8090;
    server_name _;
    location / {
        proxy_pass http://172.31.5.0:5000;
    }
}

image.png

1.1.2、 设置 proxy_set_header Host $host 时

浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息

server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://172.31.5.0:5000;
    }
}

image.png

1.1.3、2.3 设置 proxy_set_header Host $host:$proxy_port 时

浏览器直接访问 nginx,获取到的 Host 是 $host:$proxy_port 的值

server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $host:$proxy_port;
        proxy_pass http://172.31.5.0:5000;
    }
}

image.png

1.1.4、2.4 设置 proxy_set_header Host $http_host 时

浏览器直接访问 nginx,获取到的 Host 包含浏览器请求的 IP 和端口

server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://172.31.5.0:5000;
    }
}

image.png

1.1.5、2.5 设置 proxy_set_header Host $host 时

浏览器直接访问 nginx,获取到的 Host 是 $host 的值,没有端口信息。此时代码中如果有重定向路由,那么重定向时就会丢失端口信息,导致 404

server {
    listen 8090;
    server_name _;
    location / {
        proxy_set_header Host $host;
        proxy_pass http://172.31.5.0:5000;
    }
}

image.png
image.png

1.1.6、X-Real-IP

下面我们看一下有多级代理存在时如何获取客户端真实IP.

首先要明确在header里面的 X-Real-IP只是一个变量,后面的设置会覆盖前面的设置(跟X-Forwarded-For的追加特性区别明显),所以我们一般只在第一个代理设置proxy_set_header X-Real-IP $remote_addr;就好了,然后再应用端直接引用$http_x_real_ip就行.
参考:https://blog.csdn.net/xiaoxia...

2、服务器使用nginx反向代理后,后端服务器获取访客的真实ip的失败

公司目前使用的业务是前端使用nginx做为反向代理,后端使用nginx作为web服务器,由于前期没有配置,导致后端服务器记录的访问日志的ip全部是来着前端反向代理服务器的。
如果需要后端服务器记录访客真是ip。需要进行如下配置:

一、配置反向代理端的nginx服务器

在server后面增加如下这三个参数用于记录IP:

  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

image.png
二、在后端服务器配置如下

在后端服务器的nginx_http处配置如下:
配置log_format信息, 后续的日志后面也需要加上main这个参数

log_format  main '$remote_addr $remote_user [$time_local] "$request" '           
                  '$status $body_bytes_sent "$http_referer" '                   
                  '$http_user_agent $http_x_forwarded_for $request_time $upstream_response_time $upstream_addr $upstream_status'; 

image.png
以上即可记录访客的真实ip地址

image.png

2.2、其他配置参数


`参数                      说明                                         示例`

`$remote_addr             客户端地址                                    211.28.65.253`

`$remote_user             客户端用户名称                                --`

`$time_local              访问时间和时区                                18``/Jul/2012``:17:00:01 +0800`

`$request                 请求的URI和HTTP协议                           ``"GET /article-10000.html HTTP/1.1"`

`$http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100`

`$status                  HTTP请求状态                                  200`

`$upstream_status         upstream状态                                  200`

`$body_bytes_sent         发送给客户端文件内容大小                        1547`

`$http_referer            url跳转来源                                   https:``//www``.baidu.com/`

`$http_user_agent         用户终端浏览器等信息                           "Mozilla``/4``.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident``/4``.0; SV1; GTB7.0; .NET4.0C;`

`$ssl_protocol            SSL协议版本                                   TLSv1`

`$ssl_cipher              交换数据中的算法                               RC4-SHA`

`$upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80`

`$request_time            整个请求的总时间                               0.205`

`$upstream_response_time  请求过程中,upstream响应时间                    0.002`

3、获取多有请求头

  ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
 HttpServletRequest request = getRequest();
//获取所有请求头名称
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = request.getHeader(name);
            System.out.println(name+"---"+value);
        }

4、nginx.conf配置

性能优化-开启高效文件传输模式sendfile on;
sendfile on; #特殊的数据传输功能

tcp_nopush on;

参数sendfile on 用于开启文件高效传输模式,同时将tcp_nopush on 和tcp_nodelay on 两个指令设置为on,可防止网络及磁盘I/O阻塞,提升Nginx工作效率

(1) 设置参数 sendfile on
参数语法 sendfile on | off;

放置位置 http,server,location,if in location

(2) 设置参数 tcp_nopush on 说明:当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
参数语法 tcp_nopush on | off;

放置位置 http,server,location
(3) 设置参数 tcp_nodelay on 说明:有时要抓紧发货, 确保数据尽快发送, 提高可数据传输效率
参数语法 tcp_nodelay on | off;

放置位置 http,server,location
sendfile on配合使用(2)(3) 但(2)(3)只能选其一特别注意


在主配置文件nginx.conf中配置


worker_processes  2;
worker_cpu_affinity 0101 1010;
error_log logs/error.log;
 
#配置Nginx worker进程最大打开文件数
worker_rlimit_nofile 65535;
 
user www www;
events {
    #单个进程允许的客户端最大连接数
    worker_connections  20480;
    #使用epoll模型
    use epoll;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #sendfile        on;
    keepalive_timeout  65;
    #访问日志配置
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
 
    #虚拟主机
    include /application/nginx/conf/extra/www.conf;
    include /application/nginx/conf/extra/blog.conf;
    include /application/nginx/conf/extra/bbs.conf;
    include /application/nginx/conf/extra/edu.conf;
    include /application/nginx/conf/extra/phpmyadmin.conf;
    include /application/nginx/conf/extra/status.conf;
 
    #nginx优化----------------------
    #隐藏版本号
    server_tokens on;
 
    #优化服务器域名的散列表大小 
    server_names_hash_bucket_size 64;
    server_names_hash_max_size 2048;
 
    #开启高效文件传输模式
    sendfile on;
    #减少网络报文段数量
    #tcp_nopush on;
    #提高I/O性能
    tcp_nodelay on;
}

参考:
1)https://blog.csdn.net/u011897...
2) https://www.cnblogs.com/faber...
3) http://nginx.org/en/docs/http...
4)https://blog.csdn.net/diyiday...
5)https://juejin.cn/post/700363...
6)https://blog.csdn.net/weixin_...
7)https://blog.csdn.net/xiaoxia...


努力的康康呀
1 声望2 粉丝