nginx配置详解

简介: 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;
    }

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

解决办法: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

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以某个常规字符串开头,不是正则匹配

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

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

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

目录
相关文章
|
7天前
|
负载均衡 应用服务中间件 API
Nginx:location配置模块的用法(一)
Nginx:location配置模块的用法(一)
51 2
|
4天前
|
Web App开发 应用服务中间件 网络安全
如何在 Apache 和 Nginx 上配置 OCSP Stapling
如何在 Apache 和 Nginx 上配置 OCSP Stapling
21 8
|
4天前
|
jenkins 应用服务中间件 持续交付
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
22 8
|
4天前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
10 4
|
3天前
|
负载均衡 应用服务中间件 网络安全
NGINX配置详解
NGINX配置详解
|
3天前
|
应用服务中间件 Linux nginx
高并发下Nginx配置限流
【8月更文挑战第16天】
9 1
|
4天前
|
负载均衡 前端开发 应用服务中间件
使用Nginx配置SSL以及部署前端项目
本文介绍了如何使用Nginx配置SSL证书以启用HTTPS,并展示了如何通过Nginx部署前端项目,包括配置SSL证书、设置代理和负载均衡的示例。
14 2
|
4天前
|
Ubuntu 搜索推荐 应用服务中间件
如何在 Ubuntu 14.04 上配置 Nginx 使用自定义错误页面
如何在 Ubuntu 14.04 上配置 Nginx 使用自定义错误页面
11 2
|
7天前
|
缓存 前端开发 应用服务中间件
Nginx:location配置模块的用法(二)
Nginx:location配置模块的用法(二)
13 2
|
7天前
|
缓存 运维 应用服务中间件
运维系列.Nginx配置中的高级指令和流程控制
运维系列.Nginx配置中的高级指令和流程控制
17 1