Nginx服务器---正向代理

Nginx服务器-----正向代理

文章目录


前言

今天在工作中遇到了nginx服务器宕机的问题,发现对于nginx服务器一直处于一知半解的状态,于是乎想简单将nginx服务器的一些知识记录一下。

一、nginx是什么?

【百度百科】
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

除了反向代理,nginx还支持正向代理、负载均衡以及基于SSL安全证书的HTTPS访问等功能特性~
本文主要是介绍是如何配置nginx正向代理、反向代理及负载均衡。

二、代理服务

1.什么是代理呢?下面一张图了解一下

在这里插入图片描述
其中,代理模式又分为正向代理反向代理

2.正向代理

2.1什么是正向代理呢?

正向代理,指的是通过代理服务器 代理浏览器/客户端去重定向请求访问到目标服务器 的一种代理服务。
正向代理服务的特点是代理服务器 代理的对象是浏览器/客户端,也就是对于目标服务器 来说浏览器/客户端是隐藏的。

在这里插入图片描述

2.2如何实现nginx正向代理

(1)正向代理转发https说明

在实现nginx正向代理之前,先说明一下,现在的网站基本上都是https,因此要实现nginx正向代理转发请求,除了要配置转发http80端口的请求,还有配置https443端口的请求~
正向代理转发http请求很简单,不过正向代理转发https请求稍微有一点麻烦,目前网上的教程大多都是如下这样配置的
在这里插入图片描述
一开始我也像上面那样配置,虽然http请求正常转发了,但是发现https并没有转成功,而是报错HTTP/1.1 400 Bad Request~
后来才了解到,nginx官方并不支持直接转发https请求,但是阿里的一个大佬对nginx拓展了一个ngx_http_proxy_connect_module模块,并且在github上开源了 https://github.com/chobits/ngx_http_proxy_connect_module
不过维护的ngx_http_proxy_connect_module模块的补丁也是有nginx版本限制的(目前维护了1.4.x~1.19.x版本)

(2)安装nginx

这里以1.9.2版本为例,使用root用户进行安装:

cd /usr/local
wget http://nginx.org/download/nginx-1.9.2.tar.gz
tar -xzvf nginx-1.9.2.tar.gz
mkdir nginx
mv nginx-1.9.2 nginx
cd nginx/nginx-1.9.2
make && make install

注意: 执行make && make install的时候,如遇到下面这种情况,说明nginx缺少依赖包

make: *** No rule to make target `build', needed by `default'. Stop.

解决方法:

yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
# 或者
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel

这里安装nginx通过install进行编译安装,编译后默认安装目录为/usr/local/nginx,后续配置新模块ngx_http_proxy_connect_module还需要重新install编译一次

(3)下载新模块

GitHub上下载ngx_http_proxy_connect_module的zip压缩包源码

(4)解压新模块源码

将新模块ngx_http_proxy_connect_module源码压缩包上传到服务器/usr/local/nginx目录,并解压并重命名

cd /usr/local/nginx
unzip ngx_http_proxy_connect_module-master.zip
mv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_module

(5)添加新模块到nginx

使用root用户进入nginx的资源目录/usr/local/nginx/nginx-1.9.2,给nginx添加新模块ngx_http_proxy_connect_module和并重新编译nginx

cd /usr/local/nginx/nginx-1.9.2
patch -p1 < /usr/local/nginx/ngx_http_proxy_connect_module/patch/proxy_connect.patch
./configure --add-module=/usr/local/nginx/ngx_http_proxy_connect_module
make && make install

说明: 这里nginx-1.9.2版本对应proxy_connect.patch补丁,其他版本相关补丁支持版本,详情见GitHub

(6)修改nginx的配置

修改nginx的配置分别添加http和https的server,其他配置保持不变

vim /usr/local/nginx/conf/nginx.conf

这两个server主要配置是对DNS解析和proxy_pass代理进行:

#正向代理转发http请求
server {
    #指定DNS服务器IP地址 
    resolver 114.114.114.114;
    #监听80端口,http默认端口80
    listen 80;
    #服务器IP或域名
    server_name  localhost;
	
    #正向代理转发http请求
    location / {
        proxy_pass                 http://$host$request_uri;
        proxy_set_header           HOST $host;
        proxy_buffers              256 4k;
        proxy_max_temp_file_size   0k;
        proxy_connect_timeout      30;
        proxy_send_timeout         60;
        proxy_read_timeout         60;
        proxy_next_upstream error  timeout invalid_header http_502;
    }
}

#正向代理转发https请求
server {
    #指定DNS服务器IP地址 
    resolver 114.114.114.114;
    #监听443端口,https默认端口443
    listen 443;
	
    #正向代理转发https请求
    proxy_connect;
    proxy_connect_allow            443 563;
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;
    }
}

DNS说明:
(国内外)目前比较主流的DNS:
(国外)谷歌:8.8.8.8 developers.google.com
(国外)OpenDNS:208.67.222.222 signup.opendns.com
(国内)114:114.114.114.114 www.114dns.com
(国内)腾讯:119.29.29.29 www.dnspod.cn
(国内)阿里:223.5.5.5 alidns.com
(国内)百度:180.76.76.76 dudns.baidu.com

(7)检查和刷新nginx配置

cd /usr/local/nginx/sbin
./nginx -t
./nginx -s reload

注意: 如果使用/usr/local/nginx/sbin/nginx -s reload 重新读取配置文件出错,出现如下代码

nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

解决办法:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

然后便可执行刷新nginx配置的命令了

2.3 验证正向代理

Linux下验证代理服务器是否能正常代理转发http和https请求

curl http://www.baidu.com/ -v -x 127.0.0.1:80
curl https://www.baidu.com/ -v -x 127.0.0.1:443

在这里插入图片描述
在这里插入图片描述

先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

猜你喜欢

转载自blog.csdn.net/m0_67401660/article/details/126077311