千家信息网

安装Nginx的依赖、 Nginx 反向代理、负载均衡等详解

发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,下文给大家带来安装Nginx的依赖、 Nginx 反向代理、负载均衡等详解,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经
千家信息网最后更新 2025年02月04日安装Nginx的依赖、 Nginx 反向代理、负载均衡等详解

下文给大家带来安装Nginx的依赖、 Nginx 反向代理、负载均衡等详解,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用在行业内累计的经验来做一个解答。


安装Nginx的依赖:

yum -y install pcre-devel zlib-devel openssl-devel


安装源码包Nginx的关联:

要先创建管理Nginx的系统用户

useradd -M -s /sbin/nologin nginx

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module


*************************************************************************************************


一、Nginx反向代理

1.配置环境一台Nginx,一台测试云服务器,web1

[root@web1 ~]# yum install -y httpd

2.启动httpd

[root@web1 ~]# service httpd start 正在启动 httpd:                                           [确定]

3.在httpd页面写好页面

[root@web1 ~]# vim /var/www/html/index.html iiiiiiiiiiiiiiiiiiiiii

4.配置Nginx反向代理

vim /usr/local/nginx/conf/nginx.conflocation / {        proxy_pass      http://192.168.18.201;       }

5.页面访问Nginx的IP,会显示httpd配置的页面


二、Nginx负载均衡

一台Nginx,两台web服务器

1.配置nginx负载均衡

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confupstream webservers {      server 192.168.18.201 weight=1; #实验环境用权重      server 192.168.18.202 weight=1;  }  server {      listen       80;      server_name  localhost;      #charset koi8-r;      #access_log  logs/host.access.log  main;      location / {              proxy_pass      http://webservers;              proxy_set_header  X-Real-IP  $remote_addr;      }}

注,upstream是定义在server{ }之外的,不能定义在server{ }内部。定义好upstream之后,用proxy_pass引用一下即可。

2.重新加载一下配置文件

[root@nginx ~]# pkill ngixn[root@nginx ~]#  /usr/local/nginx/sbin/nginx

3.页面测试

注:不断刷新就会发现web1与web2是交替出现的,达到了负载均衡的效果。


三、Nginx页面缓存

proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=one:10m inactive=1m max_size=30g;

inactive=1m 如果缓存1分钟没人访问,nginx 会删除掉这些缓存 硬盘中的最大空间为 30G;

1.配置一个简单的Nginx缓存服务器

[root@nginx ~]# vim /etc/nginx/nginx.confproxy_cache_path /data/nginx/cache/webserver levels=1:2 keys_zone=webserver:20m max_size=1g;      upstream webservers {        server 192.168.115.87:8080 weight=1 max_fails=2 fail_timeout=2;        }   server {       listen       80;       server_name  localhost;       #charset koi8-r;       #access_log  logs/host.access.log  main;       location / {               proxy_pass      http://webservers;               proxy_set_header  X-Real-IP  $remote_addr;               proxy_cache webserver;               proxy_cache_valid 200 10m;       }}

2.建立缓存目录

[root@nginx ~]# mkdir -pv /data/nginx/cache/webserver

注:创建的目录要与配置文件里写的路径一样

3.重启Nginx

[root@nginx ~]# pkill ngixn[root@nginx ~]#  /usr/local/nginx/sbin/nginx

4.页面刷新,然后停掉httpd服务器在刷新会发现页面还会存在,然后去web服务器上查看缓存文件

[root@web1 63]# pwd/data/nginx/cache/webserver/f/63[root@C0S1 63]# ls681ad4c77694b65d61c9985553a2763f   #缓存文件

四、Nginx读写分离

1修改配置文件

[root@nginx nginx]# vim  /usr/local/nginx/conf/nginx.confserver {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {                proxy_pass http://192.168.18.202;                if ($request_method = "PUT"){                        proxy_pass http://192.168.18.201;                }        }}

2.重启Nginx

[root@nginx ~]# pkill ngixn[root@nginx ~]#  /usr/local/nginx/sbin/nginx

3.配置httpd的WebDAV功能

注,在下启用就行。

4.重新启动一下httpd

[root@web1 ~]# service httpd restart停止 httpd:                                               [确定]正在启动 httpd:                                           [确定]

5.测试一下

[root@nginx ~]# curl http://192.168.18.201

web1.test.com

[root@nginx ~]# curl http://192.168.18.202

web2.test.com

注,web1与web2访问都没问题。

[root@nginx ~]# curl -T /etc/issue  http://192.168.18.202405 Method Not Allowed

Method Not Allowed

The requested method PUT is not allowed for the URL /issue.
Apache/2.2.15 (CentOS) Server at 192.168.18.202 Port 80

注,我们上传文件到,web2上时,因为web2只人读功能,所以没有开户WebDAV功能,所以显示是405 Method Not Allowed。

[root@web1 ~]# setfacl -m u:apache:rwx /var/www/html/

下面我们再来测试一下

[root@nginx ~]# curl -T /etc/issue  http://192.168.18.201201 Created

Created

Resource /issue has been created.
Apache/2.2.15 (CentOS) Server at 192.168.18.201 Port 80

注,大家可以看到我们成功的上传了文件,说明nginx读写分离功能配置完成。最后,我们来查看一下上传的文件。

[root@web1 ~]# cd /var/www/html/[root@web1 html]# ll总用量 12drwxr-xr-x 2 root   root   4096 9月   4 13:16 forum-rw-r--r-- 1 root   root     23 9月   3 23:37 index.html-rw-r--r-- 1 apache apache   47 9月   4 14:06 issue


看了以上关于安装Nginx的依赖、 Nginx 反向代理、负载均衡等详解,如果大家还有什么地方需要了解的可以在行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,技术工程师在行业内拥有十几年的经验了。



0