NGINX 正向代理,请求报文返回值200,NGINX error.log 显示http_code 400

报错信息:

(nginx cannot assign requested adddress) while connecting to upstream

具体情况:

1. nginx 正向代理:内网网关过NGINX正向代理访问公网
2. 502 Bad gateway:浏览器页面报错
3. NGINX error.log:报错日志输出为 “(cannot assign requested adddress )while connecting to upstream”
4. 代理服务器为共享服务器,上面已有一户使用代理,进程数开到了最大,系统参数未做增设,共享服务器无法重启生效。
5. 系统: CentOS 7.X / 32核CPU 测试环境
6. NGINX版本:1.14.X

愁啊愁啊,看了N多博客

1. Nginx 性能调优
2. TCP 的那些事儿(上)
3. 有关 ab 压力测试的一个奇怪的现象
4. nginx-proxy-connect-to-ip80-failed-99-cannot-assign-requested-address
5. Linux 如何查看修改DNS配置
6. Nginx正向代理
7. nginx 正向代理配置文件示例
8. Nginx之——正向代理
9. 301、404、200、304、500等HTTP状态,代表什么意思?
10. HTTP请求报文和HTTP响应报文
…此处省略一万字

看了那么多博客,折腾很久,最后还是请教大佬,解决了

其实这个报400的问题已经有人写到博客里了,硬生生是问题解决了才被搜索到,已哭晕在厕所

参考文档:Nginx配置正向代理支持HTTP和HTTPS转发
在这里插入图片描述

原因:

1. 我的后端访问的公网地址是https,而nginx默认不支持

2. 我的NGINX编译模块未添加,支持HTTPS的正向代理第三方模块

3. 我的server配置也是一塌糊涂…

解决思路:

第一步: 找到需要添加的第三方模块,编译安装好它:

① ngx_http_proxy_connect_module-master(正向代理HTTPS模块)
② nginx_upstream_check_module-master(NGINX健康检查模块,这个是临时加上的)

关键配置

$ cd nginx-1.9.2/
$ patch -p1 < /path/to/ngx_http_proxy_connect_module/patch/proxy_connect.patch
$ ./configure --add-module=/path/to/ngx_http_proxy_connect_module

当然这里,你不能只编译一个第三方模块,你后面还要加很多你需要加的模块或路径之类的东西

参考链接:chobits / ngx_http_proxy_connect_module
关键信息1:

注意这里 “-p1”,没有空格,减p1,三个字符连在一起;还有就是再加一个也是用 “-p1”,而不是“-p2”
在这里插入图片描述
关键信息2:
不同版本需要指定的补丁之类的信息不同
在这里插入图片描述
关键信息3:
那个“/path/to/” 就是你的第三方模块放在服务器上的存放路径,如果你的是“/home/test/models/”,那你的路径就是这个,而不是“/home/test/models/to/”
关键信息4:
如果你的nginx安装文件已经使用多次,而且前面不知对它干了些啥,主要是你的 “patch -p1 < ......”都不知道指定错误或什么乱七八糟的东西时,你需要果断 “rm -rf 你的nginx安装文件” ,选择重零开始,用新的安装文件给它指定正确唯一的路径,然后你后面的编译才会没有莫名其妙的各种五花八门的报错信息出现

第二步: 写好正确的配置

worker_processes auto;
worker_rlimit_nofile 65534; 

events { 
    worker_connections 65534;
    multi_accept on; 
    use epoll; 
    }



http { 
    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 

    
    keepalive_timeout 65; 
    client_header_timeout 10; 
    client_body_timeout 10; 
    reset_timedout_connection on; 
    send_timeout 10; 
    limit_conn_zone $binary_remote_addr zone=addr:5m; 
    limit_conn addr 100;
    
    include mime.types; 
    server_tokens off;      
    charset UTF-8; 

    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on; 
    
	gzip on; 

	log_format access '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer"'
'"$http_user_agent" $http_x_forwarded_for';  

    #include /home2/nginx_model/conf.d/*.conf; 
    #include /home2/nginx_model/sites-enabled/*; 




    server {

    	listen                         7070;
		#必须要配置dns,多个就用空格隔开即可;查看dns: 'cat /etc/resolv.conf ' 
    	resolver                       x.x.x.x x.x.x.x;
		resolver_timeout 5s;

    	proxy_connect;
    	proxy_connect_allow            443 563;
    	proxy_connect_connect_timeout  10s;
    	proxy_connect_read_timeout     10s;
    	proxy_connect_send_timeout     10s;

        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 301 1h;
        proxy_cache_valid any 1m;
        proxy_connect_timeout 90;

        proxy_send_timeout 90; 
        proxy_read_timeout 90;

        proxy_buffer_size 4k; 
        proxy_buffers 4 32k; 
        proxy_busy_buffers_size 64k; 
        proxy_temp_file_write_size 64k;



        client_max_body_size 1000M;
        client_body_buffer_size 128k;

        access_log /home2/test/logs/http_proxy.access.log main;
        error_log /home2/test/logs/http_proxy.error.log  debug;


    	location / {
        	proxy_pass http://$host;
        	proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
        	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    	
		}

		location /nginx_status {
            stub_status on;
            access_log off;
            auth_basic_user_file conf/.nginx_status;
            allow 127.0.0.1;        
            deny all;
        }

	}
		      
}
大佬丢给我的配置:
server {
    listen                         3128;
    resolver                       8.8.8.8;
    proxy_connect;
    proxy_connect_allow            443 563;
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;
        access_log /opt/tengine/logs/http_proxy.access.log main;
        error_log /opt/tengine/logs/http_proxy.error.log;
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;
    }
}

还有就是:

浏览器打开NGINX状态页面:502 Bad gateway

不用管,正向代理这样报错才对

在这里插入图片描述

第三步:推数

不再是“code 400”,而是 “code 500” 才对,大佬不愧是大佬

# 网关机器上或在你的后端机器上
curl -x    nginx正向代理服务器ip:正向代理端口  https://后端要去访问的公网地址/

说明:

  1. 至于上述设计的配置及细节原因就不做一一展开了,有兴趣自行百度,为毛要添那么多,名曰 “NGINX 优化”
  2. 系统参数部分,最好一开始就开到做大,不然后面东西多了或和别人共用机器,可能不好重启机器,系统参数部分,后面有时间再补充,睡觉