linux学习笔记整理: 关于linux:nginx服务器 2024/7/20;
nginx服务器:
自理解:
nginx是一种分发式服务器,统一进入的接口,并将进入的用户进行指定性分发给不同服务器地址交互;
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。并发能力: 50,000
京东,淘宝,12306,新浪等都在使用nginx相关服务器
安装nginx:
nginx可以独立安装在一台服务器--也可以和项目在同一个服务器。
-
安装nginx的依赖插件
前提已安装阿里云插件
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
-
下载nginx
nginx: download
-
创建一个目录作为nginx的安装路径
mkdir /usr/nginx
-
解压
tar -zxvf nginx-1.26.1.tar.gz
-
进入解压后的目录
cd nginx-1.26.1
-
指定nginx的安装路径
./configure --prefix=/usr/nginx
-
编译和安装nginx
make install
启用nginx:
nginx目录结构
conf 配置目录
html 静态资源
logs 日志目录 (用于排错)
sbin 脚本目录(启动和关闭nginx)
启动nginx
./nginx 启动
./nginx -s stop 关闭
./nginx -s reload 重新加载配置文件
访问nginx 80
http://nginx所在的ip:nginx的端口/
若无法连接注意端口放行
配置nginx:
.../nginx/conf/nginx.conf
#user nobody; #工作的线程数 worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { # 每个工作对象允许的连接数 worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; server { listen 81; server_name localhost; location /{ root static; index main.html; } } server { #访问的端口号 listen 82; server_name localhost; location /{ # 代理的服务器地址 proxy_pass http://192.168.111.132:8080; } } #gzip on; server { listen 80; # 监听的端口号 server_name localhost; # 监听的主机名.域名 #charset koi8-r; #access_log logs/host.access.log main; # 资源/ location / { root html; #根目录 index index.html main.html; # 资源 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
负载均衡(轮询制):
负载均衡(Load Balance [4])其意思就是把请求分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
web项目必须搭建的为集群模式。
启用springBoot项目/TomCat部署:
将springBoot项目打包放入Linux系统中;
端口号放行
使用以下代码启动springBoot打包好的文件;
java -jar 文件名.jar
配置nginx:
.../nginx/conf/nginx.conf
upstream gzx{ server 172.16.7.17:8080; server 192.168.85.126:8080; } server { listen 82; server_name localhost; location /{ proxy_pass http://gzx; } }重新加载nginx配置文件:
.../nginx/sbin/nginx -s reload
负载均衡(权重制):
权重策略: 服务器硬件配置不同时。
upstream gzx{ #通过使用weight=3设置比重,访问3次172才会访问1次192; server 172.16.7.17:8080 weight=3; server 192.168.85.126:8080 weight=1; } server { listen 82; server_name localhost; location /{ proxy_pass http://gzx; } }
负载均衡(IP制):
根据访问者客户的ip固定访问对应的web服务器。
upstream gzx{ server 172.16.7.17:8080; server 192.168.85.126:8080; #通过访问者IP固定其访问接口; ip_hash; } server { listen 82; server_name localhost; location /{ proxy_pass http://gzx; } }
动静分离:
动:动态资源[接口] 静:静态资源 [css js image]。
分离:
把静态资源放入nginx服务器下。动态资源web服务器下。
作用: 不需要将不会改变的东西重复的从服务器调用至nginx在发送给客户;而是直接从nginx中发送给客户;
操作:
首先创建对应目录并将静态资源放入nginx中,
/nignx/static/js等目录下
然后打开nginx配置文件
server{ listen 81; server_name localhost; location /{ proxy_pass http://使用静态资源的IP:端口; } location ~ \.js|.css|.jpg|....|.png$ { root static; } }
HA高可用搭建:
高可用的原理keepalived:
通过部署多个nginx服务器防止主服务器崩溃无法访问;设置备用服务器
| 服务器:132 |
| nginx服务器:88 |-> |----------------|
客户
->虚拟IP
->:|---------------------| | 服务器:133 |...| nginx服务器:89 |...
搭建方式:
安装keepalived
yum install -y keepalived
默认安装至/etc/keepalived下
修改
keepalived.conf
配置文件主机:
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # ip的地址 smtp_ server 192.168.111.188 smtp_connect_timeout 30 router_id 192.168.111.188 } # 执行脚本 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 # 每2s执行一次该脚本 weight -20 # keepalive宕机 权重-20 优先级 } vrrp_instance VI_1 { state MASTER # 角色 interface ens33 # 网卡名 virtual_router_id 51 # id 保证主从相同 priority 100 # 优先级 主节点大于从节点 advert_int 1 authentication { auth type PASS auth pass 1111 } virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开 } track_script { chk_http_port # 追踪nginx脚本 } }备用机:
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # ip的地址 smtp_ server 192.168.111.189 smtp_connect_timeout 30 router_id 192.168.111.189 } # 执行脚本 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 # 每2s执行一次该脚本 weight -20 # keepalive宕机 权重-20 优先级 } vrrp_instance VI_1 { state BACKUP # 角色 interface ens33 # 网卡名 virtual_router_id 51 # id 保证主从相同 priority 90 # 优先级 主节点大于从节点 advert_int 1 authentication { auth type PASS auth pass 1111 } virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开 } track_script { chk_http_port # 追踪nginx脚本 } }(上方配置文件内地址)放于/usr/local/src/nginx_check.sh
#!/bin/bash # 检查是否开启nginx---统计nginx进程的个数 A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then pkill -9 keepalived fi解释文件:
A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then #如果nginx没有启动就启动nginx /app/nginx/sbin/nginx #重启nginx if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移 pkill keepalived fi fi修改权限: chmod 777 nginx_check.sh
启动:
.../nginx/sbin/nginx
keepalived systemctl start|stop keepalived
访问时直接访问虚拟地址即可
yum卸载
yum remove keepalived
删除相关文件
find / -name keepalived
/etc/selinux/targeted/tmp/modules/100/keepalived
/etc/selinux/targeted/active/modules/100/keepalived
rm -rf /etc/selinux/targeted/tmp/modules/100/keepalived
rm -rf /etc/selinux/targeted/active/modules/100/keepalived
卸载keeplived工作路径
cd /home/hd/keepalived-1.2.18
make uninstall
删除相关文件
查看相关文件: find / -name keepalived
删除相关文件
rm -rf /run/lock/subsys/keepalived rm -rf /etc/keepalived rm -rf /etc/sysconfig/keepalived rm -rf /etc/rc.d/init.d/keepalived rm -rf /usr/sbin/keepalived rm -rf /usr/local/keepalived rm -rf /home/hd/keepalived-1.2.18/keepalived rm -rf /home/hd/keepalived-1.2.18/keepalived/etc/keepalived rm -rf /home/hd/keepalived-1.2.18/bin/keepalived
原文地址:https://blog.csdn.net/gzx233/article/details/140565537
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!