唯有你也想见我, 我们俩的见面才有意义
Nginx 的安装 先下载 http://nginx.org/en/download.html , 推荐下载稳定版 可以先下载到 windows 系统里, 然后再通过 xftp 传输到虚拟机 或者通过 wget 的方式安装(虚拟机需要有网络)
1 2 3 [root@xiamu ~] [root@xiamu opt]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@xiamu opt] nginx-1.22.1.tar.gz [root@xiamu opt] [root@xiamu opt] nginx-1.22.1 nginx-1.22.1.tar.gz [root@xiamu opt] [root@xiamu nginx-1.22.1] auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [root@xiamu nginx-1.22.1] [root@xiamu nginx-1.22.1] [root@xiamu nginx-1.22.1]
如果指定编译安装的路径是自己自定义 的话, 采用如下这种方式安装 否则跳过此步按照后续默认的 /user/local/nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 $ ./configure --prefix=/opt/module/nginx $ make $ sudo make install $ sudo vi /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/opt/module/nginx/logs/nginx.pid ExecStartPre=/opt/module/nginx/sbin/nginx -t -c /opt/module/nginx/conf/nginx.conf ExecStart=/opt/module/nginx/sbin/nginx -c /opt/module/nginx/conf/nginx.conf ExecReload=/opt/module/nginx/sbin/nginx -s reload ExecStop=/opt/module/nginx/sbin/nginx -s stop ExecQuit=/opt/module/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target $ sudo systemctl daemon-reload $ sudo systemctl start nginx $ sudo systemctl status nginx
常见报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found [root@xiamu nginx-1.22.1] ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. [root@xiamu nginx-1.22.1] ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. [root@xiamu nginx-1.22.1]
开启网络服务报错 1 2 3 4 5 6 7 8 9 参考网站: https://www.cyberithub.com/failed-to-start-lsb-bring-up-down-networking/ 报错信息 Solved: network.service Failed to start LSB: Bring up/down networking in RHEL/Centos 7/8 尝试禁用NetworkManager, 或者BOOTPROTO设置成none [root@localhost ~] [root@localhost ~] [root@localhost ~]
查看 nginx 的目录 1 2 3 4 5 6 7 8 [root@xiamu nginx-1.22.1] [root@xiamu local ] bin games lib libexec sbin src etc include lib64 nginx share [root@xiamu local ] [root@xiamu nginx] conf html logs sbin
开启 nginx 服务 1 2 3 4 5 6 7 8 9 10 11 12 [root@xiamu nginx] [root@xiamu sbin] nginx [root@xiamu sbin] [root@xiamu sbin] ./nginx 启动 ./nginx -s stop 快速停止 ./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求 ./nginx -s reload 重新加载配置
在 windows 浏览器输入虚拟机的 ip, welcome to nginx, 如果访问不到, 请关闭防火墙
1 2 3 4 [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin]
安装成系统服务(配置脚本启动 nginx) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin] [Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecQuit=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin] [root@xiamu sbin]
了解 ngix 的目录 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@xiamu nginx] client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@xiamu nginx] 11928 [root@xiamu nginx] root 11928 1 0 13:20 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nobody 11929 11928 0 13:20 ? 00:00:00 nginx: worker process root 12992 7479 0 13:36 pts/2 00:00:00 grep --color=auto nginx
基本运行原理
Nginx 配置与应用场景 nginx 目录下 conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 worker_processes worker_processes 1; 默认为1,表示开启一个业务进程 worker_connections worker_connections 1024; 单个业务进程可接受连接数 include mime.types; include mime.types; 引入http mime类型 default_type application/octet-stream; default_type application/octet-stream; 如果mime类型没匹配上,默认使用二进制流的方式传输。 sendfile on; sendfile on; 使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。 keepalive_timeout 65; keepalive_timeout 65; 虚拟主机配置 server { listen 80; 监听端口号 server_name localhost; 主机名 location / { 匹配路径 root html; 文件根目录 index index.html index.htm; 默认页名称 } error_page 500 502 503 504 /50x.html; 报错编码对应页面 location = /50x.html { root html; } }
未开启 sendfile 开启后 server
配置自定义的域名 C:\Windows\System32\drivers\etc 下修改 host 文件, 如果不能保存, 请先保存到桌面然后再拖拽进来替换该文件 (注意配置完之后要保存, 并且要关掉电脑上的 VPN)
在阿里云的域名控制台配置内网 ip https://dns.console.aliyun.com/#/dns/setting/xiamu.icu 主机记录填写 www 记录值填写虚拟机的 ip 地址
然后打开 cmd 去 ping www.xiamu.icu , ping 通了, 说明配置成功了, 注意 ping 的是www.xxx.com 而不是 xxx.com ping 通之后可以打开浏览器访问www.xiamu.icu
批量添加很多域名, 在主机记录填写一个 * , 浏览器中 http://wwww.xiamu.icu/ , 四个 w 也是可以访问的
在根(/)路径下创建两个文件 www/www/index.html 和 www/vod/index.html, 并且往里面写入内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@xiamu www] index.html [root@xiamu www] this is www web site! [root@xiamu www] /www/www [root@xiamu vod] index.html [root@xiamu vod] this is void site. [root@xiamu vod] /www/vod
修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 [root@xiamu nginx] http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.xiamu.icu; location / { root /www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name vod.xiamu.icu; location / { root /www/vod; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@xiamu nginx] [root@xiamu nginx]
配置多个域名对应同一个资源文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 server { listen 80; server_name www.xiamu.icu; location / { root /www/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name vod.xiamu.icu vod1.xiamu.icu; location / { root /www/vod; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
可以发现 vod.xiamu.icu 和 vod1.xiamu.icu 这两个域名都可以访问到 void 的资源, 但是 void2.xiamu.icu, 访问的是 www 资源, 没有配置的域名会从第一个开始往下找, 如果没有找到, 就使用第一个域名对应的资源
1 2 3 4 5 6 7 8 9 10 11 12 server_name *.xiamu.icu; server_name www.xiamu.*; hosts文件修改成(注意有www) 192.168.1.100 www.xiamu.com 192.168.1.100 www.xiamu.net 192.168.1.100 www.xiamu.org
后缀匹配:
1 2 server_name ~^[0-9]+\.xiamu\.icu$;
正则表达式匹配:
反向代理到外网与内网主机的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 server { listen 80; server_name localhost; location / { proxy_pass http://www.atguigu.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } nginx1的配置 location / { proxy_pass http://192.168.1.102; } nginx2的配置 location / { root html; index index.html index.htm; } 会被192.168.1.102代理
负载均衡 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 upstream httpds { server 192.168.1.102:80; server 192.168.1.103:80; } server { listen 80; server_name localhost; location / { proxy_pass http://httpds; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 192.168.1.101 => 192.168.1.102 => 192.168.1.103
基于反向代理的负载均衡 负载均衡之权重, down, backup 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 upstream httpds { server 192.168.1.102 weight=8 down; server 192.168.1.103 weight=2; server 192.168.1.104 weight=1 backup; } server { listen 80; server_name localhost; location / { proxy_pass http://httpds; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
轮询 默认情况下使用轮询方式,逐一转发,这种方式适用于无状态请求。 weight(权重) 指定轮询几率,weight 和访问比率成正比,用于后端服务器性能不均的情况。 ip_hash 根据客户端的 ip 地址转发同一台服务器,可以保持回话。 least_conn 最少连接访问 url_hash 根据用户访问的 url 定向转发请求 fair 根据后端服务器响应时间转发请求
动静分离
下载 tomcathttps://www.digitalocean.com/community/tutorials/how-to-install-apache-tomcat-7-on-centos-7-via-yum
然后在 192.168.1.104 中部署一个项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 server { listen 80; server_name localhost; location / { proxy_pass http://192.168.1.104:8080/admin/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 这样可以通过http://192.168.1.101/访问到http://192.168.1.104:8080/admin/ 接着删除192.168.1.104下的静态资源js, css, images, fonts 再次访问的页面就不会有css等静态资源 我们需要把这些静态资源上传到192.168.1.101的/usr/local/nginx/html目录下 [root@xiamu01 html] 50x.html css fonts images index1.html index.html js server { listen 80; server_name localhost; location / { proxy_pass http://192.168.1.104:8080/admin/; } location /css { root html; index index.html index.htm; } location /js { root html; index index.html index.htm; } location /fonts { root html; index index.html index.htm; } location /images { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
使用正则表达式配置静态资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 server { listen 80; server_name localhost; location / { proxy_pass http://192.168.1.104:8080/admin/; } location ~*/(css|js|fonts|images) { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
伪静态配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 location / { rewrite ^/2.html$ /hello?pageNum=2 break ; proxy_pass http://192.168.1.104:8888; } http://192.168.1.101/hello?pageNum=2 => http://192.168.1.101/2.html location / { rewrite ^/([0-9]+).html$ /hello?pageNum=$1 break ; proxy_pass http://192.168.1.104:8888; } 使用正则表达式匹配任意数字 ... http://192.168.1.101/hello?pageNum=3 => http://192.168.1.101/3.html ... http://192.168.1.101/hello?pageNum=10 => http://192.168.1.101/10.html ... location / { rewrite ^/([0-9]+).html$ /hello?pageNum=$1 redirect; proxy_pass http://192.168.1.104:8888; } http://192.168.1.101/7.html => http://192.168.1.101/hello?pageNum=7
UrlRewrite rewrite 语法格式及参数语法: rewrite 是实现 URL 重写的关键指令,根据 regex (正则表达式)部分内容, 重定向到 replacement,结尾是 flag 标记。 rewrite [flag]; 关键字 正则 替代内容 flag 标记 关键字:其中关键字 error_log 不能改变 正则:perl 兼容正则表达式语句进行规则匹配 替代内容:将正则匹配的内容替换成 replacement flag 标记:rewrite 支持的 flag 标记 rewrite 参数的标签段位置: server,location,if flag 标记说明: last #本条规则匹配完成后,继续向下匹配新的 location URI 规则 break #本条规则匹配完成即终止,不再匹配后面的任何规则 redirect #返回 302 临时重定向,浏览器地址会显示跳转后的 URL 地址 permanent #返回 301 永久重定向,浏览器地址栏会显示跳转后的 URL 地址
网关服务器 应用服务器防火墙配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 开启防火墙 systemctl start firewalld 重启防火墙 systemctl restart firewalld 重载规则 firewall-cmd --reload 查看已配置规则 firewall-cmd --list-all 指定端口和ip访问 firewall-cmd --permanent --add-rich-rule="rule family=" ipv4" source address=" 192.168.1.101" port protocol=" tcp" port=" 8080" accept" 移除规则 firewall-cmd --permanent --remove-rich-rule="rule family=" ipv4" source address=" 192.168.1.101" port port=" 8080" protocol=" tcp" accept" 网关配置 upstream httpds { server 192.168.1.102 weight=8 down; server 192.168.1.104:8888 weight=1 backup; } server { listen 80; server_name localhost; location / { rewrite ^/([0-9]+).html$ /hello?pageNum=$1 break ; proxy_pass http://httpds; }
防盗链 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 valid_referers 192.168.44.101;if ($invalid_referer ) {return 403; } location ~*/(css|js|fonts|images) { valid_referers 192.168.1.101; if ($invalid_referer ) { return 403; } root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~*/(css|js|fonts|images) { valid_referers none 192.168.1.101; if ($invalid_referer ) { return 403; } root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
curl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 [root@xiamu04 admin] valid_referers none 192.168.1.101; [root@xiamu04 admin] <img src="images/photos/user-avatar.png" > [root@xiamu04 admin] HTTP/1.1 200 OK Server: nginx/1.22.1 Date: Fri, 28 Oct 2022 11:47:22 GMT Content-Type: text/html Content-Length: 42 Connection: keep-alive Accept-Ranges: bytes ETag: W/"42-1666955317000" Last-Modified: Fri, 28 Oct 2022 11:08:37 GMT [root@xiamu04 admin] HTTP/1.1 200 OK Server: nginx/1.22.1 Date: Fri, 28 Oct 2022 11:49:32 GMT Content-Type: image/png Content-Length: 15649 Last-Modified: Thu, 27 Oct 2022 08:55:50 GMT Connection: keep-alive ETag: "635a4796-3d21" Accept-Ranges: bytes [root@xiamu04 admin] HTTP/1.1 403 Forbidden Server: nginx/1.22.1 Date: Fri, 28 Oct 2022 11:49:43 GMT Content-Type: text/html Content-Length: 153 Connection: keep-alive
盗链资源返回页面或者提示图片 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 location ~*/(css|js|fonts|images) { valid_referers none 192.168.1.101; if ($invalid_referer ) { return 401; } root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } error_page 401 /401.html; location = /401.html { root html; } location ~*/(css|js|fonts|images) { valid_referers none 192.168.1.101; if ($invalid_referer ) { rewrite ^/ /images/x.png break ; } root html; index index.html index.htm; }
安装 Keepalived 常见报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. !!! [root@xiamu01 keepalived-2.2.7] --> 正在检查事务 ---> 软件包 mariadb-libs.x86_64.1.5.5.68-1.el7 将被 安装 --> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 需要 --> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 需要 --> 解决依赖关系完成 错误:软件包:1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 (updates) 需要:libmysqlclient.so.18()(64bit) 错误:软件包:1:net-snmp-agent-libs-5.7.2-49.el7_9.2.x86_64 (updates) 需要:libmysqlclient.so.18(libmysqlclient_18)(64bit) 您可以尝试添加 --skip-broken 选项来解决该问题 您可以尝试执行:rpm -Va --nofiles --nodigest [root@xiamu01 opt] [root@xiamu01 opt]
yum 安装 Keepalived
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 [root@xiamu01 ~] 配置 使用yum安装后配置文件在 /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id lb111 } vrrp_instance atguigu { state MASTER interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 } } [root@xiamu01 ~] ! Configuration File for keepalived global_defs { router_id lb110 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 } }
查看 ip
1 2 3 4 5 6 7 8 9 10 11 [root@xiamu01 ~] 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link /ether 00:50:56:3e:9d:2e brd ff:ff:ff:ff:ff:ff inet 192.168.1.101/24 brd 192.168.1.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.1.200/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fe3e:9d2e/64 scope link valid_lft forever preferred_lft forever
启动服务
1 2 3 4 [root@xiamu01 ~] [root@xiamu01 ~] [root@xiamu01 ~]
使用 192.168.1.200 也能够访问到页面了
Https 证书配置 不安全的 http 协议
非对称加密算法 另一种同样不安全的非对称加密算法
CA 机构参与保证互联网安全 查看操作系统的证书 cmd => certmgr.msc
openssl openssl 包含:SSL 协议库、应用程序以及密码算法库
自签名 openssl
图形化工具 XCA 下载地址 https://www.hohnstaedt.de/xca/index.php/download
CA 签名 域名的申请 安装 lnmphttps://oneinstack.com/
bbs 论坛https://discuz.com/