这里写目录标题
nginx
nginx的优点
什么是集群
集群:就是一组相互独立的计算机,通过单一的系统模式进行管理,每个集群节点(即集群中的每台计算机)都是运行各自服务的独立服务器。这些服务器之间可以彼此通信,它们作为整体向用户提供一组网络资源,当用户请求集群系统时,集群给用户的感觉就是一个单独的服务器,而实际上用户请求的是一组集群服务器。
简单来讲就是指多台服务器合作做同一件事
举个例子:
像常见的,我们打开谷歌,百度的搜索页面,它们的页面看着很简单,觉得可以很轻松的制作出一样的网页,但实际上,这个页面背后是成百上千台服务器也就是集群协同工作的结果。
常见的集群
什么是正向代理、反向代理、透明代理
常见的代理技术
- 正向代理:Forward Proxy
- 反向代理:Reverse proxy
- 透明代理:Transparent proxy
区别:
- 正向代理代理的对象是客户端,反向代理代理的对象是服务端
正向代理
一般情况下,如果没有特别说明,代理技术默认说的是正向代理。正向代理由如下两种场景:

正向代理是一个位于客户端【User A】和原始服务器【Origin Server】之间的代理服务器【Forward Proxy】,为了从原始服务器取得内容,客户端向代理服务器发送一个请求并指定目标Origin Server,然后代理服务器向Origin Server转交请求并将获得的内容返回给客户端。
代理服务器可能在内网也可以在外网。一般客户端需要特殊的配置才能使用。
 作用
- 客户端【User A】无法直接访问- 原始服务器【Origin Server】
- 加速访问原始服务器【Origin Server】
- Cache作用
- 客户端访问授权
- 隐藏访问者的行踪
软件
- 科学上网工具 shadowsocks
- squid
- trafficserver
反向代理
反向代理正好与正向代理相反,对于客户端而言代理服务器就像是原始服务器,并且客户端不需要进行任何特别的设置。

客户端【User A】向反向代理【Reverse Proxy】发送请求,接着反向代理将判断向哪个原始服务器【Origin Server】转交请求,并将获得的内容返回给客户端。
作用
- 保护和隐藏原始资源服务器
- 负载均衡,CDN实现原理
软件
- nginx
- trafficserver
透明代理
透明代理将拦截客户端【User A】发送的请求,拦截后自己代为访问原始服务器【Origin Server】,获取响应结果后再由透明代理交给客户端。一般公司内的上网行为管理软件就是透明代理。透明代理的客户端根本不需要知道有代理服务器的存在。

nginx部署
安装nginx:https://nginx.org/en/download.html
安装nginx
关闭防火墙
创建系统用户nginx
[root@zyq ~]#: useradd -r -M -s /sbin/nologin nginx
[root@zyq ~]#: id nginx
uid=991(nginx) gid=991(nginx) groups=991(nginx)
安装编译工具
[root@zyq ~]#: yum -y install make gcc gcc-c++
安装依赖包
[root@zyq ~]#: yum -y install pcre-devel openssl openssl-devel gd-devel
创建日志存放目录
[root@zyq ~]#: mkdir -p /var/log/nginx
[root@zyq ~]#: chown -R nginx.nginx /var/log/nginx
下载nginx
[root@zyq ~]#: wget https://nginx.org/download/nginx-1.24.0.tar.gz
--2024-01-15 15:39:30--  https://nginx.org/download/nginx-1.24.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1112471 (1.1M) [application/octet-stream]
Saving to: ‘nginx-1.24.0.tar.gz’
nginx-1.24.0.tar.gz    100%[=========================>]   1.06M   441KB/s    in 2.5s    
2024-01-15 15:39:33 (441 KB/s) - ‘nginx-1.24.0.tar.gz’ saved [1112471/1112471]
[root@zyq ~]#: ls
anaconda-ks.cfg  nginx-1.24.0.tar.gz
[root@zyq ~]#: 
[root@zyq ~]#: tar xf nginx-1.24.0.tar.gz 
[root@zyq ~]#: cd  nginx-1.24.0/
[root@zyq nginx-1.24.0]#: ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
编译安装
[root@zyq nginx-1.24.0]#: ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@zyq ~]#: make && make install
[root@zyq ~]#: cd /usr/local/
[root@zyq local]#: ls
bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  src
[root@zyq local]#: ls nginx/
conf  html  logs  sbin
 
配置环境变量
[root@zyq ~]#: echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@zyq ~]#: source /etc/profile.d/nginx.sh 
[root@zyq ~]#: which nginx
/usr/local/nginx/sbin/nginx
启动nginx
[root@zyq ~]#: nginx
[root@zyq ~]#: ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                 
LISTEN    0         511                0.0.0.0:80              0.0.0.0:*                 
LISTEN    0         128                   [::]:22                 [::]:*                
服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}
设置开机自启
[root@zyq ~]#: cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@zyq ~]#: vim /usr/lib/systemd/system/nginx.service
[root@zyq ~]#: cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@zyq ~]#: systemctl daemon-reload 
[root@zyq ~]#: systemctl status nginx
○ nginx.service - nginx server daemon
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: dis>
     Active: inactive (dead)
     
[root@zyq ~]#: nginx -s stop 
[root@zyq ~]#: ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                 
LISTEN    0         128                   [::]:22                 [::]:*                 
[root@zyq ~]#: systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@zyq ~]#: ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                 
LISTEN    0         511                0.0.0.0:80              0.0.0.0:*                 
LISTEN    0         128                   [::]:22                 [::]:*           











