关于linux:Nginx服务器-Nginx基础服务实战

5次阅读

共计 4501 个字符,预计需要花费 12 分钟才能阅读完成。

You got a dream,you gotta protect it.People can’t do something themselves,they wanna tell you you can’t do it.If you want something,go get it.
不要他人通知你该做什么,有幻想,就得爱护。别人做不成什么事件,就跟你说你也做不成。如果你想要什么,就要去争取。——《当幸福来敲门》

根本概述

Nginx 是互联网支流的高性能 http 和 反应代理 Web 服务器,Nginx 岂但能够作为 Web 服务器,它还提供了动态资源服务、缓存、负载平衡 等性能。

不管你是前端程序员还是后端程序员,对于 Nginx 的接触,应该不会生疏。对于前端和后端来说, 它就像是沟通桥梁, 相似粘合剂的作用。尤其是在前后端拆散的时代,Nginx 出场的频率和一线偶像明星出镜的次数差不多。
在云原生 [Cloud Native] 时代,Envoy 的呈现,仿佛减少了咱们技术选型的可能性。当然 Envoy 和 Nginx 都采纳了 多线程 + 非阻塞 + 异步 IO(Libevent)的架构模式。

实战搭建 Nginx

基于 Centos 7 部署 Nginx
  • 装置 Nginx 相干依赖
# 装置使 nginx 反对 rewrite
yum -y install pcre* 
#装置使 nginx 反对 gcc-c++
yum -y install gcc-c++
#装置使 nginx 反对 zlib      
yum -y install zlib*
#装置使 nginx 反对 openssl
yum -y install openssl
  • 下载安装 nginx-1.19.8.tar.gz 安装包,上传到服务器目录: /usr/local
[root@centos-pivtoal ~]# cd /usr/local
[root@centos-pivtoal local]# ls
aegis  bin  cloudmonitor  etc  games  include  lib  lib64  libexec  nginx-1.19.8.tar.gz  sbin  share  src
[root@centos-pivtoal local]# tar -xvf nginx-1.19.8.tar.gz 
[root@centos-pivtoal local]# cd nginx-1.19.8
[root@centos-pivtoal nginx-1.19.8]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@centos-pivtoal nginx-1.19.8]# 
  • 配置装置查看: ./configure
[root@centos-pivtoal nginx-1.19.8]# ./configure
[root@centos-pivtoal nginx-1.19.8]# 
  • 编译并装置 Nginx: make && make install
[root@centos-pivtoal nginx-1.19.8]#  make && make install
[root@centos-pivtoal nginx-1.19.8]# 
  • 配置开机自启动 Nginx

[1]. 在 /lib/systemd/system/ 在目录创立 nginx.service:

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[注意事项]:

Description: 形容服务
After: 形容服务类别
[Service] 服务运行参数的设置
Type:forking 是后盾运行的模式
ExecStart 为服务的具体运行命令
ExecReload 为重启命令
ExecStop 为进行命令
PrivateTmp:True 示意给服务调配独立的长期空间
[Service] 的启动、重启、进行命令全副要求应用绝对路径
[Install] 运行级别下服务装置的相干设置,可设置为多用户,即零碎运行级别为 3

[2]. 增加零碎自启动

# 设置开机启动
systemctl enable nginx.service
#启动 nginx 服务
systemctl start nginx.service
#查看 nginx 服务状态
systemctl status nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service
基于 Dokcer 部署 Nginx
  • 拉取 Nginx 镜像
[root@centos-pivtoal ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete 
0732ab25fa22: Pull complete 
d7f36f6fe38f: Pull complete 
f72584a26f32: Pull complete 
7125e4df9063: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@centos-pivtoal ~]# docker tag docker.io/library/nginx:latest nginx:latest 
[root@centos-pivtoal ~]# 
  • 编写 Docker 部署脚本
docker run -itd -p 80:80 -p 443:443  --name nginx-server --network-alias nginx-server --hostname nginx-server --restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  -v /home/application/html:/usr/share/nginx/html nginx:latest
  • 执行脚本命令

docker run -itd -p 80:80 -p 443:443 –name nginx-server –network-alias nginx-server –hostname nginx-server –restart always -v /docker/nginx/conf/conf.d/:/etc/nginx/conf.d/ -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/application/html:/usr/share/nginx/html nginx:latest


#### 实战配置 Nginx
* 设置 Nginx 过程数目[worker_processes]:默认能够设置为 CPU 的核数相等,并发比拟大的时候,能够设置为 cpu 核数 *2

查看服务器 cpu 信息:cat /proc/cpuinfo | grep processor

[root@iZm5efc4cs8k6t2agr7tceZ manager]# cat /proc/cpuinfo | grep processor
processor : 0
processor : 1
processor : 2
processor : 3
[root@iZm5efc4cs8k6t2agr7tceZ manager]#

> 增加配置举例:> [1] .4 CPU (4 Core) + 4 worker_processes(每个 worker_processes 应用 1 个 CPU) </br>
> worker_processes 4;</br>
> worker_cpu_affinity 0001 0010 0100 1000;</br>
> [2].8 CPU (8 Core) + 8 worker_processes (每个 worker_processes 应用 1 个 CPU)</br>
> worker_processes 8;</br>
> worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;</br>
> [3].16 CPU (16 Core) + 16 worker_processes (每个 worker_processes 应用 1 个 CPU)</br>
> worker_processes 16;</br>
> worker_cpu_affinity 
> 0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;

* 配置事件处理模型[events]:</br>

配置 Nginx worker 过程最大关上文件数

worker_rlimit_nofile 65535;
events {

应用高性能的 epoll 事件驱动,解决效率高

use epoll;
accept_mutex on;

关上同时承受多个新网络连接申请的性能

multi_accept on;

单个过程容许的客户端最大连接数

worker_connections 65535;
}


* 开启高效传输模式 </br>

开启高效文件传输模式

sendfile on;

须要在 sendfile 开启模式才无效,避免网路阻塞,踊跃的缩小网络报文段的数量。将响应头和注释的开始局部一起发送,而不一个接一个的发送。

tcp_nopush on;
tcp_nodelay on;

* 开启传输压缩 </br>

gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

正文完
 0