Pivotal Cloud Technology | PivotalCloud 云技术

Nginx服务器 | Nginx基础服务实战

6Xj5gx

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.

不要别人告诉你该做什么,有梦想,就得保护。他人做不成什么事情,就跟你说你也做不成。如果你想要什么,就要去争取。——《当幸福来敲门》

1|0基本概述

6XjZnO.jpg

Nginx 是互联网主流的高性能 http 和 反响代理 Web 服务器,Nginx 不但可以作为 Web 服务器,它还提供了静态资源服务、缓存、负载均衡 等功能。

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

2|0实战搭建Nginx

2|1基于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

[注意事项]:

[Unit]: 服务的说明

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
2|2基于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

3|0实战配置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)

worker_processes 4;

worker_cpu_affinity 0001 0010 0100 1000;

[2].8 CPU (8 Core) + 8 worker_processes (每个worker_processes 使用1个CPU)

worker_processes 8;

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

[3].16 CPU (16 Core) + 16 worker_processes (每个worker_processes 使用1个CPU)

worker_processes 16;

worker_cpu_affinity
0000000000000001 0000000000000010 0000000000000100 0000000000001000 0000000000010000 0000000000100000 0000000001000000 0000000010000000 0000000100000000 0000001000000000 0000010000000000 0000100000000000 0001000000000000 0010000000000000 0100000000000000 1000000000000000;

  • 配置事件处理模型[events]:
#配置Nginx worker进程最大打开文件数 worker_rlimit_nofile 65535; events { #使用高性能的 epoll 事件驱动,处理效率高 use epoll; accept_mutex on; #打开同时接受多个新网络连接请求的功能 multi_accept on; #单个进程允许的客户端最大连接数 worker_connections 65535; }
  • 开启高效传输模式
#开启高效文件传输模式 sendfile on; #需要在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量。将响应头和正文的开始部分一起发送,而不一个接一个的发送。 tcp_nopush on; tcp_nodelay on;
  • 开启传输压缩
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;

__EOF__

本文作者PivotalCloud
本文链接https://www.cnblogs.com/mazhilin/p/14582856.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:Copyright © 2018-2021 PivotalCloud Technology Systems Incorporated. All rights reserved.
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   PivotalCloud  阅读(145)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示