• nginx的常用配置


    nginx通用配置

    http {
        # nginx负载均衡配置
        upstream dynamic_balance {
            #ip_hash;
            server 192.168.100.123: 8081;
        }
        # 省略其他
        server {
            listen 80;
            server_name localhost;
            #访问工程路径
            root website;
            index index.html index.htm;
            
            #转发把原http请求的Header中的Host字段也放到转发的请求
            proxy_set_header Host $host;
            #获取用户真实IP
            proxy_set_header X - real - ip $remote_addr;
            proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
            
            #接口转发
            location /base/ {
                proxy_pass http: //dynamic_balance; 
            }
            
            #启用history模式( 什么请求都只返回index.html)
            location / {
                try_files $uri $uri / /index.html;
            }
        }
    }

    nginx配置proxy_set_header Host $host的作用?

    没有配置 proxy_set_header Host $host 等参数, 请求总是报 400(bad request).

    proxy_set_header Host $host这一行的作用是把原http请求的Header中的Host字段也放到转发的请求里。

    如果不加这一行的话,nginx转发的请求header里就不会有Host字段,而服务器是靠这个Host值来区分你请求的是哪个域名的资源的。

    问题地址:https://segmentfault.com/q/1010000018619879

    使用nginx后如何在web应用中获取用户ip

    在实际应用中,我们可能需要获取用户的ip地址,比如做异地登陆的判断,或者统计ip访问次数等,通常情况下我们使用request.getRemoteAddr()就可以获取到客户端ip,但是当我们使用了nginx作为反向代理后,使用request.getRemoteAddr()获取到的就一直是nginx服务器的ip的地址,那这时应该怎么办?

    解决方案:在nginx配置中配置proxy_set_header X-real-ip $remote_addr;

    其中这个X-real-ip是一个自定义的变量名,名字可以随意取,这样做完之后,用户的真实ip就被放在X-real-ip这个变量里了.web端可以这样获取:request.getAttribute("X-real-ip")

    方案二: 配置里面这样设置:

    proxy_set_heade  X-Forwarded-For $proxy_add_x_forwarded_for;
  • 相关阅读:
    @property @synthesize的含义以及误区。
    OC中类的扩展介绍
    Thinking in Java第三、四章学习笔记----操作符与控制执行流程
    LeetCode之Easy篇 ——(12)Integer to Roman
    LeetCode之Easy篇 ——(9)Palindrome Number
    LeetCode之Easy篇 ——(7)Reverse Integer
    LeetCode之Easy篇 ——(1)Two Sum
    Java关键字汇总
    Thinking in Java 第二章学习笔记----一切都是对象
    php文件上传原理详解
  • 原文地址:https://www.cnblogs.com/moqiutao/p/14620243.html
Copyright © 2020-2023  润新知