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;    }}

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;    }}

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;    }}

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;    }}

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;    }}


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;


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

在后端服务器的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'; 


以上即可记录访客的实在ip地址

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...