【Nginx】基础知识


title: 【Nginx】基础知识
date: 2020-07-18 13:27:10
tags:
- Nginx
- 运维

安装

安装 pcre

下载
解压
./configure
make && make install

更新依赖

sudo yum install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

安装 nginx

下载
解压
./configure
make && make install

CentOS7 的默认防火墙改成了 firewall,不再使用 iptables 为默认防火墙了

安装好 nginx 后需要打开 80 端口

firewall-cmd --list-all #查看开发的端口号
firewall-cmd --zone=public --add-port=80/tcp --permanent # 开发 80 端口 tcp 协议
# --zone #作用域
# --add-port=80/tcp #添加端口,格式:端口/通讯协议
# --permanent #永久生效,没有此参数重启后失效

firewall 相关命令

systemctl stop firewalld.service # 停止
systemctl start firewalld.service # 启动
systemctl status Firewalld.service # 状态
systemctl restart firewalld.service # 重启

相关命令

查看版本号:

[root@centos-7 sbin]# ./nginx -v
nginx version: nginx/1.19.1

停止:

[root@centos-7 sbin]# ./nginx -s stop
[root@centos-7 sbin]# ps aux | grep nginx
root     16465  0.0  0.0 112824   980 pts/0    S+   21:31   0:00 grep --color=auto nginx

启动:

[root@centos-7 sbin]# ./nginx
[root@centos-7 sbin]# ps aux | grep nginx
root     16475  0.0  0.0  20568   620 ?        Ss   21:32   0:00 nginx: master process ./nginx
nobody   16476  0.0  0.0  23092  1380 ?        S    21:32   0:00 nginx: worker process
root     16478  0.0  0.0 112824   976 pts/0    S+   21:32   0:00 grep --color=auto nginx

重加载:

[root@centos-7 sbin]# ./nginx -s reload
[root@centos-7 sbin]# ps aux | grep nginx
root     16475  0.0  0.0  20708  1344 ?        Ss   21:32   0:00 nginx: master process ./nginx
nobody   16480  0.0  0.0  23208  1492 ?        S    21:33   0:00 nginx: worker process

配置文件

一般在/usr/local/nginx/conf下。

注意:如果在 nginx 安装过程中指定了--prefix=/path/的情况下,这个目录就在指定的 path 下。

注意:yum 安装的可能不在这个目录下。

配置文件分为 3 个部分:

  • 全局块(对应下文中配置文件的 1-10 行)

    从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令。

    主要包括配置运行 nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型、配置文件的引入等。

    比如:worker_processes 1;是 nginx 服务器并发处理服务的关键配置,值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。(和 CPU 核心数量一致比较合理?)

  • events 块

    主要影响 nginx 服务器与用户的网络连接。

    比如:worker_connections 1024; 支持的最大连接数。

  • http 块

    配置中最频繁的步伐,代理、缓存、日志定义绝大多数功能和第三方模块的配置都在这里

    注意:http 块也可以包括 http 全局块、server 块。

    • http 全局块(下文中 17-33 行)
    • 包括文件引入、mime-type 定义、日志自定义、连接超时时间、单链接请求数上限等。
    • server 块

    • 和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

    • 每个 http 块可以包括多个 server 块,每个 server 块就相当于一个虚拟主机。

    • 每个 server 块也分为全局 server 块以及可以同时包含多个 location 块。

    • 全局 server 块(下文 35-41 行)最常见的就是配置本虚拟主机的监听配置和本虚拟主机的名称或 IP 配置。

    • location 块,一个 server 块可以包含多个 location 块,主要是基于 nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如前面的/uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。

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

}

NGINX 原理

master & worker

image.png

[root@centos-7 keepalived]# ps aux | grep nginx
root     11262  0.0  0.0  20612  1424 ?        Ss   14:14   0:00 nginx: master process ./nginx
nobody   18694  0.0  0.0  23124  1664 ?        S    16:27   0:00 nginx: worker process

worker 是如何工作的?

争抢。

image.png

一个 master 和多个 worker 的好处?

1、可以使用 nginx -s reload 热部署

当使用了热部署后,之前有 client 请求的 worker 还是按照之前的规则进行处理请求,空闲的 worker 就可以根据新的配置来进行处理 client 的请求。

2、对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,同时在编程以及问题查找时,也会方便很多。

3、采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快启动新的worker进程。

当然,worker进程的异常退出,肯定是程序有bug了,异常退出,会导致当前worker上的所有请求失败,不过不会影响到所有请求,所以降低了风险。

设置多少个 worker 合适?

Nginx同redis类似都采用了io多路复用机制,每个worker都是一个独立的进程,但每个进程里只有一个主线程,通过异步非阻塞的方式来处理请求,即使是 千上万个请求也不在话下。

每个worker的线程可以把一个cpu的性能发挥到极致。所以worker数和服务器的cpu数相等是最为适宜的。设少了会浪费cpu,设多了会造成cpu频繁切换上下文带来的损耗。

# 设置worker数量
worker_processes 4

# work绑定cpu(4work绑定4cpu)
worker_cpu_affinity 0001 0010 0100 1000

# work绑定cpu (4work绑定8cpu中的4个)
worker_cpu_affinity 0000001 00000010 00000100 00001000

连接数 worker_connection

这个值是表示每个worker进程所能建立连接的最大值。

所以,一个nginx 能建立的最大连接数,应该是worker.connections * worker processes

当然,这里说的是最大连接数,对于HTTP 请求本地资源来说,能够支持的最大并发数量是worker.connections * worker processes

如果是支持http1.1的浏览器每次访问要占两个连接,所以普通的静态访问最大并发数是worker.connections * worker.processes / 2

而如果是HTTP作为反向代理来说,最大并发数量应该是worker.connections * worker_proceses/4

因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。

发送请求,占用了woker的几个连接数?

2 个或者 4 个。

比如请求的是静态资源,一来一回是两个连接数。

如果访问别的资源比如说是 tomcat,则 worker 和 tomcat 也有一来一回两个连接,所以是 4 个连接数。

nginx有一个master,有四个woker,每个woker支持最大的连接数1024,支持的最大并发数是多少?

普通的静态访问最大并发数是: worker connections * worker processes/2

而如果是HTTP作为反向代理来说,最大并发数量应该是worker connections * worker processes/4

EOF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值