«
Nginx介绍及安装

时间:2022-8-17     作者:李泽信     分类: nginx


一、Nginx简介

Nginx是一款由俄罗斯开发的开源高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3/SMTP代理服务,其性能优势显著,官网上称:单台Nginx服务器可以处理50000并发;
特点:高性能,稳定,消耗硬件资源小、能够处理大并发,主要用于静态的解析,动静液面的分离;
优势


二、Nginx实现原理

三、Nginx支持高并发的原因

四、Nginx安装

[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar xf nginx-1.22.0.tar.gz 
[root@localhost nginx-1.22.0]# ./configure --prefix=/data/nginx --user=nginx --group=nginx --with-http_stub_status_module

注解:
--prefix=/usr/local/nginx               ##指定安装位置
--user=nginx --group=nginx              ##指定运行服务的用户和组
--with-http_stub_status_module          ##开启状态监听模块 
--conf-path=                            ##指向配置文件存放位置
--error-log-path=                       ##指向错误日志存放位置
--pid-path=                             ##指向pid文件存放位置 
--with-rtsig_module                     ##启用rtsig模块支持(实时信号)
--with-select_module                    ##启用select模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:--without-select_module
--with-http_ssl_module              ##启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)
--with-http_xslt_module                 ##启用ngx_http_xslt_module支持(过滤转换XML请求)
--with-http_image_filter_module             ##启用ngx_http_image_filter_module支持(传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用,要用到gd库) 
--with-http_gzip_static_module          ##启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
--with-http_degradation_module          ##启用ngx_http_degradation_module支持(允许在内存不足的情况下返回204或444码)
--without-http_access_module            ##禁用ngx_http_access_module支持(该模块提供了一个简单的基于主机的访问控制,允许或拒绝基于ip地址)
--without-http_auth_basic_module        ##禁用ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于http基本认证方法,来保护你的站点或其部分内容)
---without-http_rewrite_module          ##禁用ngx_http_rewrite_module支持(该模块允许使用正则表达式改变URL)
--without-http_fastcgi_module           ##禁用ngx_http_fastcgi_module支持(该模块允许Nginx 与FastCGI 进程交互,并通过传递参数来控制FastCGI 进程工作。)

[root@localhost nginx-1.22.0]# make && make install
[root@localhost nginx-1.22.0]# ls /data/nginx
conf  html  logs  sbin
[root@localhost data]# /data/nginx/sbin/nginx

[root@localhost data]# ln -s /data/nginx/sbin/nginx /usr/local/sbin/
[root@localhost data]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/data/nginx/sbin/nginx"
NPF="/data/nginx/logs/nginx.pid"
case "$1" in
  start)
    $NP;
    if [ $? -eq 0 ]
    then
      echo "nginx is starting!! "
    fi
  ;;
  stop)
    kill -s QUIT $(cat $NPF)
    if [ $? -eq 0 ]
    then
    echo "nginx is stopping!! "
    fi
  ;;
  restart)
    $0 stop
    $0 start
  ;;
  reload)
    kill -s HUP $(cat $NPF)
    if [ $? -eq 0 ]
    then
      echo "nginx config file is reload! "
    fi
  ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1  
esac
exit 0

[root@localhost data]# chmod +x /etc/init.d/nginx 
[root@localhost data]# chkconfig --add nginx
[root@localhost data]# chkconfig nginx on
[root@localhost data]#  /etc/init.d/nginx start #start/stop/restart/reload
[root@localhost ~]# vi /data/nginx/conf/nginx.conf      ##编辑配置文件在server中添加如下行:
     47         location /status {
     48                 stub_status on;
     49                 access_log off;
     50         }
[root@localhost ~]# /data/nginx/sbin/nginx -t   #检查配置是否正确
[root@localhost ~]# /data/nginx/sbin/nginx -s reload    #重新加载配置文件
http            {                   ##http服务配置区域
include mime.types;                 ##指定文件扩展名和文件类型映射表
default_type  application/octet-stream;     ##指定文件类型
charset  utf-8;                     ##指定字符集
server_names_hash_bucket_size  128; ##服务器名字的hash表大小
client_header_buffer_size 2k;           ##客户端请求头部buffer大小
large_client_header_buffers 4 4k;           ##指定客户端请求中较大的消息头的缓存数量和大小
client_max_body_size 8m;                ##指定客户端请求的单个文件的最大字节数

sendfile  on;                           ##开启高效传输模式
tcp_nopush  on;                     ##防止网络阻塞

keepalive_timeout 60;                   ##客户端连接超时时间

#FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2        ##配置fastcgi缓存路径和目录结构等级
        keys_zone=TEST:10m  inactive=5m;            ##关键字区域存储时间和非活动删除时间
fastcgi_connect_timeout 300;            ##连接到后端FastCGI的超时时间
fastcgi_send_timeout 300;               ##向FastCGI传送请求的超时时间
fastcgi_read_timeout 300;               ##接收FastCGI应答的超时时间
fastcgi_buffer_size 4k;                 ##指定读取FastCGI应答第一部分需要多大的缓冲区
fastcgi_buffers 8 4k;                   ##指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求
fastcgi_busy_buffers_size 8k;               ##通常为fastcgi_buffer_size大小的两倍
fastcgi_temp_file_write_size 8k;            ##写入缓存文件时使用多大的数据块,大小同上

fastcgi_cache TEST;                     ##开启Fastcgi的缓存并且为其指定一个名称
fastcgi_cache_valid 200 302 1h;         ##指定不同的状态码,其缓存的时间
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;               ##URL经过被访问多少次将被缓存
fastcgi_cache_use_stale error timeout invalid_header http_500;      ##指定什么情况下不进行缓存

open_file_cache max=204800  inactive=20s;       ##指定缓存文件最大数量,经过多长时间文件没有被请求后则删除缓存,
open_file_cache_min_uses 1;         ##指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件更改信息一直是在缓存中打开的;
open_file_cache_valid 30s;              ##指定多长时间检查一次缓存的有效信息,检查该缓存的源文件是否发生变化修改等;

tcp_nodelay on;                     ## nagle算法,有需要发送的就立即发送,连接转换为长连接时使用;
server_tokens off;              ##禁用版本号
gzip on;                            ##开启gzip压缩
gzip_min_length  1k;                ##指定最小压缩文件的大小
gzip_buffers    4  16k;         ##指定压缩缓冲区的个数和大小
gzip_http_version 1.0;              ##指定压缩版本
gzip_comp_level 2;                  ##指定压缩等级1-9,9等级最高
gzip_types  text/plain application/x-javascript text/css application/xml;       ##指定压缩文件类型
gzip_vary on;               ##前端缓存服务器缓存经过压缩的页面

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
                                  '$status $body_bytes_sent "$http_referer" '
                                  '"$http_user_agent" $http_x_forwarded_for';
                        ##配置日志格式,具体变量表示请结合百度,日志格式为access
语法规则: location  [=|~|~*|^~]  /uri/  { … }
=:表示精确匹配,这个优先级也是最高的
^~:表示url以某个常规字符串开头,理解为匹配 url路径即可。
~ :表示区分大小写的正则匹配
~*:表示不区分大小写的正则匹配(和上面的唯一区别就是大小写)
!~和!~*:分别为区分大小写不匹配及不区分大小写不匹配的正则
/ :通用匹配,任何请求都会匹配到,默认匹配.
优先级排序:  =   >  ^~  >
常用的正则表达式:
*               ##重复0次或者多次
?               ##重复0次或者1次
+               ##重复1次或多次
.               ##匹配除换行符号之外的所有字符
(a|b)           ##匹配a或b
^               ##以...开头
$               ##以...结尾
{n}             ##重复n次
{n,}                ##重复最少n次
{n,m}           ##重复n到m次
\W              ##匹配任意不是字母,数字,下划线,汉字的字符
\S              ##匹配任意不是空白的字符
\D              ##匹配任意非数字的字 符
\B              ##匹配不是单词开头或结束的位置
[^x]                ##匹配除了x以外的所有字符
[^abcd]         ##匹配除了a或b或c或d以外的所有字符
^[abcd]         ##匹配除了字符串abcd以外的所有字符
[root@localhost ~]# yum -y install gcc ctags
[root@localhost ~]# wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz
[root@localhost ~]# tar xf webbench-1.5.tar.gz -C /data/
[root@localhost ~]# cd /data/webbench-1.5/
[root@localhost webbench-1.5]# mkdir /usr/local/man
[root@localhost webbench-1.5]# make && make install
[root@localhost ~]# webbench -c 10000 -t 5 http://192.168.100.102/index.html    ##并发数为10000,时间为5秒
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.100.102/index.html
10000 clients, running 5 sec.

Speed=147744 pages/min, 1397500 bytes/sec.
Requests: 12312 susceed, 0 failed.