700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Nginx代理——正向 反向代理 动静分离和负载均衡

Nginx代理——正向 反向代理 动静分离和负载均衡

时间:2022-07-07 13:28:13

相关推荐

Nginx代理——正向 反向代理 动静分离和负载均衡

文章目录

一、Nginx代理概述(1)什么是正向代理和反向代理-正向代理:-反向代理-反向代理和正向代理的区别(2)配置代理-Nginx代理配置语法-配置正向代理-Nginx反向代理示例二、基于Nginx反向代理的负载均衡(1)负载均衡概述1、实验环境2、实验目的3、实验步骤(5)Nginx负载均衡状态配置-测试backup和down状态下的负载均衡三、配置Nginx动静分离(1)动静分离概述(2)配置Nginx的动静分离1、实验环境2、实验目的3、实验步骤-proxy配置-动态web和静态web的配置

一、Nginx代理概述

(1)什么是正向代理和反向代理

代理就是客户端通过代理服务器去访问指定的服务器,例如去买房子,人门通过中介去买开发商的房子,中介就是代理

-正向代理:

正向代理是面向客户端的。客户端想要访问一个web服务器,但是客户端的ip被web服务器禁止访问了,这个时候就可以通过代理服务器,客户端通过代理服务器去访问web服务器,web服务器只会知道是代理服务器的ip访问的它,而不知道是客户端正向代理更多的用在公司内网,公司内员工的主机都通过一个代理服务器访问互联网,而互联网上被访问的服务器,只知道代理服务器的ip,不知道公司内员工的ip

-反向代理

反向代理是面向服务器的。通常客户端要访问web服务器,就得知道web服务器的真实ip,但这样对于公司来说就需要多个公网ip,并且web的真实ip被暴露在了互联网上,十分的不安全,这个时候只需要部署一个代理服务器即可,客户端访问代理服务器,代理服务器去访问公司内网的web服务器,然后调取数据后返回给客户端,这个过程客户端不知道web服务器的真实ip,而公司也不需要多个公网ip,只需要代理服务器一个公网ip即可

反向代理通常搭配负载均衡一起使用

-反向代理和正向代理的区别

代理的对象不一样,正向代理是面向客户端的,而反向代理是面向服务器的

实际工作中使用最多的是反向代理

(2)配置代理

Nginx作为一个高性能的web服务器,同时也是一个优秀的代理服务器,可以使用Nginx来配置正向和反向代理

-Nginx代理配置语法

Nginx代理配置:

语法:

proxy_pass URL

默认:

可配置区域:location,if in location,limit_except

URL即资源,就是用户访问proxy_pass URL所在的location时会跳转到指定的URL

类似于nopush缓冲区

语法:

proxy_buffering on\off

默认:proxy_buffering on

可配置区域:http,server,location

扩展:

proxy_buffer_size , proxy_buffers , proxy_busy_buffer_size

跳转重定向

语法:

proxy_redirect default, proxy_redirect off, proxy_refirect redirect replacement

默认:proxy_redirect default

可配置区域:http , server ,location

Nginx中的proxy_redirect功能十分强大,它的作用时对发送客户端的URL进行修改,例如:

server {listen 80;server_name ;location /{proxy_pass http://1.1.1.1:8080;}}#这段配置虽然是正确的,但是在进行抓包后,发现的URL是这样的http://1.1.1.1:8080/这样不仅看的不舒服,并且还暴露的端口号,增加了风险,所以可以使用proxy_redirect进行重定向,加一条重定向proxy_redirect http://1.1.1.1:8080/ /,这样在抓包时看到的就是/,从而防止了暴露端口号等信息server {listen 80;server_name ;location /{proxy_pass http://1.1.1.1:8080;proxy_redirect http://1.1.1.1:8080/ /;}}

修改头部信息

语法:

proxy_set_header field value

默认:proxy_set_header Host $proxy_host,proxy_set_header Connection close

可配置区域:http,server,location

例如:

客户端ip:100.1,反向代理服务器ip:100.2,后端web服务器ip:100.3

#客户端访问反向代理服务器,反向代理服务器访问后端服务器,后端服务器回应反向代理服务器然后返回给客户端proxy_set_herder Host $host :Host为变量名称,$host为变量值。$host是一个内建变量,变量值为代理服务器请求的ip即100.3,这段配置的意思即修改头部信息加入Host变量的值,值为$hostproxy_set_header X-Real-IP $remote_addr:同理,X-Real-IP为变量名称,$remote_addr为变量值。$remote_addr也是一个内建变量,变量值为客户端的ip,即100.1,那么这段配置意思就是修改头部信息加入X-Real-IP的值proxy_set_header Host $host:$proxy_port:同理Host为变量名称,$host:$proxy_port为变量值。$host:$proxy_port是两个内建变量,$host的值为代理服务器的ip100.2,$proxy_set_port的值为转发服务器(100.2)请求web服务器的端口,通常来说就是80,因为Nginx的端口默认为80,这段的意思就是修改头部信息加入Host的值

代理到后端的TCP连接超时

语法:

proxy_connect_timeout time

默认:proxy_connect_timeout 60s (默认超时时间为60秒)

可配置区域:http,server,location

扩展:proxy_read_timeout,proxy_send_timeout

超时过后会断开TCP连接,这个是基于长连接的,即长连接的超时时间

-配置正向代理

实验环境:

实验目的:

web服务器拒绝本机访问指定资源的图片,本机通过代理可以成功访问web服务器指定资源的图片

rzy—实验步骤:

******(1)先做基础配置[root@Centos7 ~]# hostnamectl set-hostname rzy[root@Centos7 ~]# su[root@rzy ~]# systemctl stop firewalld[root@rzy ~]# setenforce 0setenforce: SELinux is disabled[root@rzy ~]# mount /dev/cdrom /mnt/mount: /dev/sr0 写保护,将以只读方式挂载mount: /dev/sr0 已经挂载或 /mnt 忙/dev/sr0 已经挂载到 /mnt 上******(2)上传Nginx包,安装Nginx[root@rzy ~]# ll总用量 1020-rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg-rw-r--r-- 1 root root 1039530 4月 23 00:15 nginx-1.18.0.tar.gz[root@rzy ~]# yum -y install zlib-devel pcre-devel #安装依赖包。。。。。。完毕![root@rzy ~]# tar xf nginx-1.18.0.tar.gz -C /usr/src/[root@rzy ~]# cd /usr/src/nginx-1.18.0/[root@rzy nginx-1.18.0]# useradd -M -s /sbin/nologin nginx[root@rzy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install[root@rzy nginx-1.18.0]# scp -r /usr/local/nginx root@192.168.100.203:/usr/local/nginx #把配置文件直接传到rzy02中The authenticity of host '192.168.100.203 (192.168.100.203)' can't be established.ECDSA key fingerprint is SHA256:VhTZ5YxS5af2rHtfCvyc6ehXh3PD2A8KY2MyE6rHjiU.ECDSA key fingerprint is MD5:e8:41:d2:8a:7e:e9:a9:47:a3:f0:29:be:e9:6d:df:51.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.100.203' (ECDSA) to the list of known hosts.root@192.168.100.203's password: 。。。。。。(略)[root@rzy nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #优化执行路径[root@rzy nginx-1.18.0]# vim /usr/lib/systemd/system/nginx.service #编写启动脚本[Unit]Description=nginxAfter=network.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target#保存退出[root@rzy nginx-1.18.0]# systemctl start nginx #开启nginx[root@rzy nginx-1.18.0]# netstat -anpt | grep 80 #检查端口tcp 00 0.0.0.0:80 0.0.0.0:*LISTEN3706/nginx: master ******(3)修改配置文件,配置正向代理[root@rzy nginx-1.18.0]# cd /usr/local/nginx/conf/[root@rzy conf]# cp nginx.conf nginx.conf.bak #备份一份配置文件[root@rzy conf]# sed -i '/#/d' nginx.conf #删除注释和空行[root@rzy conf]# sed -i '/^$/d' nginx.conf[root@rzy conf]# vim nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10server {11 listen 80;12 server_name localhost;13 location / {14 root html;15 index index.html index.htm;16 proxy_pass http://$http_host$request_uri; #利用变量配置跳转,灵活一点17 proxy_set_header Host $http_host 18 proxy_set_header X-Real-IP $remote_addr;19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #被访问的web服务器可以看到客户端的真实ip20 }21 }22 }#######注释:$http_host:这是一个Nginx的内建变量,变量值就是客户端请求的目标ip地址,例如,客户端已经指向了代理,然后访问1.1.1.1,那么这个变量的值就是.1.1.1,同理访问2.2.2.2那么这个变量值就是2.2.2.2$request_uri:这是一个Nginx的内建变量,变量值就是客户端访问的资源,例如,客户端已经指向了代理,然后访问http://1.1.1.1/那么这个变量值就是/,同理访问http://1.1.1.1/aaa那么这个值就变成了/aaa$http_host :Nginx的内建变量,值为请求客户端请求代理服务器的ip$remote_addr:Nginx的内建变量,值为请求客户端的ip

rzy02——实验步骤

******(1)做基础配置[root@Centos7 ~]# hostnamectl set-hostname rzy02[root@Centos7 ~]# su[root@rzy02 ~]# systemctl stop firewalld[root@rzy02 ~]# setenforce 0setenforce: SELinux is disabled[root@rzy02 ~]# mount /dev/cdrom /mnt/mount: /dev/sr0 写保护,将以只读方式挂载mount: /dev/sr0 已经挂载或 /mnt 忙/dev/sr0 已经挂载到 /mnt 上******(2)优化Nginx[root@rzy02 ~]# yum -y install zlib-devel pcre-devel 。。。。。。完毕![root@rzy02 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/[root@rzy02 ~]# useradd -M -s /sbin/nologin nginx[root@rzy02 ~]# vim /usr/lib/systemd/system/nginx.service[Unit]Description=nginxAfter=network.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target#保存退出[root@rzy02 ~]# systemctl start nginx[root@rzy02 ~]# netstat -antp | grep 80tcp 00 0.0.0.0:80 0.0.0.0:*LISTEN14885/nginx: master ******(3)修改配置文件[root@rzy02 ~]# cd /usr/local/nginx/conf/[root@rzy02 conf]# cp nginx.conf nginx.conf.bak[root@rzy02 conf]# sed -i '/#/d' nginx.conf[root@rzy02 conf]# sed -i '/^$/d' nginx.conf[root@rzy02 conf]# vim nginx.conf #修改1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10server {11 listen 80;12 server_name ;13 index index.html index.htm;14 root html;15 location ~ .*\.(jpg|gif|png)$ {#匹配以.jpg、gif、png结尾的资源16allow 192.168.100.202;17deny all;18root html;19 }20 }21 }[root@rzy02 conf]# systemctl restart nginx******(4)编写页面[root@rzy02 html]# vim index.html aaaaaa<img src="1.png"\>[root@rzy02 html]# rz #上传照片z waiting to receive.**B0100000023be50[root@rzy02 html]# ls50x.html index.html iron_man-006.jpg[root@rzy02 html]# mv iron_man-006.jpg 1.png

使用本机或者开一台win7虚拟机进行访问,正常是无法看到图片的,因为web服务器的图片资源只允许代理服务器

添加代理,再次访问。注意:使用inetcpl.cpl只对ie浏览器和360浏览器生效,其他的浏览器配置文件不是这个

查看web服务器的日志

******(1)rzy02修改配置文件[root@rzy02 html]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10log_format main '$remote_addr - $remote_user [$time_local] "$request" '11'$status $body_bytes_sent "$http_referer" '12'"$http_user_agent" "$http_x_forwarded_for"'; #让rzy02支持显示真实ip13 14access_log logs/access.log main;15server {16 listen 80;17 server_name ;18 index index.html index.htm;19 root html;20 location ~ .*\.(jpg|gif|png)$ {21allow 192.168.100.202;22deny all;23root html;24 }25 }26 }[root@rzy02 html]# systemctl restart nginx******(2)再次使用浏览器访问,然后查看日志[root@rzy02 html]# tail -1 /usr/local/nginx/logs/access.log #成功显示真实客户端的ip192.168.100.202 - - [23/Apr/:03:45:13 +0800] "GET /1.png HTTP/1.0" 304 0 "http://192.168.100.203/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)" "192.168.100.222"

-Nginx反向代理示例

******(1)继续上面的进度,只需要修改一下代理服务器Nginx的配置文件即可[root@rzy conf]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10server {11 listen 80;12 server_name localhost;13 location / {14 root html;15 index index.html index.htm;16 proxy_pass http://192.168.100.203:8080; #修改代理17 }18 }19 }[root@rzy conf]# systemctl restart nginx ******(2)然后修改web服务器的Nginx配置文件[root@rzy02 html]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10log_format main '$remote_addr - $remote_user [$time_local] "$request" '11'$status $body_bytes_sent "$http_referer" '12'"$http_user_agent" "$http_x_forwarded_for"';13 14access_log logs/access.log main;15server {16 listen 8080; #修改端口号17 server_name ;18 index index.html index.htm;19 root html;20 location ~ .*\.(jpg|gif|png)$ {21allow 192.168.100.202;22deny all;23root html;24 }25 }26 }[root@rzy02 html]# systemctl restart nginx

使用浏览器访问代理服务器,发现成功跳转,反向代理配置成功

二、基于Nginx反向代理的负载均衡

(1)负载均衡概述

作用:提升服务器群集的请求吞吐率,提升服务器群集的整体性能,提高冗余

负载均衡分为两种:

按范围划分的负载均衡GSLB。

原理:按范围划分,去分配给用户指定的服务器

例如:公司的业务覆盖面积很广,从山西省到广东省都有web服务器,这个时候就会有相应的调度中心节点,而用户访问的是这个调度中心节点,然后节点通过用户所在的地区,划分给距离用户最近的服务器,这个就是按范围划分的

全局负载均衡SLB

全局负载均衡按层级划分,分为传输层和应用层的负载均衡,传输层为协议,应用层为服务

Nginx就是一个典型的七层SLB,LVS是四层的SLB,Haproxy四层和七层都支持

例如:客户端访问Ngxin代理服务器,Nginx代理服务器通过指定的算法来分配服务器去接受请求

1、实验环境

Nginx负载均衡需要使用proxy_pass代理模块来进行配置,可以说有负载均衡就一定配置了反向代理,但是配置了反向代理不一定配置了负载均衡

负载均衡使用的区域:upstream区域

语法:upstream name { }

可配置的区域:http

2、实验目的

配置rzy反向代理,并且配置rzy02和rzy03负载均衡,可以来回切换页面

3、实验步骤

-rzy配置

******(1)先做基础配置[root@Centos7 ~]# hostnamectl set-hostname rzys[root@Centos7 ~]# su[root@rzy ~]# systemctl stop firewalld[root@rzy ~]# setenforce 0setenforce: SELinux is disabled[root@rzy ~]# mount /dev/cdrom /mnt/mount: /dev/sr0 写保护,将以只读方式挂载mount: /dev/sr0 已经挂载或 /mnt 忙/dev/sr0 已经挂载到 /mnt 上******(2)上传Nginx源码包,进行安装[root@rzy ~]# yum -y install zlib-devel pcre-devel #安装依赖包。。。。。。完毕![root@rzy ~]# ll总用量 1020-rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg-rw-r--r-- 1 root root 1039530 4月 23 23:55 nginx-1.18.0.tar.gz[root@rzy ~]# tar xf nginx-1.18.0.tar.gz -C /usr/src/ [root@rzy ~]# cd /usr/src/nginx-1.18.0/[root@rzy nginx-1.18.0]# useradd -M -s /sbin/nologin nginx[root@rzy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install [root@rzy nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #做软连接,优化Nginx的命令执行路径[root@rzy nginx-1.18.0]# vim /usr/lib/systemd/system/ngxin.service #编写启动脚本[Unit]Description=nginxAfter=network.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target[root@rzy system]# systemctl start nginx[root@rzy system]# netstat -anpt | grep nginxtcp 00 0.0.0.0:80 0.0.0.0:*LISTEN17474/nginx: master [root@rzy conf]# cp nginx.conf nginx.conf.bak #做一份配置文件的备份[root@rzy conf]# sed -i '/#/d' nginx.conf #删除注释行和空行[root@rzy conf]# sed -i '/^$/d' nginx.conf ******(3)修改配置文件1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10upstream aaa {#配置负载均衡的区域11server 192.168.100.203:80 weight=5; #加入两个web服务器的ip和端口,配置weight权重,权重相同,为轮询方式12server 192.168.100.204:80 weight=5;13}14server {15 listen 80;16 server_name localhost;17 location / {18 proxy_pass http://aaa; #配置代理,跳转到指定区域。区域名称要和上面配置的负载均衡的区域名称相同19 root html;20 index index.html index.htm;21 }22 error_page 500 502 503 504 /50x.html;23 location = /50x.html {24 root html;25 }26}27 }[root@rzy conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@rzy conf]# nginx -s reload #重载nginx使配置文件生效

-rzy02和rzy03的配置

******(1)做基础配置,安装Nginx,和rzy相同(略)******(2)分别在两台web服务器上编写两个不同的网页#rzy02配置[root@rzy02 ~]# cd /usr/local/nginx/html/[root@rzy02 html]# echo "web1" > index.html #编写网页[root@rzy02 html]# systemctl start nginx#rzy03配置[root@rzy03 ~]# cd /usr/local/nginx/html/[root@rzy03 html]# echo "web2" > index.html [root@rzy03 html]# systemctl start nginx

使用浏览器访问代理服务器rzy,发现网页会进行轮询显示

(5)Nginx负载均衡状态配置

后端服务器在负载均衡调度中的状态:

max_fails和fail_timeout配合使用,可以防止服务器压力过大,因为当服务器压力过大之后,并发达到最大限度,这个时候就会有连接请求失败,失败达到一定次数,服务器就会先拒绝连接请求,然后处理已经接受的请求,经过等待时间后才会再次进行负载均衡。

语法:在upstream区域中的server后面加状态即可

-测试backup和down状态下的负载均衡

******(1)修改rzy的主配置文件,配置测试down状态[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10upstream aaa {11server 192.168.100.203:80 weight=5 down; #把203变为down的状态,不进行负载均衡12server 192.168.100.204:80 weight=5;13}14server {15 listen 80;16 server_name localhost;17 location / {18 proxy_pass http://aaa;19 root html;20 index index.html index.htm;21 }22 error_page 500 502 503 504 /50x.html;23 location = /50x.html {24 root html;25 }26}27 }[root@rzy ~]# nginx -s reload #重载

使用浏览器访问代理服务器,查看是否只出现一个页面

只出现了204服务器的页面,说明203已经不参与负载均衡

******(2)修改配置文件,测试backup状态[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10upstream aaa {11server 192.168.100.203:80 weight=5;12server 192.168.100.204:80 weight=5 backup; #修改204为备份状态13}14server {15 listen 80;16 server_name localhost;17 location / {18 proxy_pass http://aaa;19 root html;20 index index.html index.htm;21 }22 error_page 500 502 503 504 /50x.html;23 location = /50x.html {24 root html;25 }26}27 }[root@rzy ~]# nginx -s reload #重载

现在访问代理服务器,发现只能访问203的页面

现在关闭203的web服务器,再次进行访问

发现出现了204的页面,说明204备份服务器已经在进行负载均衡

三、配置Nginx动静分离

(1)动静分离概述

动静分离:通过中间件也就是代理服务器把静态请求和动态请求进行分离,分离资源可以减少不必要的请求消耗,减少请求延迟

原理:中间件也就Nginx代理会把动态请求转发给其他服务器,自己只处理静态请求,当然也可以把静态请求转发给后端的可以解析静态请求的web服务器(因为Nginx默认只能处理静态页面)

(2)配置Nginx的动静分离

1、实验环境

只是为了看到效果,所以这里都使用Nginx

2、实验目的

客户端通过代理服务器可以访问动态页面和静态页面

3、实验步骤

-proxy配置

******(1)先做基础配置[root@Centos7 ~]# hostnamectl set-hostname proxy[root@Centos7 ~]# su[root@proxy ~]# systemctl stop firewalld[root@proxy ~]# setenforce 0setenforce: SELinux is disabled[root@proxy ~]# mount /dev/cdrom /mnt/mount: /dev/sr0 写保护,将以只读方式挂载mount: /dev/sr0 已经挂载或 /mnt 忙/dev/sr0 已经挂载到 /mnt 上******(2)上传Nginx源码包进行安装[root@proxy ~]# ll总用量 1020-rw-------. 1 root root 1264 1月 12 18:27 anaconda-ks.cfg-rw-r--r-- 1 root root 1039530 4月 25 18:29 nginx-1.18.0.tar.gz[root@proxy ~]# tar xf nginx-1.18.0.tar.gz -C /usr/src/[root@proxy ~]# cd /usr/src/nginx-1.18.0/[root@proxy nginx-1.18.0]# yum -y install zlib-devel pcre-devel[root@proxy nginx-1.18.0]# useradd -M -s /sbin/nologin nginx[root@proxy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install #配置编译安装[root@proxy nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #优化执行路径[root@proxy nginx-1.18.0]# vim /lib/systemd/system/nginx.service[Unit]Description=nginxAfter=network.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target[root@proxy nginx-1.18.0]# systemctl start nginx [root@proxy nginx-1.18.0]# netstat -anpt | grep nginxtcp 00 0.0.0.0:80 0.0.0.0:*LISTEN3714/nginx: master ******(3)修改配置文件[root@proxy nginx-1.18.0]# cd /usr/local/nginx/conf/[root@proxy conf]# cp nginx.conf nginx.conf.bak[root@proxy conf]# sed -i '/#/d' nginx.conf[root@proxy conf]# sed -i '/^$/d' nginx.conf[root@proxy conf]# vim nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10upstream static {#配置两个复杂均衡,把静态和动态分开11server 192.168.100.203:80 weight=5;12}13upstream java {14server 192.168.100.204:80 weight=5;15}16server {17 listen 80;18 server_name localhost;19 location / {#配置区域,默认调转到静态的负载均衡20 root html;21 index index.html;22 proxy_pass http://static;23 }24 location ~ .*\.(jsp|gif|png|css)$ {#匹配资源,有以.jsp、gif等为后缀的资源跳转到动态的负载均衡25 proxy_pass http://java;26 }27}28 }#保存退出[root@proxy conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@proxy conf]# nginx -s reload

-动态web和静态web的配置

******(1)nginx的配置,静态web#进行基础配置、上传Nginx源码包进行安装(略)#修改配置文件,编写网页目录1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10server {11 listen 80;12 server_name localhost;13 location / {14 root html;15 index index.html;16}17 }18 }#保存退出[root@nginx ~]# systemctl start nginx[root@nginx ~]# netstat -anpt | grep nginxtcp 00 0.0.0.0:80 0.0.0.0:*LISTEN1129/nginx: master [root@nginx ~]# echo "static" > /usr/local/nginx/html/index.html ******(2)Nginx-02的配置,动态web#进行基础配置,上传Nginx源码包进行安装(略)#修改配置文件,编写带有png格式的图片的页面[root@nginx-02 ~]# vim /usr/local/nginx/conf/nginx.conf1 worker_processes 1;2 events {3worker_connections 1024;4 }5 http {6include mime.types;7default_type application/octet-stream;8sendfile on;9keepalive_timeout 65;10server {11 listen 80;12 server_name localhost;13 location / {14 root html; 15 index index.jsp; #修改解析页面,匹配代理服务器的资源匹配从而跳转到此服务器16} 17 } #保存退出[root@nginx-02 ~]# cd /usr/local/nginx/html/[root@nginx-02 html]# mv index.html index.jsp[root@nginx-02 html]# echo "java" > index.jsp [root@nginx-02 html]# systemctl start nginx[root@nginx-02 html]# netstat -anpt | grep nginxtcp 00 0.0.0.0:80 0.0.0.0:*LISTEN1139/nginx: master

使用浏览器访问进行测试

访问index.jsp资源查看是否能够跳转

至此动静分离成功

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。