准备
安装基本库
1
| yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
|
安装基本工具
1
| yum -y install wget httpd-tools vim
|
初始化
1
| cd /data/nginx/;mkdir app download logs work backup
|
- /data/nginx
- app:代码目录
- download:源码包
- logs:自定义日志
- work:shell脚本
- backup:默认配置文件
查看yum源是否可用
查看是否有iptables规则

关闭iptables规则
1 2
| iptables -F iptables -t nat -F
|
查看命令
1
| [root@sparsematrix nginx]
|

Nginx简述
1
| Nginx是一个开源且高性能、可靠的HTTP中间件、代理服务
|
常见的HTTP服务
- HTTPD - Apache基金会
- IIS - 微软
- GWS - Google
为什么选择Nginx
IO多路复用epoll/并发问题
多个描述符I/O操作都能在一个线程内并发交替地顺序完成,这就叫I/O多路复用,这里的”复用”指的是复用同一个线程

什么是epoll
1
| IO多路复用的实现方式select、poll、epoll
|
- select:结账的时候,服务员不告诉老板哪桌要结账,老板一个个去问才知道哪桌要结账
- epoll:结账的时候,服务员告诉老板哪桌要结账
轻量级
CPU亲和(affinity)
CPU亲和
是一种把CPU核心和Nginx工作进程绑定的方式,把每个worker进程固定在一个CPU上执行,减少切换CPU的cache miss,获得更好的性能

0拷贝
Nginx安装
- Mainline version:开发版
- Stable version:稳定版
1 2 3 4 5
| [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
|

查看yum源中nginx相关版本
使用yum源安装Nginx
查看当前安装nginx版本

查看Nginx编译参数

Nginx安装编译参数详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| --prefix=/etc/nginx 【安装目的目录或路径】 --sbin-path=/usr/sbin/nginx 【安装目的目录或路径】 --modules-path=/usr/lib64/nginx/modules 【安装目的目录或路径】 --conf-path=/etc/nginx/nginx.conf 【安装目的目录或路径】 --error-log-path=/var/log/nginx/error.log 【安装目的目录或路径】 --http-log-path=/var/log/nginx/access.log 【安装目的目录或路径】 --pid-path=/var/run/nginx.pid 【安装目的目录或路径】 --lock-path=/var/run/nginx.lock 【安装目的目录或路径】 --http-client-body-temp-path=/var/cache/nginx/client_temp【执行对应模块时,Nginx所保留的临时性文件】 --http-proxy-temp-path=/var/cache/nginx/proxy_temp 【执行对应模块时,Nginx所保留的临时性文件】 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 【执行对应模块时,Nginx所保留的临时性文件】 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 【执行对应模块时,Nginx所保留的临时性文件】 --http-scgi-temp-path=/var/cache/nginx/scgi_temp【执行对应模块时,Nginx所保留的临时性文件】 --user=nginx --group=nginx【设定Nginx进程启动的用户和组用户】 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module【目录中选择一个随机主页】 Syntax:random_index on | off Default:random_index off Context:location --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module【Nginx的客户端状态】 Syntax:stub_status Default: - Context: server,location --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'【设置额外的参数将被添加到CFLAGS变量】 --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'【设置附加的参数,链接系统库】
|
Nginx安装目录解析
使用rpm -ql查看nginx的安装目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| /etc/logrotate.d/nginx【配置文件】Nginx日志轮转,用于logrotate服务的日志切割 /etc/nginx【目录、配置文件】Nginx主配置文件 /etc/nginx/conf.d【目录、配置文件】Nginx主配置文件 /etc/nginx/conf.d/default.conf【目录、配置文件】Nginx主配置文件 /etc/nginx/fastcgi_params【配置文件】cgi配置相关,fastcgi配置 /etc/nginx/koi-utf【配置文件】编码转换映射转化文件 /etc/nginx/koi-win【配置文件】编码转换映射转化文件 /etc/nginx/mime.types【配置文件】设置http协议的Content-Type与拓展名对应关系 /etc/nginx/modules【目录】Nginx模块目录 /etc/nginx/nginx.conf【目录、配置文件】Nginx主配置文件 /etc/nginx/scgi_params【配置文件】cgi配置相关,fastcgi配置 /etc/nginx/uwsgi_params【配置文件】cgi配置相关,fastcgi配置 /etc/nginx/win-utf【配置文件】编码转换映射转化文件 /etc/sysconfig/nginx【配置文件】用于配置出系统守护进程管理器管理方式 /etc/sysconfig/nginx-debug【配置文件】用于配置出系统守护进程管理器管理方式 /usr/lib/systemd/system/nginx-debug.service【配置文件】用于配置出系统守护进程管理器管理方式 /usr/lib/systemd/system/nginx.service【配置文件】用于配置出系统守护进程管理器管理方式 /usr/lib64/nginx /usr/lib64/nginx/modules【目录】Nginx模块目录 /usr/libexec/initscripts/legacy-actions/nginx /usr/libexec/initscripts/legacy-actions/nginx/check-reload /usr/libexec/initscripts/legacy-actions/nginx/upgrade /usr/sbin/nginx【命令】Nginx服务的启动管理的终端命令 /usr/sbin/nginx-debug【命令】Nginx服务的启动管理的终端命令 /usr/share/doc/nginx-1.12.1【文件、目录】Nginx的手册和帮助文件 /usr/share/doc/nginx-1.12.1/COPYRIGHT【文件、目录】Nginx的手册和帮助文件 /usr/share/man/man8/nginx.8.gz【文件、目录】Nginx的手册和帮助文件 /usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html /var/cache/nginx【目录】Nginx的缓存目录 /var/log/nginx【目录】Nginx的日志目录
|

curl可以理解为浏览器

启动Nginx

关闭nginx
平滑启动 nginx
Nginx默认配置语法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| [root@sparsematrix ~] [root@sparsematrix nginx]
user nginx; worker_processes 1;
error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { worker_connections 1024; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf; }
|
查看/etc/nginx/conf.d/default.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| [root@sparsematrix nginx] [root@sparsematrix conf.d] default.conf [root@sparsematrix conf.d] server { listen 8088; server_name localhost;
location / { root /usr/share/nginx/html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }
}
|
检查配置文件语法

查看access_log


Nginx模块
stub_status
1 2 3 4
| [root@sparsematrix ~] location /mystatus { stub_status; }
|


random_index
1 2 3 4 5 6 7
| [root@sparsematrix test] /data/nginx/test [root@sparsematrix test] 总用量 12 -rw-r--r--. 1 root root 149 8月 21 08:05 1.html -rw-r--r--. 1 root root 149 8月 21 08:06 2.html -rw-r--r--. 1 root root 151 8月 21 08:06 3.html
|
1 2 3 4 5 6 7
| [root@sparsematrix ~] location / { root /data/nginx/test; random_index on; }
|
不断刷新浏览器


