简介

  Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,特点是占有内存少,并发能 力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,在高连接并发的情况下,Nginx是Apache服务器不错的替代品。中国大陆使用nginx 网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

nginx 原理与参数

master-workers 的机制的好处

  首先,对于每个 worker 进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销, 同时在编程以及问题查找时,也会方便很多。其次,采用独立的进程,可以让互相之间不会 影响,一个进程退出后,其它进程还在工作,服务不会中断,master 进程则很快启动新的 worker 进程。当然,worker 进程的异常退出,肯定是程序有 bug 了,异常退出,会导致当 前 worker 上的所有请求失败,不过不会影响到所有请求,所以降低了风险。

设置 worker

  Nginx 同 redis 类似都采用了 io 多路复用机制,每个 worker 都是一个独立的进程,但每个进 程里只有一个主线程,通过异步非阻塞的方式来处理请求, 即使是千上万个请求也不在话 下。每个 worker 的线程可以把一个 cpu 的性能发挥到极致。所以 worker 数和服务器的 cpu 数相等是最为适宜的。设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗。

连接数 worker_connection

  这个值是表示每个 worker 进程所能建立连接的最大值,所以,一个 nginx 能建立的最大连接数,应该是 worker_connections * worker_processes。当然,这里说的是最大连接数,对于 HTTP 请 求 本 地 资 源 来 说 , 能 够 支 持 的 最 大 并 发 数 量 是 worker_connections * worker_processes,如果是支持 http1.1 的浏览器每次访问要占两个连接,所以普通的静态访 问最大并发数是: worker_connections * worker_processes /2,而如果是 HTTP 作 为反向代理来说,最大并发数量应该是 worker_connections * worker_processes/4。因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。

安装

初次安装

[root@localhost ~]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
No package nginx available.
Error: Nothing to do

原因是nginx位于第三方的yum源里面,而不在centos官方yum源里面

解决方案

#使用 yum-config-manager 软件包命令
[root@localhost ~]# yum install -y yum-utils
#安装wget
[root@localhost ~]#yum install -y wget
#切换到repo配置文件目录
[root@localhost ~]# cd /etc/yum.repos.d/
#备份CentOs-Base.repo源文件
[root@localhost yum.repos.d]# mv CentOs-Base.repo CentOs-Base.repo.bak
#网络下载centos官方和扩展.repo文件
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
#安装epel-release
[root@localhost yum.repos.d]# yum install -y epel-release
[root@localhost yum.repos.d]#mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-7.repo
#清理软件源缓存
[root@localhost yum.repos.d]# yum clean all
#重建缓存
[root@localhost yum.repos.d]# yum makecache
#可选选项,更新系统 可选操作
[root@localhost yum.repos.d]# yum update
# 也可以使用yum-config-manager --add-repo 命令。进行以下操作推荐备份原.repo文件。yum-config-manager命令必要条件: yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/repo/epel-7.repo

二次安装

[root@localhost ~]# yum install -y nginx
[root@localhost ~]# find / -name 'nginx'
/etc/logrotate.d/nginx
/etc/nginx
/var/lib/nginx
/var/log/nginx
/usr/sbin/nginx
/usr/lib64/perl5/vendor_perl/auto/nginx
/usr/lib64/nginx
/usr/share/nginx

校验

# 启动
[root@localhost ~]# cd /usr/sbin/
[root@localhost sbin]# ./nginx
# 查看状态
[root@localhost sbin]# ps -ef|grep nginx
root 40783 1 0 21:25 ? 00:00:00 nginx: master process ./nginx
nginx 40784 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40785 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40786 40783 0 21:25 ? 00:00:00 nginx: worker process
nginx 40787 40783 0 21:25 ? 00:00:00 nginx: worker process

停止

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

重载

[root@localhost sbin]# ./nginx -s reload
# 重载指定配置文件
[root@localhost sbin]# ./nginx -s reload -c /etc/nginx/nginx.conf

第二种启动方式

# 启动
[root@localhost ~]# systemctl start nginx
# 查看状态
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2020-04-04 21:52:43 CST; 50s ago
Process: 42188 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 42185 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 42183 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 42191 (nginx)
CGroup: /system.slice/nginx.service
├─42191 nginx: master process /usr/sbin/nginx
├─42192 nginx: worker process
├─42193 nginx: worker process
├─42194 nginx: worker process
└─42195 nginx: worker process Apr 04 21:52:43 localhost systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 04 21:52:43 localhost nginx[42185]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 04 21:52:43 localhost nginx[42185]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:52:43 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
# 停止
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead) Apr 04 21:48:37 localhost nginx[41953]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:48:37 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
Apr 04 21:49:40 localhost systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Apr 04 21:49:40 localhost systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Apr 04 21:52:43 localhost systemd[1]: Starting The nginx HTTP and reverse proxy server...
Apr 04 21:52:43 localhost nginx[42185]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 04 21:52:43 localhost nginx[42185]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 04 21:52:43 localhost systemd[1]: Started The nginx HTTP and reverse proxy server.
Apr 04 21:54:12 localhost systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Apr 04 21:54:12 localhost systemd[1]: Stopped The nginx HTTP and reverse proxy server.

Nginx(一):安装与常用命令的更多相关文章

  1. Nginx编译安装及常用命令

    一个执着于技术的公众号 前言 前面我们已经了解Nginx基础入门知识,今天就带大家一起学习下Nginx编译安装部署 准备工作 一台linux机器(本次实验以CentOS 7.5为例) 到Nginx官方 ...

  2. ios开发环境配置及cordova安装与常用命令

    一.ios开发环境配置 1.首先要有台Mac Book,如果有Mac Book,跳过步骤2.3.4,如果没有,执行步骤2.3.4: 2.下载并安装VMware Workstation,最好是下最新版本 ...

  3. Git安装以及常用命令(图文详解)

    **Git安装以及常用命令** 1.下载安装Git,傻瓜式安装相信大家都会. 官网下载地址:[https://git-scm.com/downloads] 2.Git基本操作 (1)git --ver ...

  4. linux基础学习之软件安装以及常用命令

    linux基础学习之软件安装以及常用命令 调用中央仓库: yum install wget 然后下载nodejs: wget https://nodejs.org/dist/v10.14.2/node ...

  5. Git安装和常用命令

    Git是目前世界上最先进的分布式版本控制系统!!! Git能自动帮我们记录每次文件的改动,还可以让同事协作编辑. 接下来,简单的介绍下Git的安装和常用命令: Git安装: 1.Windows系统,进 ...

  6. RabbitMQ入门教程(一):安装和常用命令

    原文:RabbitMQ入门教程(一):安装和常用命令 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

  7. 记录redis安装及常用命令

    Redis安装及常用命令 一.安装 1.下载,解压,进入redis解压目录,make. make PREFIX=目录/redis install :安装到指定目录文件名为redis. 2.将解压目录里 ...

  8. 实验 1 Linux 系统的安装和常用命令

    实验 1 Linux 系统的安装和常用命令 (题目) 一.实验目的 (1)掌握 Linux 虚拟机的安装方法.Spark 和 Hadoop 等大数据软件在 Linux 操作系统 上运行可以发挥最佳性能 ...

  9. Nginx安装及常用命令

    一.选定源码目录 cd /usr/local/src 可以是任何目录,本文选定的是/usr/local/src 二.安装依赖库 yum install gcc yum install pcre-dev ...

  10. Docker安装和常用命令

    Docker安装 Docker的安装可以参考 https://docs.docker.com/ 下面的 Get Docker / Docker CE / Linux, 需要关注的主要是CentOS和U ...

随机推荐

  1. Java 添加、读取、删除Excel中的图表趋势线

    本文以Java示例介绍如何在Excel中添加趋势线,以及读取趋势线公式.通过文中的方法可支持添加6种不同类型的趋势线,包括Linear.Exponential.Logarithmic.Moving A ...

  2. 如何使用ABBYY FineReader处理文档图像的缺陷?

    通过扫描仪或者数码相机获取的图像文件,容易出现文本扭曲.页面歪斜等缺陷,会影响到OCR的识别质量.此时,用户可使用ABBYY FineReader 15(Windows系统)OCR文字识别软件的自动和 ...

  3. 【移动自动化】【二】Appium

    实施自动化需要的工具 adb Android控制工具,获取获取Android各种数据和控制,Appium会调起adb命令去执行Android设备 adb命令参考 https://www.cnblogs ...

  4. mongodb查询前10条

    mongo可以通过时间或者通过id来判断上一条记录或者下一条记录,我是通过id 前10条 db.数据库名称.find({ '_id': { '$lt': ids } }).sort({_id: -1} ...

  5. yii2.0验证码的两种实现方式

    第一种方式:采用model 1. 模型:Code.php <?phpnamespace app\models; use yii\base\Model;class Code extends Mod ...

  6. ERP制造模块操作与设计--开源软件诞生30

    赤龙ERP制造模块讲解--第30篇 用日志记录"开源软件"的诞生 [进入地址 点亮星星]----祈盼着一个鼓励 博主开源地址: 码云:https://gitee.com/redra ...

  7. idea2020安装破解教程

    申明:本教程 IntelliJ IDEA 破解补丁.激活码均收集于网络,请勿商用,仅供个人学习使用 不花钱 的方式 IDEA 2020.2 激活到 2089 年 idea官网下载安装包:https:/ ...

  8. centOs7.5.64之前的操作系统搭建GitLab记录

    GitLab搭建步骤: 1. Install and configure the necessary dependencies (1)yum install curl openssh-server o ...

  9. rest-framework 解析器

    一 解析器的作用: 根据请求头 content-type 选择对应的解析器对请求体内容进行处理. 有application/json,x-www-form-urlencoded,form-data等格 ...

  10. 安装spyder记录

    sudo apt-get install spyder 报错:ERROR: Could not find a version that satisfies the requirement pyqt5& ...