Nginx(详解以及如何使用)

目录

[1. 什么是Nginx?](#1. 什么是Nginx?)

[2. 为什么使用nginx?](#2. 为什么使用nginx?)

[3. 安装nginx](#3. 安装nginx)

3.1?安装nginx的依赖插件

[3.2 下载nginx](#3.2 下载nginx)

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

?3.4?解压

?3.5?进入解压后的目录

3.6?指定nginx的安装路径

?3.7?编译和安装nginx

[3.8 启动nginx](#3.8 启动nginx)

?3.9?访问nginx

?4.?nginx目录结构

?5.nginx配置文件

[6. nginx的核心功能](#6. nginx的核心功能)

[6.1 nginx反向代理功能](#6.1 nginx反向代理功能)

[6.2 nginx的负载均衡](#6.2 nginx的负载均衡)

[6.3 nginx动静分离](#6.3 nginx动静分离)

[7. nginx的HA高可用的搭建](#7. nginx的HA高可用的搭建)

[7.1 高可用的原理--keepalived](#7.1 高可用的原理–keepalived)

[7.2 搭建ha高可用](#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

相关推荐
七夜zippoe5 小时前
CANN Runtime任务描述序列化与持久化源码深度解码
大数据·运维·服务器·cann
盟接之桥5 小时前
盟接之桥说制造:引流品 × 利润品,全球电商平台高效产品组合策略(供讨论)
大数据·linux·服务器·网络·人工智能·制造
Fcy6486 小时前
Linux下 进程(一)(冯诺依曼体系、操作系统、进程基本概念与基本操作)
linux·运维·服务器·进程
袁袁袁袁满6 小时前
Linux怎么查看最新下载的文件
linux·运维·服务器
代码游侠6 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法
主机哥哥7 小时前
阿里云OpenClaw部署全攻略,五种方案助你快速部署!
服务器·阿里云·负载均衡
Harvey9037 小时前
通过 Helm 部署 Nginx 应用的完整标准化步骤
linux·运维·nginx·k8s
珠海西格电力科技8 小时前
微电网能量平衡理论的实现条件在不同场景下有哪些差异?
运维·服务器·网络·人工智能·云计算·智慧城市
释怀不想释怀8 小时前
Linux环境变量
linux·运维·服务器
zzzsde8 小时前
【Linux】进程(4):进程优先级&&调度队列
linux·运维·服务器