前言
Nginx是一款轻量级的Web 服务器/反向代理服务器。其特点是占有内存少,并发能力强。这里将Nginx的配置教程记录分享一下。
那么什么是反向代理呢,反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
部署教程
部署环境:
3台CentOS 7.4 服务器,其中一台是nginx反向代理服务器,另外两台是web服务器。
nginx反向代理服务器:
- 主机名:groupe-nginx-0707,IP:192.168.1.167/24
web服务器:
- 主机名:groupe-nginx-0707-web1,IP:192.168.1.18
- 主机名:groupe-nginx-0707-web2,IP:192.168.1.217
先进行两台web服务器的配置:
[root@groupe-nginx-0707-web2 ~]# yum -y install httpd
[root@groupe-nginx-0707-web2 html]# echo "web1" > index.html
[root@groupe-nginx-0707-web2 html]# systemctl start httpd
[root@groupe-nginx-0707-web2 html]# curl http://localhost/index.html
web1
进行nginx反向代理服务器配置:
```
[root@groupe-nginx-0707 ~]# yum -y install nginx
[root@groupe-nginx-0707 ~]# vim /etc/nginx/nginx.conf
## 有关配置
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
gzip on;
upstream www {
server 192.168.1.18;
server 192.168.1.217;
}
## 相关配置
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3;
proxy_read_timeout 3;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_pass http://www;
}
[root@groupe-nginx-0707 ~]# systemctl start nginx
配置完成后,使用curl命令进行验证结果:
[root@groupe-nginx-0707 ~]# curl http://localhost/
web1
[root@groupe-nginx-0707 ~]# curl http://localhost/
web2
[root@groupe-nginx-0707 ~]# curl http://localhost/
web1
[root@groupe-nginx-0707 ~]# curl http://localhost/
web2
[root@groupe-nginx-0707 ~]# curl http://localhost/
web1
[root@groupe-nginx-0707 ~]# curl http://localhost/
web2
[root@groupe-nginx-0707 ~]#
本文暂时没有评论,来添加一个吧(●'◡'●)