反向代理
安装nginx 1.26.1 平滑升级 负载均衡
1、nginx 反向代理配置
反向代理:⽤户直接访问反向代理服务器就可以获得⽬标服务器 (后端服务器)的资源。
前端服务器配置:(接收和响应客户端,代理另外一台主机)
[root@git ~]# yum -y install gcc gcc-g++ openssl-devel pcre-devel
[root@git ~]# ls
nginx-1.26.1.tar.gz
[root@git ~]# tar -zxvf nginx-1.26.1.tar.gz
[root@git ~]# ls
nginx-1.26.1 nginx-1.26.1.tar.gz
[root@git ~]# cd nginx-1.26.1
[root@git nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream
[root@web1 ~]# make && make install
[root@web1 nginx-1.26.1]# useradd -s /bin/nologin -M nginx
[root@web1 ~]# /usr/local/nginx/sbin/nginx
[root@web1 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@web1 ~]# firewall-cmd --reload
[root@web1 ~]# netstat -lntup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4402/nginx: master
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
添加监控模块(修改配置文件)
[root@sla nginx]# vim /usr/local/nginx/conf/nginx.conf //在location模块下面添加新模块status
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
}
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
动态(后端)服务器配置:
修改index.html文件,并且发布webfuwu
[root@web2 ~]# echo "this is java web server" > /usr/local/nginx/html/index.html
[root@web2 ~]# /usr/local/nginx/sbin/nginx
[root@web1 ~]# echo "this is static server" > /usr/local/nginx/html/index.html
[root@web1 ~]# /usr/local/nginx/sbin/nginx
[root@web1 ~]# ps -aux | grep nginx //查看现存进程信息
root 1331 0.0 0.2 46128 1160 ? Ss 10:16 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1332 0.0 0.3 46580 1904 ? S 10:16 0:00 nginx: worker process
root 1338 0.0 0.2 112824 976 pts/0 R+ 10:17 0:00 grep --color=auto nginx
[root@web1 ~]# curl localhost //访问本机(web1)
this is static server
[root@web1 ~]# curl 10.0.0.201 //访问web2
this is java web server
反向代理效果:当访问200主机(web1),(nginx反向代理201主机(web2)的服务器)返回201(web2)主机的页面。
在配置⽂件中添加⼀⾏反向代理块指令(proxy_pass),表示当访问本机地址 10.0.0.200 的 80 端⼝时即可跳转到后端服务器 10.0.0.201 的 80 端⼝上。
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
# root html;
# index index.html index.htm;
proxy_pass http://10.0.0.201:80;
}
[root@web1 ~]# /usr/local/nginx/sbin/nginx -s reload
代理百度
location / {
# root html;
# index index.html index.htm;
proxy_pass http://www.baidu.com:80;
}
nginx代理其他服务器的时候,不需要对方同意,更加方便了模块化操作。
2、代理优化(1)简单轮询
[root@web1 ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
upstream backend_servers {