Nginx(详解以及如何使用)

目录

1. 什么是Nginx?

2. 为什么使用nginx?

3. 安装nginx

3.1 安装nginx的依赖插件

3.2 下载nginx

 3.3 创建一个目录作为nginx的安装路径

 3.4 解压

 3.5 进入解压后的目录

3.6 指定nginx的安装路径

 3.7 编译和安装nginx

3.8 启动nginx

 3.9 访问nginx

 4. nginx目录结构

 5.nginx配置文件

6. nginx的核心功能

6.1 nginx反向代理功能

6.2 nginx的负载均衡

6.3 nginx动静分离

7. nginx的HA高可用的搭建

7.1 高可用的原理--keepalived

7.2 搭建ha高可用


1. 什么是Nginx?

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

并发能力: 50,000 。

2. 为什么使用nginx?

  • Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数。
  • Nginx支持热部署,启动简单,可以做到7*24不间断运行。几个月都不需要重新启动。

3. 安装nginx

nginx可以独立安装在一台服务器--也可以和项目在同一个服务器。

3.1 安装nginx的依赖插件

yum  install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

如果yum这个命令不能用,请看这篇:Linux查看端口号命令以及yum源无法使用的解决方法-CSDN博客

3.2 下载nginx

nginx: download

 3.3 创建一个目录作为nginx的安装路径

mkdir /usr/nginx

 3.4 解压

tar -zxvf nginx-1.26.1.tar.gz

 3.5 进入解压后的目录

cd nginx-1.26.1

3.6 指定nginx的安装路径

 ./configure --prefix=/usr/nginx

 3.7 编译和安装nginx

make install

3.8 启动nginx

在sbin目录下操作或者配置环境使在任何地方都可以使用:

nginx  启动
nginx -s stop  关闭
nginx -s reload 重新加载配置文件

 3.9 访问nginx

http://nginx所在的ip:nginx的端口/

默认端口号:80 

 4. nginx目录结构

 5.nginx配置文件

 在/usr/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;
          
       }
    }

    #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;
    #    }
    #}

}

6. nginx的核心功能

6.1 nginx反向代理功能

正向代理

代理的为客户端,对于服务器不知道真实客户的信息。例如:翻墙软件。

 反向代理服务器

代理的为服务器端。对于客户来说不知道服务器的信息。例如: nginx。

 项目部署例:

 准备web项目

 准备nginx

启动web项目:

 配置nginx:

   server {
       listen 82;
       server_name localhost;
       location /{
           # 代理的服务器地址
          proxy_pass   http://192.168.111.132:8080;
       }
    }

 启动ngin:

./usr/nginx/sbin/nginx

6.2 nginx的负载均衡

负载均衡(Load Balance [4]):其意思就是把请求分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

web项目必须搭建的为集群模式。

 web服务器项目至少搭建2台以上。

运行两个web工程项目:

springboot项目:

 运行springboot项目:java -jar xxx.jar

注意: 端口号别忘记放行。

 配置nginx完成负载均衡:

 重新加载nginx配置

/usr/nginx/sbin/nginx -s reload

测试

 负载均衡的策略

默认为轮询。

权重策略: 服务器硬件配置不同时。

 ip_hash策略: 根据访问者客户的ip固定访问对应的web服务器。

 花钱买第三方策略插件。

6.3 nginx动静分离

动:动态资源(接口)

 静:静态资源 (css js image)。

分离: 之前我们把静态资源和动态资源全部放在web服务器下。 把静态资源放入nginx服务器下。动态资源web服务器下。

7. nginx的HA高可用的搭建

7.1 高可用的原理--keepalived

7.2 搭建ha高可用

俩台机器:

 安装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脚本
	}
	
}

 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         
keepalived    systemctl start|stop keepalived

相关推荐

  1. Nginx使用详解

    2024-07-22 02:12:01       29 阅读
  2. PostCSS安装以及使用详解

    2024-07-22 02:12:01       38 阅读
  3. nginx配置详解+nginx_lua模块的使用

    2024-07-22 02:12:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 02:12:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 02:12:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 02:12:01       45 阅读
  4. Python语言-面向对象

    2024-07-22 02:12:01       55 阅读

热门阅读

  1. ubuntu 上安装软件

    2024-07-22 02:12:01       18 阅读
  2. ubuntu系统下安装配置 8.0.37的MySQL

    2024-07-22 02:12:01       15 阅读
  3. Keras和Pytorch输入图像的张量维度

    2024-07-22 02:12:01       16 阅读
  4. 中介子方程六十五

    2024-07-22 02:12:01       20 阅读
  5. linux命令

    2024-07-22 02:12:01       17 阅读
  6. 1186. 删除一次得到子数组最大和

    2024-07-22 02:12:01       19 阅读
  7. GPT-LLM

    GPT-LLM

    2024-07-22 02:12:01      17 阅读
  8. 【开源库学习】libodb库学习(二)

    2024-07-22 02:12:01       15 阅读
  9. Vue-Plugin-HiPrint 打印设计

    2024-07-22 02:12:01       16 阅读