基本方案

在本节中,我们将在Debian Linux上使用nginx webserver设置基本反向代理。
我们将运行两个单独的服务器server1server2
Server1的IP地址10.1.1.251,并使用Nginx作为反向代理。
Server2在IP地址10.1.1.252,并运行Apache2 WebServer。

我们假设Server2已启动并正在运行,以在端口80上提供内容:

$lynx -dump http://10.1.1.252
   Hello this is apache2 sitting on host 10.1.1.252

我们将重点放在Server1和Nginx反向代理的配置上。

最小反向代理配置

安装nginx

# apt-get install nginx

接下来,我们禁用默认虚拟主机:

# unlink /etc/nginx/sites-enabled/default

接下来,在“/etc/nginx /sites-available”目录中创建一个新文件,以保存反向代理配置:

server {
        listen 80;
        location/{
             proxy_pass http://10.1.1.252;
        }
}

proxy_pass指令,指示nginx将在套接字10.1.1.251:80上通信的所有请求代理给远程套接字10.1.1.252:80

请确保nginx配置正确和重新启动nginx

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# service nginx restart

在此阶段,您应该能够在ip地址'10.1.1.251'上查询新的反向代理,并获得在ip地址'10.1.1.251'上运行的'apache2'内容:

$lynx -dump http://10.1.1.251
   Hello this is apache2 sitting on host 10.1.1.252

基本的反向代理配置成功后,可以设置nginx的其他指令来优化现有配置。

在Debian Linux上设置nginx反向代理服务器

什么是反向代理

简而言之,反向代理服务器充当客户端之间的中间服务,要求诸如HTTP页面和一个或者多个服务器的资源。
使用反向代理允许多种优点,例如改进的性能,负载均衡,透明服务器维护反向代理服务器后面的服务器,提高安全性等。

日期:2020-07-07 20:56:56 来源:oir作者:oir