Nginx学习笔记分享(安装、配置SSL、反向代理、负载均衡、动静分离、高可用集群等)

1、nginx安装

1.1、安装 pcre 依赖

1、下载 pcre tar.gz压缩包(可从pcre官网中下载),并把压缩文件放入linux系统中。
在这里插入图片描述
注意:nignx的压缩包也下载好了。

2、解压压缩文件

tar -xvf pcre-8.37.tar.gz -C /usr/src

在这里插入图片描述
3、进入解压之后的目录,执行./configure

如:

# 编译与检查环境
[howie@laizhenghua src]# cd pcre-8.37/
[howie@laizhenghua pcre-8.37]# ./configure

4、再次执行make && make install

如:

# 编译与安装
[howie@laizhenghua pcre-8.37]# make && make install

查看pcre依赖是否安装成功(查看版本号):

[howie@laizhenghua pcre-8.37]# pcre-config --version
8.37

1.2、安装openssl与zlib等其他依赖

安装这两个依赖,我们也可以像安装pcre一样,先手动下载然后解压执行编译安装命令。但是此方法比较麻烦,我们有更简单的安装方式(使用yum命令)。所以安装opensslzlib就使用yum命令进行安装。

我们只需执行一条命令:

[howie@laizhenghua pcre-8.37]$ sudo yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

注意以上命令,也安装了其他依赖,不止opensslzlib

1.3、安装nginx

1、解压缩nginx-xx.tar.gz包。

如:

[howie@laizhenghua soft]$ cd nginx
[howie@laizhenghua nginx]$ ls
nginx-1.12.2.tar.gz  pcre-8.37.tar.gz
# 解压压缩包
[howie@laizhenghua nginx]# sudo tar -xvf nginx-1.12.2.tar.gz -C /usr/src

在这里插入图片描述
2、进入解压缩目录,执行./configure

如:

[howie@laizhenghua src]# cd nginx-1.12.2/
[howie@laizhenghua nginx-1.12.2]# ./configure

3、再次执行make && make install

如:

[howie@laizhenghua nginx-1.12.2]# make && make install

所有东西安装好后,会在usr/local目录下多出来一个文件夹nginx。我们可以查看是否安装成功:

在这里插入图片描述

1.4、启动nginx

[root@VM-0-17-centos sbin]# pwd
/usr/local/nginx/sbin
[root@VM-0-17-centos sbin]# ./nginx
[root@VM-0-17-centos sbin]# ps -ef | grep nginx
root      1549     1  0 16:35 ?        00:00:00 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
www       1569  1549  0 16:35 ?        00:00:00 nginx: worker process
www       1570  1549  0 16:35 ?        00:00:00 nginx: cache manager process
root      2019     1  0 16:37 ?        00:00:00 nginx: master process ./nginx
nobody    2020  2019  0 16:37 ?        00:00:00 nginx: worker process
root      2096  1754  0 16:37 pts/0    00:00:00 grep --color=auto nginx
[root@VM-0-17-centos sbin]# 

浏览器访问ip地址,出现以下界面表示安装成功。
在这里插入图片描述
如果防火墙没有开发我们指定的端口,nginx是无法访问的。所以我们还要检查与开放防火墙端口。

查看防火墙开放的端口

[root@laizhenghua conf]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ssh
  ports: 20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 8080/tcp 8080/udp 443/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@laizhenghua conf]#

我们发现以上nignx默认端口80已经开放。如果没有开放我们可以使用如下命令开放:

firewall-cmd --add-port=80/tcp --permanent

# 添加成功后,重启防火墙
firewall-cmd --reload

2、nginx常用的命令

注意:如果没有配置nignx的全局环境变量,使用nginx操作命令前提条件是必须进入nignx的目录(/usr/local/nginx/sbin

比较常用的命令有:

1、查看nginx的版本号

[root@laizhenghua sbin]# pwd
/usr/local/nginx/sbin
[root@laizhenghua sbin]# ./nginx -v
nginx version: nginx/1.12.2

2、启动nginx

[root@VM-0-17-centos sbin]# ./nginx
[root@laizhenghua sbin]#

3、关闭nginx

[root@laizhenghua sbin]# ./nginx -s stop
[root@laizhenghua sbin]# 

[root@laizhenghua sbin]# ps -ef | grep nginx
root      1549     1  0 16:35 ?        00:00:00 nginx: master process /www/server/nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
www       1569  1549  0 16:35 ?        00:00:00 nginx: worker process
www       1570  1549  0 16:35 ?        00:00:00 nginx: cache manager process
root      6562  3380  0 17:06 pts/0    00:00:00 grep --color=auto nginx

4、重新加载nginx

[root@laizhenghua sbin]# ./nginx -s reload
[root@laizhenghua sbin]#

3、nginx的配置文件

首先需要知道的是nignx配置文件所在的位置:
在这里插入图片描述
或者使用whereis nginx.conf命令查看。

nginx配置文件的组成(三部分):

  1. 全局块
  2. events块
  3. http块

1、全局块

/*
从配置文件到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置命令。
主要配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程IPD存放路径、
日志存放路径和类型以及配置文件的引入等。

如以下配置
*/

#user  nobody;
worker_processes  1;
// 这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量越多,但是会受到硬件、软件等设备的制约。

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

2、events块

/*
events 块设计的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启多 work process 下的网络连接进行序列化、
是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持最大连接
数等。

以下配置就表示每个 work process 支持的最大连接数为 1024
这部分的配置对象 Nginx 的性能影响较大,在实际中应该灵活配置
*/
events {
    worker_connections  1024;
}

3、http块

/*
1、这是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里
2、需要注意的是:http 块也可以包括 http全局块、server块
3、http块有分为http全局块和server块:
	1.http全局块:可配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等
	2.server块:这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术
		的产生是为了节省互联网服务器的硬件成本
		① 全局server块:最常见的 配置是监听的端口和主机名称或IP配置
		② location块:一个 server 块可以配置多个 location 块。
			这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如server_name/uri-string),对主机名称(也可以是IP别名)
			之外的字符串(例如 前面的/uri-string)进行匹配,对特定的请求进行处理,地址定向、数据缓存
			和应答控制等功能,还有许多第三方模块的配置也在这里进行。
			
*/
// http 全局块
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80; // Nginx 监听的端口
        server_name  localhost; // 主机名称(本地)
		
		// 路径
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

完整配置文件内容:

[root@laizhenghua conf]# cat 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;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

}
[root@laizhenghua conf]# 

4、Nginx配置实例

4.1、反向代理实例1

如果没有打包好的项目,我们是实现不了反向代理的。为了方便学习与实现反向代理效果,我们约定实现的效果为:

在浏览器中输入一个域名,跳转到linux系统tomcat主页面中。

实现步骤:

1、在Linux系统中安装Tomcat,使用默认的端口号8080,这一步省略。注意防火墙开放8080端口。
在这里插入图片描述
2、修改nginx配置文件nginx.conf

http{
	...
	server {
	   listen       80;
	   server_name  localhost;
	
	   #charset koi8-r;
	
	   #access_log  logs/host.access.log  main;
	
	   location / {
	       root   html;
		   proxy_pass http://www.laizhenghua.cn:8080; # 加上此属性,即可完成反向代理
	       index  index.html index.htm;
	   }
	   ...
  }

3、测试

浏览器输入域名进行访问,注意不加端口号。在没有反向代理之前我们需要加上8080端口才能访问到tomcat的主页面,加上反向代理后,我们只需访问域名就能访问到tomcat主页面。
在这里插入图片描述

4.2、反向代理实例2

需要实现的效果:使用 Nginx 反向代理,根据访问的路径跳转到不同端口的服务中。例如:

/*
1、访问 http://127.0.0.1:80/edu  跳转到127.0.0.1:8080
2、访问 http://127.0.0.1:80/vod  跳转到127.0.0.1:8081
*/

实现步骤:

1、准备两个 tomcat 服务器,一个8080端口,一个8081端口

2、创建文件夹与测试页面

如8080端口服务(在webapps目录下新建edu文件夹与index.html测试页面):

[root@laizhenghua edu]# pwd
/opt/apache-tomcat-9.0.46/webapps/edu
[root@laizhenghua edu]# ls
index.html
[root@laizhenghua edu]# cat index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h3>hello 8080</h3>
</body>
</html>
[root@laizhenghua edu]#

至于8081端口服务:

我们没有多余的服务器,并且一个服务器运行两个tomcat有点麻烦,所以就不弄了。

3、修改 Nginx 配置文件

# 这里使用了正则表达式,表示只要路径包含 edu 就跳转到指定的地址
location ~ /edu/ {
    proxy_pass   http://www.laizhenghua.cn:8080;
}

关于有正则和无正则:

  • location = xxx {}=用于不含正则表达式的 uri 前,要求请求字符串与 URI 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
  • location ~ xxx {}~用于表示uri包含正则表达式,并且区分大小写。
  • location ~* xxx {}~*用于表示uri包含正则表达式,并且不区分大小写 。
  • location ^~ xxx{}^~用于不包含正则表达式的uri前,要求Nginx服务器找到标识uri和请求字符串匹配度高的location后,立即使用此location处理请求,而不再使用location块中的正则uri和请求字符串做匹配。
  • 注意:如果uri包含正则表达式,则必须要有~或者~*标识。

4、测试

分别访问http://www.laizhenghua.cn/edu/index.html
在这里插入图片描述

4.3、负载均衡实例

需要实现的效果:浏览器地址栏输入http://www.laizhenghua.cn/edu/index.html,负载均衡效果,平均到80808081端口中。

实现步骤:

1、准备两台 tomcat服务器,一台8080、一台8081

2、在两台 tomcat 里面webapps目录中,创建edu文件夹,在edu中创建测试文件index.html,内容自定义。

3、在nginx的配置文件中进行负载均衡的配置

http {
	...
	upstream myserver {
		# 采用 weight 策略
		server localhost:8080 weight=1;
		server localhost:8081 weight=2;
	}
	...
	server {
		location / {
			...
			proxy_pass http:myserver;
			proxy_connect_timeout 10;
			...
		}
	}
}

4、测试

访问域名即可,刷新即可看到效果。

关于负载均衡:

/*
随着互联网信息的爆炸性增长,负载均衡(load balance) 已经不再是一个很陌生的话题,顾明思议,负载均衡即是
将负载分摊到不同的服务单元,既保证服务的可用性,又保证响应足够块,给用户很好的体验。快速增长的访问量和
数据流量催生了格式各样的负载均衡产品,很多专业的负载均衡硬件提供了很好的功能,但是价格不菲,这使得负载
均衡软件大受欢迎,Nginx就是其中的一个,在linux下有 Nginx、LVS、Haproxy等等服务可以提供负载均衡服务,
而且Nginx提供了几种分配方式(策略):

1、轮询(默认):
	每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight:
	1.weight代表权重。默认等于1,权重越高被分配的客户端越多。
	2.指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如:
		upstream server_pool {
			server 127.0.0.1:8080 weight=5;
			server 127.0.0.1:8081 weight=10;
		}
3、ip_hash
	1.每个请求按访问 ip 和 hash 结果分配,这样每个访问固定一个后端服务器,可以解决 session 的问题。
	2.例如:
		upstream server_pool {
			ip_hash;
			server 192.168.5.20:80;
			server 192.168.5.21:80;
		}
4、fair(第三方)
	1.按后端服务器的响应时间来分配请求,响应时间短的优先分配。
	2.如:
		upstream server_pool {
			server 192.168.5.20:80;
			server 192.168.5.21:80;
			fair;
		}
*/

4.4、动静分离实例

Nginx动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和静态页面物理分离。严格意义上说应该是动态请求和静态请求分开,可以理解成使用Nginx处理静态页面使用Tomcat处理动态页面

动静分离从目前实现角度来讲大致分为两种,一种就是纯粹把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案。另一种就是动态跟静态文件混合在一起发布,通过Nginx来分开。

通过location指定不同的后缀名实现不同的请求转发。通过exprires参数设置,可以使浏览器缓存过期时间,减少与服务器之间的请求和流量。具体Expires定义:给一个资源设定一个过期时间,也就是说无需去服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常合适不经常变动的资源(如果经常更新的文件,不建议使用Expires来缓存),我这里设置3d,表示在这3天之内访问这个URL,发送一个请求,对比服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码304,如果有修改,则直接从服务器重新下载,返回状态码200。

动静分离的实现:

1、准备静态资源(准备一个static文件夹),用于进行访问
在这里插入图片描述
2、修改Nginx配置文件

location /static/ {
	# root 表示去Linux系统目录下寻找
    root   /mydata/nginx;
    # proxy_pass http://www.laizhenghua.cn:8080;
    # index  index.html index.htm;
    autoindex on;
}

3、测试

注意:自己配置了ssl,所有访问时是https

浏览器访问https://www.laizhenghua.cn/static/js/
在这里插入图片描述
在这里插入图片描述

4.5、配置高可用集群

在这里插入图片描述
Nginx配置高可用Nginx集群,我们需要:

  1. 两台Nginx服务器
  2. keepalived
  3. 虚拟IP

实现步骤:

1、两台服务器都需要安装上Nginx。

2、两台服务器都需要安装keepalived

为了安装方便,我们使用yum命令安装:

yum install keepalived -y

# 如
[root@baidu logs]# yum install keepalived -y
Last metadata expiration check: 1:32:24 ago on Sun 23 May 2021 07:23:38 PM CST.
Dependencies resolved.
...

查看是否安装成功:

[root@baidu logs]# rpm -q -a keepalived
keepalived-2.0.20-2.fc32.x86_64

安装成功后会在/etc/keepalived生成一个配置文件keepalived.conf,我们需要使用这个配置文件做主从配置。

[root@laizhenghua keepalived]# pwd
/etc/keepalived
[root@laizhenghua keepalived]# ls
keepalived.conf

3、在主机和从机中修改keepalived.conf文件并添加检查服务器是否宕机的脚步nginx_check.sh,完成高可用集群配置。

主机配置:

# 修改 /etc/leepalived/keepalivec.conf 配置文件,文件内容如下

# 全局配置
global_defs {
	 notification_email {
		 acassen@firewall.loc
		 failover@firewall.loc
		 sysadmin@firewall.loc
	 }
	 notification_email_from Alexandre.Cassen@firewall.loc
	 smtp_server 192.168.17.129
	 smtp_connect_timeout 30
	 
	 router_id LVS_DEVEL # (重点参数)局域网 keppalived 主机身份标识信息(每台唯一)
	 # echo "127.0.0.1 LVS_DEVEL" >> /etc/hosts  
}
# 检测脚本配置
vrrp_script chk_http_port {
	script "/usr/local/src/nginx_check.sh"
	interval 2 #(检测脚本执行的间隔,例如:2s检测一次)
	weight 2
}
# 虚拟 ip 地址
vrrp_instance VI_1 {
	state MASTER # 备份服务器上将 MASTER 改为 BACKUP
	interface ens33 # 网卡
	virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
	priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 1111
	}
	virtual_ipaddress {
		192.168.17.50 # VRRP H 虚拟地址
	}
}

# 在 /usr/local/src/nginx_check.sh 上添加检测脚本,脚本内容如下

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
	/usr/local/nginx/sbin/nginx
	sleep 2
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	killall keepalived
	fi
fi

从机配置:

# 修改 /etc/leepalived/keepalivec.conf 配置文件,文件内容如下
global_defs {
	 notification_email {
		 acassen@firewall.loc
		 failover@firewall.loc
		 sysadmin@firewall.loc
	 }
	 notification_email_from Alexandre.Cassen@firewall.loc
	 smtp_server 192.168.17.129
	 smtp_connect_timeout 30
	 router_id LVS_DEVEL
}
vrrp_script chk_http_port {
	script "/usr/local/src/nginx_check.sh"
	interval 2 #(检测脚本执行的间隔)
	weight 2
}
vrrp_instance VI_1 {
	state BACKUP # 备份服务器上将 MASTER 改为 BACKUP
	interface ens33 # 网卡
	virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
	priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 1111
	}
	virtual_ipaddress {
		192.168.17.50 # VRRP H 虚拟地址
	}
}

# 在 /usr/local/src/nginx_check.sh 上添加检测脚本,脚本内容如下

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
	/usr/local/nginx/sbin/nginx
	sleep 2
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	killall keepalived
	fi
fi

4、测试

把两台服务器上nginxkeepalived启动

keepalived启动方式:systemctl start keepalived.service

都启动成功后,浏览器访问虚拟ip。访问成功后,把主机的nginx关闭,再次访问虚拟ip

5、SSL配置

修改Nginx的 配置文件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;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        rewrite ^/(.*)$ https://www.laizhenghua.cn:443/$1 permanent;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        #location / {
            #root   html;
	    #proxy_pass http://www.laizhenghua.cn:8080;
            #index  index.html index.htm;
        #}

        #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;
        #}
   
	#location ~ /edu/ {
            #proxy_pass   http://www.laizhenghua.cn:8080;
	#}
	#location ~ /vod/ {
   	    #proxy_pass   http://www.baidu.com;
	#}


        # 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  laizhenghua.cn;

        ssl_certificate      /etc/ssl/laizhenghua.cn.pem;
        ssl_certificate_key  /etc/ssl/laizhenghua.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            proxy_pass http://www.laizhenghua.cn:8080;
            index  index.html index.htm;
        }
    }

}

6、Nginx的原理

1、master & worker

我们查看进程后可以看到masterworker进程:
在这里插入图片描述
图示:
在这里插入图片描述
2、worker是如何进行工作的
在这里插入图片描述
3、一个 master 和多个 woker 有好处

  1. 可以使用 nginx –s reload 热部署,利用 nginx 进行热部署操作
  2. 每个 woker 是独立的进程,如果有其中的一个 woker 出现问题,其他 woker 独立的,继续进行争抢,实现请求过程,不会造成服务中断

4、设置多少个worker合适

NginxRedis类似都采用了IO多路复用机制,每个worder都是一个独立的进程。通过异步非阻塞的方式来处理请求,即使是上千上万个请求也不在话下,每个worker的线程可以把一个cpu的性能发挥到极致。所以worker数和服务器的cpu数量相等是最为适宜的。

5、连接数worker_connection

思考1:发送一个请求,占用了worker的几个连接数?

答案:2个或者4个

4个情况:
在这里插入图片描述
思考2:Nginx有一个master,有四个worker,每个我客人支持最大的连接数1024,支持的最大并发数是多少?

普通静态访问最大并发数量:(worker_connections * worker_processes) / 2

(4 * 1024) / 2 = 2048

HTTP作为反向代理,最大并发数量:(worker_connections * worker_processes) / 4

END

Thank you for watching

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lambda.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值