nginx续1:

八、虚拟主机配置

基于域名的虚拟主机

root@server2 \~\]# ps -au\|grep nginx //查看进程 修改Nginx服务配置,添加相关虚拟主机配置如下 1. \[root@proxy \~\]# vim /usr/local/nginx/conf/nginx.conf 2. .. .. 3. server { 4. listen 80; //端口 5. server_name www.a.com; //域名 6. auth_basic "Input Password:"; //认证提示符 7. auth_basic_user_file "/usr/local/nginx/pass"; //认证密码文件 8. location / { 9. root html; //指定网站根路径 10. index index.html index.htm; 11. } 12. 13. } 14. ... ... 15. 16. server { 17. listen 80; //端口 18. server_name www.b.com; //域名 19. location / { 20. root web; //指定网站根路径 21. index index.html index.htm; 22. } \[root@localhost \~\]# mkdir /usr/local/nginx/web //创建网页根目录 \[root@localhost \~\]# echo "web" \> /usr/local/nginx/web/index.html //写测试页面 \[root@localhost \~\]# /usr/local/nginx/sbin/nginx -s reload //重新加载 客户机测试: \[root@localhost \~\]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.1.134 www.a.com www.b.com ## 九、nginx反向代理配置 ⽤户直接访问反向代理服务器就可以获得⽬标服务器(后端服务器)的资源。 ### 1、修改配置 在配置⽂件中添加⼀⾏反向代理块指令(proxy_pass),表示当访问本机地址 192.168.1.125的 80 端⼝时即可跳转到后端服务器 192.168.1.100 的 80 端⼝上。 \[root@server2 \~\]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://192.168.1.100:80; } \[root@server2 \~\]# /usr/local/nginx/sbin/nginx -s reload ### 2、建立后端服务器 去server1: 也安装了nginx \[root@server1 \~\]# vim /usr/local/nginx/html/index.html 这里是192.168.1.100 ### 3、访问测试 浏览器测试,输入server2的地址192.168.1.125 ![](https://i-blog.csdnimg.cn/direct/4c2beeb7d25045a28816d15831e0f119.png) ## 十、nginx访问IP黑名单 ### 1、修改配置 \[root@server2 \~\]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost; allow 192.168.1.225; //允许192.168.1.225用户访问 deny 192.168.1.0/24; //拒绝1.0网段的用户访问 deny all; //拒绝所有,哪条在前哪条优先级高 #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://192.168.1.100:80; } \[root@server2 \~\]# /usr/local/nginx/sbin/nginx -s reload ### 2、真机浏览器访问 因为当前服务器拒绝了 1.0 ⽹段的⽤户访问,⽽本机浏览器正是通过 1.254 ⽹关与服务器建⽴连接,所以浏览器被拒绝访问了,显示 403 错误信息。 ### 3、另找一台虚拟机(192.168.1.225)访问 \[root@web \~\]# curl 192.168.1.125 我是192.168.1.100 //访问成功 ## 十一、负载均衡 ### 1、环境准备 四台虚拟机都安装了nginx staticserver ip 192.168.1.250 server1 ip 192.168.1.100 server2 ip 192.168.1.125 server3 ip 192.168.1.225 写一下测试页面,便于区分 \[root@staticserver \~\]# echo "I am static server" \> /usr/local/nginx/html/index.html \[root@server1 \~\]# echo "I am server1" \> /usr/local/nginx/html/index.html \[root@server2 \~\]# echo "I am server2" \> /usr/local/nginx/html/index.html \[root@server3 \~\]# echo "I am server3" \> /usr/local/nginx/html/index.html ### 2、配置,在staticserver里 \[root@staticserver \~\]# vim /usr/local/nginx/conf/nginx.conf upstream servers { //#upstream模块要写到http的里面 server 192.168.1.100:80; server 192.168.1.125:80; server 192.168.1.225:80; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { # root html; # index index.html index.htm; proxy_pass http://servers; #通过proxy_pass将用户的请求转发给servers集群,他的语句优先级高于root,所以放root前面和后面都可以 \[root@staticserver \~\]# /usr/local/nginx/sbin/nginx -s reload ![](https://i-blog.csdnimg.cn/direct/76135c48075545af9aaecbdafe96d02c.png) ### 3、访问测试 真机浏览器访问staticserverIP192.168.1.250,可以看到另外三台主机的页面,点击刷新即可 ### 4、配置upstream服务器集群池属性 #### 1)七层负载均衡基础配置 ![](https://i-blog.csdnimg.cn/direct/cf16571c198c45eb9556c66c08181a14.png) #### 2)负载均衡状态 ![](https://i-blog.csdnimg.cn/direct/81e36fa9d21140d2a8f5fdd672e30257.png) 在服务器组的组内服务器后填写该服务器的状态,如: #### ![](https://i-blog.csdnimg.cn/direct/8efc0f1859cc41488429277dffa55dcf.png) #### 3)负载均衡策略 ##### (1)轮询 ![](https://i-blog.csdnimg.cn/direct/d9d7340e429b48d8a23773d860f03c9c.png) ##### (2)weight 加权 ![](https://i-blog.csdnimg.cn/direct/5ec245ee18524cee9bcaecc5cb9244e6.png) ##### (3)ip_hash 当对后端的多台动态应用服务器做负载均衡时,ip_hash指令能够将某个客户端IP的请求通过哈希算法定位到同一台后端服务器上。 这样,当来自某一个IP的用户在后端Web服务器A上登录后,再访问该站点的其他URL,能保证其访问的还是后端web服务器A。 注意: 使用ip_hash指令无法保证后端服务器的负载均衡,可能导致有些后端服务器接收到的请求多,有些后端服务器接受的请求少,而且设置后端服务器权重等方法将不起作用。 ![](https://i-blog.csdnimg.cn/direct/72e527f1595345e1a500fdf92b277e7a.png) ##### (4)least_conn least_conn:最少连接,把请求转发给连接数较少的后端服务器。轮询算法是把请求平均地转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,leastconn这种方式就可以达到更好的负载均衡效果。 ![](https://i-blog.csdnimg.cn/direct/9a971f1ec47b493e826925a212775449.png) ##### (5)url_hash 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用ur_hash,可以使得同一个url (也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再次收到请求,就可以从缓存中读取。 ## ![](https://i-blog.csdnimg.cn/direct/fda5e97b371645678919fe37639615b0.png) 十二、平滑升级(不停止服务的情况下) \[root@server1 \~\]# /usr/local/nginx/sbin/nginx //先保证原有服务使启动的 \[root@server1 \~\]# wget https://nginx.org/download/nginx-1.27.0.tar.gz //下载新版本 \[root@server1 \~\]# tar -zxvf nginx-1.27.0.tar.gz //解压 \[root@server1 nginx-1.27.0\]# cd nginx-1.27.0/ \[root@server1 nginx-1.27.0\]# ./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@server1 nginx-1.27.0\]# make \&\& make install \[root@server1 nginx-1.27.0\]# ls /usr/local/nginx/sbin/ nginx nginx.old \[root@server1 nginx-1.27.0\]# /usr/local/nginx/sbin/nginx -v //查看版本 nginx version: nginx/1.27.0 \[root@server1 nginx-1.27.0\]# /usr/local/nginx/sbin/nginx.old -v //旧版本 nginx version: nginx/1.26.1 \[root@server1 nginx-1.27.0\]# ps -aux\|grep nginx //查看进程,找到老版本pid编号 root 7838 0.0 0.2 46096 1144 ? Ss 13:40 0:00 nginx: master process ./sbin/nginx nginx 7839 0.0 0.4 46544 2152 ? S 13:40 0:00 nginx: worker process root 10940 0.0 0.2 112720 968 pts/2 R+ 16:34 0:00 grep --color=auto nginx \[root@server1 nginx-1.27.0\]# kill -USR2 7838 //使用kill -USR2 启用新版本的Nginx的软件,7838是老版本的pid编号 \[root@server1 nginx-1.27.0\]# ps -aux\|grep nginx root 7838 0.0 0.2 46096 1332 ? Ss 13:40 0:00 nginx: master process ./sbin/nginx nginx 7839 0.0 0.4 46544 2152 ? S 13:40 0:00 nginx: worker process root 10941 0.0 0.6 46096 3324 ? S 16:37 0:00 nginx: master process ./sbin/nginx nginx 10942 0.0 0.3 46548 1916 ? S 16:37 0:00 nginx: worker process root 10944 0.0 0.2 112720 964 pts/2 R+ 16:37 0:00 grep --color=auto nginx \[root@server1 nginx-1.27.0\]# kill -WINCH 7839 //优雅关闭子进程 \[root@server1 nginx-1.27.0\]# ps -aux\|grep nginx root 7838 0.0 0.2 46096 1332 ? Ss 13:40 0:00 nginx: master process ./sbin/nginx root 10941 0.0 0.6 46096 3324 ? S 16:37 0:00 nginx: master process ./sbin/nginx nginx 10942 0.0 0.3 46548 1916 ? S 16:37 0:00 nginx: worker process nginx 10947 0.0 0.3 46544 1908 ? S 16:39 0:00 nginx: worker process root 10949 0.0 0.2 112720 964 pts/2 R+ 16:39 0:00 grep --color=auto nginx \[root@server1 nginx-1.27.0\]# kill -QUIT 7838 //优雅关闭主进程 \[root@server1 nginx-1.27.0\]# ps -aux\|grep nginx root 10941 0.0 0.6 46096 3324 ? S 16:37 0:00 nginx: master process ./sbin/nginx nginx 10942 0.0 0.3 46548 1916 ? S 16:37 0:00 nginx: worker process root 10953 0.0 0.2 112720 968 pts/2 R+ 16:40 0:00 grep --color=auto nginx \[root@server1 nginx-1.27.0\]# curl -I localhost //使用curl 查看当前服务器的版本 HTTP/1.1 200 OK Server: nginx/1.27.0 //已经更新成1.27版本了 Date: Tue, 30 Jul 2024 08:41:05 GMT Content-Type: text/html; charset=utf-8 Content-Length: 13 Last-Modified: Tue, 30 Jul 2024 07:02:16 GMT Connection: keep-alive ETag: "66a88ff8-d" Accept-Ranges: bytes ## 十三、nginx代理tomcat10 配置tomcat10 运行环境 tomcat9可以在jdk8的环境运行 tomcat10必须在jdk17以上的版本运行 \[root@server1 \~\]# tar -xf jdk-22_linux-x64_bin.tar.gz \[root@server1 \~\]# mv jdk-22.0.1/ /usr/local/jdk/ //把解压后的文件移动到/usr/local/jdk/方便管理配置 \[root@server1 \~\]# cd /usr/local/jdk/ \[root@server1 jdk\]# sed -i '$aexport JAVA_HOME=/usr/local/jdk/' /etc/profile \[root@server1 jdk\]# sed -i '$aPATH=$JAVA_HOME/bin:$PATH' /etc/profile \[root@server1 jdk\]# source /etc/profile //使配置文件生效 \[root@server1 jdk\]# java -version //查看版本 java version "22.0.1" 2024-04-16 Java(TM) SE Runtime Environment (build 22.0.1+8-16) Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)

相关推荐
木下-俱欢颜40 分钟前
搭建基于chrony+OpenSSL(NTS协议)多层级可信时间同步服务
运维·网络安全·udp·ssl
旧故新长1 小时前
访问 Docker 官方镜像源(包括代理)全部被“重置连接”或超时
运维·docker·容器
GBXLUO2 小时前
如何使用远程桌面控制电脑
服务器
柳如烟@2 小时前
在Rocky Linux 9.5上部署MongoDB 8.0.9:从安装到认证的完整指南
linux·运维·mongodb
搬码临时工2 小时前
电脑怎么远程访问服务器?4种常见的简单方法
运维·服务器·网络·异地访问
QQ2740287563 小时前
Kite AI 自动机器人部署教程
linux·运维·服务器·人工智能·机器人·web3
文牧之3 小时前
PostgreSQL 配置设置函数
运维·数据库·postgresql
.小墨迹3 小时前
Apollo学习——planning模块(3)之planning_base
linux·开发语言·c++·学习·自动驾驶
K龙4 小时前
私有资产测绘&安全流水线Shovel
运维·安全·开发·其它
影龙帝皖4 小时前
Linux服务之lvs+keepalived nginx+keepalived负载均衡实例解析
linux·nginx·lvs