• nginx配置详解


    1.nginx中配置(获取用户真实ip):

    在这里插入图片描述

    
    #可在nginx中正则获取第一个放进请求头中
    set $Real $proxy_add_x_forwarded_for;   
     if ( $Real ~ (\d+)\.(\d+)\.(\d+)\.(\d+),(.*) ){    
         set $Real $1.$2.$3.$4;    
         }
       proxy_set_header  X-Real-IP  $remote_addr;  #获取上一级代理的ip
       proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for; #如果有多级就获取第一个(用户》运营商(ADSL)》CDN》Nginx)
       eg:
       location / {   
        proxy_set_header            Host $host; 
        proxy_set_header            X-real-ip $remote_addr; 
        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.”坑“,项目上线深深上了一课,研究了一晚解决将项目部署到线上token取不到的BUG,原因是nginx转发携带请求头字段是不支持有’_‘

    location / {   
     proxy_set_header            Host $host;
     proxy_set_header            Author_token $remote_addr;#自定义header头无效    
     proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    解决办法:nginx underscores_in_headers默认off 忽略了下划线 也可以使用减号代替 我是在后端将token放请求头中,nginx找不到

    3. proxy_set_header中 p r o x y h o s t , proxy_host, proxyhost,host,$http_host的区别,客户端和nginx请求头中都有同一个字段,则选择客户端带的

    ``
    在使用Nginx做反向代理的时候,proxy_set_header功能可以设置反向代理后的http header中的host, h t t p h o s t , http_host, httphost,proxy_host,那么这几个有什么区别呢?Nginx的官网文档中说下面这两条是做反代时默认的,所以$proxy_host 自然是 proxy_pass后面跟着的host了proxy_set_header Host p r o x y h o s t ; p r o x y s e t h e a d e r C o n n e c t i o n c l o s e ; 如果客户端发过来的请求的 h e a d e r 中有’ H O S T ’这个字段时, proxy_host;proxy_set_header Connection close;如果客户端发过来的请求的header中有’HOST’这个字段时, proxyhost;proxysetheaderConnectionclose;如果客户端发过来的请求的header中有HOST这个字段时,http_host和 h o s t 都是原始的’ H O S T ’字段比如请求的时候 H O S T 的值是 w w w . c s d n . n e t 那么反代后还是 w w w . c s d n . n e t 如果客户端发过来的请求的 h e a d e r 中没有有’ H O S T ’这个字段时,建议使用 host都是原始的’HOST’字段比如请求的时候HOST的值是www.csdn.net 那么反代后还是www.csdn.net如果客户端发过来的请求的header中没有有’HOST’这个字段时,建议使用 host都是原始的HOST字段比如请求的时候HOST的值是www.csdn.net那么反代后还是www.csdn.net如果客户端发过来的请求的header中没有有HOST这个字段时,建议使用host,这表示请求中的server name。

    
    ### 4.proxy_set_header配置实例
    
    ```bash
    windows客户端(请求web服务):192.168.1.1
    nginx作为反向代理服务器:192.168.1.136
    nginx作为后端web服务器:192.168.1.137
    前提条件:配置nginx转发到后端服务器
    server {   
      listen 8080;    
      server_name 192.168.1.136;
       location / {     
             root "/www/html";     
             index index.html;     
             #auth_basic "required auth";     
             #auth_basic_user_file "/usr/local/nginx/users/.htpasswd";     
             error_page 404 /404.html;
             }
       location /images/ {    
             root "/www";     
             rewrite ^/images/bbs/(.*\.jpeg)$ /images/$1 break;     
             rewrite ^/images/www/(.*)$ http://192.168.1.136/$1 redirect;
             }
       location /basic_status {     
             stub_status;
             }
       location ^~/proxy_path/ { 
             root "/www/html";     
             index index.html;     
             proxy_pass http://192.168.1.137/;     
             proxy_set_header Host $host;     
             proxy_set_header X-Real-IP $remote_addr;     
             #proxy_set_header X-Forwarded-For $remote_addr;     
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             }
        location ^~/proxy_path/ {
             root "/www/html";     
             index index.html;     
             proxy_pass http://192.168.1.137/;
             }
        }
    将左侧匹配到的/proxy_path/开头的url全部转发到后端服务器192.168.223.137
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    Nginx反向代理Tomcat访问报错400问题

    线上用nginx反向代理tomcat访问,配置完成后,直接访问tomcat完全正常,但是只要在nginx添加反向代理tomcat,访问nginx就会报错400。
    原因和解决办法:
    1)后端服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果nginx代理层不重写请求头中的host字段,将会导致请求失败,报400错误。
    解决办法:proxy_set_header Host $http_host;
    2)nginx配置中header头部信息的host不能被配置重了。tomcat没有对headers中的host进行唯一校验。
    解决办法(下面两个要去掉一个):proxy_set_header Host $host;proxy_set_header Host $http_host; #去掉这一行
    location写法:

    (location =) > (location 完整路径) > (location ^~ 路径) > (location ,* 正则顺序) > (location 部分起始路径) > (/)

    以 = 开头,表示精确匹配;如只匹配根目录结尾的请求,后面不能带任何字符串。

    以^~ 开头,表示uri以某个常规字符串开头,不是正则匹配

    以~ 开头,表示区分大小写的正则匹配;

    以~* 开头,表示不区分大小写的正则匹配

    以/ 开头,通用匹配, 如果没有其它匹配,任何请求都会匹配到

  • 相关阅读:
    JavaScript设计模式(三)——单例模式、装饰器模式、适配器模式
    开源的房地产行业营销拓客系统,值得推荐!
    轻量级的Linux发行版:4MLinux稳定版发布
    聚水潭对接金蝶云星空数据方案
    GO -mod和​GO111MODULE设置的那些事原创
    C++学习笔记---命名空间namespace
    Java设计模式-工厂模式(FactoryPattern)
    队列--环形数组实现
    【对比学习】CUT模型论文解读与NCE loss代码解析
    IP地址查询
  • 原文地址:https://blog.csdn.net/qq_46077249/article/details/133350005