Nginx是什么?

  Nginx是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx优点:  

(1)高性能 

在正常情况下,单次请求会得到更快的响应;

在高峰期(如有数以万计的并发请求),Nginx可以比其他Web服务器更快地响应请求。

apache是同步多进程模型,一个连接对应一个进程;

nginx是异步的,多个连接(万级别)可以对应一个进程 。

(2)高扩展性,跨平台  

(3)稳定性高:用于反向代理,宕机的概率微乎其微  

(4)低内存消耗  

一般情况下,10 000个非活跃的HTTP Keep-Alive连接在Nginx中仅消耗2.5MB的内存,这是Nginx支持高并发连接的基础。  

(5)单机支持10万以上的并发连接  

 

Nginx 相对于 Apache 优点如下:

  1. 高并发响应性能非常好,官方 Nginx 处理静态文件并发 5w/s
  2. 负载均衡及反向代理性能非常强;
  3. 系统内存和 CPU 占用率低;
  4. 可对后端服务进行健康检查;
  5. 支持 PHP cgi 方式和 FastCGI 方式;
  6. 可以作为缓存服务器、邮件代理服务器;
  7. 配置代码简洁且容易上手。

apache相对于nginx的优点:

  1. Rewrite比nginx的rewrite强大 (rewrite的主要功能就是实现统一资源定位符URL的跳转)
  2. 模块多,基本想到的都可以找到
  3. 少bug, nginx的bug相对较多
  4. 超稳定

nginx作单纯的WEB服务器,也就是放静态内容,性能上比Apache要好,特别可承受压力、带宽及资源消耗上都要优于Apache。

很多大型网站都喜欢把nginx放在前端,Apache作为后端。

 

二、编译安装Nginx服务的操作步骤

1.环境准备

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

nginx配置里面cpu核心数 nginx 性能指标_Nginx

 

 

2.安装依赖包

yum install -y pcre-devel zlib-devel gcc gcc-c++ make

nginx配置里面cpu核心数 nginx 性能指标_Nginx_02

 

 

3.创建运行用户

useradd -M -s /sbin/nologin nginx

nginx配置里面cpu核心数 nginx 性能指标_html_03

 

 

4.编译安装nginx

cd /opt

nginx配置里面cpu核心数 nginx 性能指标_nginx配置里面cpu核心数_04

 

tar zxvf nginx-1.12.0.tar.gz

nginx配置里面cpu核心数 nginx 性能指标_nginx_05

 

 

cd nginx-1.12.0/

./configure \

--prefix=/usr/local/nginx \ #指定安装路径

--user=nginx \ #指定用户名

--group=nginx \ #指定组名

--with-http_stub_status_module

make && make install -j4

nginx配置里面cpu核心数 nginx 性能指标_nginx配置里面cpu核心数_06

 

 

5.路径优化

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

nginx配置里面cpu核心数 nginx 性能指标_Nginx_07

 

 

6.添加 Nginx 系统服务

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

nginx配置里面cpu核心数 nginx 性能指标_Nginx_08

 

 

chmod 754 /lib/systemd/ system/nginx.service 
systemctl start nginx.service 
systemctl enable nginx.service

 


三、配置文件了解

1.主配置文件

vim /usr/local/nginx/conf/nginx.conf

全局配置:

user nginx nginx; #Nginx用户及组:用户 组。window下不指定
worker_processes 1; #工作进程:数目
error_log logs/error.log; 
error_log logs/error.log notice; 
error_log logs/error.log info; #错误日志:存放路径。
pid logs/nginx.pid; #pid(进程标识符):存放路径。
worker_rlimit_nofile 204800; #指定进程可以打开的最大描述符:数目。

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

I/O事件配置:

events { 
use epoll; #使用epoll模型 
worker_connections 4096; #每个进程处理 4096个连接 
}

 

      

HTTP模块配置:    

http {#文件扩展名与文件类型映射表#默认文件类型
#日志格式设定
#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 {
#监听地址及端口
            server_name localhost;
#网页的默认字符集
 #access_log logs/host.access.log main;
          location / {
#根目录配置
#默认首页文件名
          }
 #error_page 404 /404.html;
redirect server error pages to the static page /50x.html
 error_page 500 502 503 504 /50x.html; #内部错误的反馈页面
#错误页面配置
                               root html;
           }
}

  #用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;

是nginx中最频繁配置的部分,而且这里也包含两个小块,分别为全局块和server块。主要影响nginx的性能。  

 

状态统计配置

location /status {                                               #访问位置为/status

                         stub_status on;                        #打开状态统计功能

                         access_log off;                        #关闭此位置的日志记录

}

授权的访问控制

#添加认证配置

auth basic "secret";              #设置密码提示框文字信息

auth_basic_user_file /usr/local/nginx/passwd.db;

客户端访问控制

location / {

root html;

index index.html index.htm;

auth_basic "secret";

auth_basic_user_file /usr/local/nginx/passwd.db;

#添加控制规则

deny 192.168.2.66;                     #拒绝访问的客户端IP

allow all;                                      #允许其他所有客户端访问

}