Nginx

1 准备

1.1 环境测试

  • 网络: ping baidu.com

  • 确保yum可用: yum list|grep gcc。有返回列表即yum可用

  • 安装依赖:yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

  • 安装常用包: yum -y install wget httpd-tools vim

  • 初始化目录: cd /opt; mkdir app backup download logs work.

2 nginx基础

2.1 什么是nginx

nginx是开源、高性能、可靠的http中间件,代理服务;常见的中间件服务还有Apache;IIS,GWS;

2.2 nginx优势

  • IO多路复用epoll;

  • 轻量级

  • CPU亲和(affinity)

  • sendfile

2.3 快速安装

  1. 访问nginx网站http://nginx.org/en/linux_packages.html#stable,找到yum安装源,create the file named /etc/yum.repos.d/nginx.repo with the following contents:
  1. [nginx]
  2. name=nginx repo
  3. baseurl=http://nginx.org/packages/centos/7/$basearch/
  4. gpgcheck=0
  5. enabled=1
  1. 安装:yum install nginx。 查看安装版本:nginx -v.

2.4 Nginx安装目录

查看nginx安装相关目录: rpm -ql nginx.

路径 类型 作用
/etc/logrotate./nginx 配置文件 Nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx/; /etc/nginx/nginx.conf; /etc/nginx/conf.d/; /etc/nginx/conf.d/default.conf 目录、配置文件 nginx主配置文件
/etc/nginx/fastcgi_params; /etc/nginx/uswgi_params; /etc/nginx/scgi_params; 配置文件 cgi配置相关;fastcgi配置。
/etc/nginx/koi-utf; /etc/nginx/koi-win; /etc/nginx/win-utf 配置文件 编码转换映射转化文件
/etc/nginx/mime.types 配置文件 设置http协议的Content-Type与扩展名对应关系
/usr/lib/systemd/system/nginx-debug.service; /usr/lib/systemd/system/nginx.service; /etc/sysconfig/nginx; /etc/sysconfig/nginx-debug 配置文件 用于配置出系统守护进程管理器管理方式。
/usr/lib64/nginx/modules/; /etc/nginx/modules/ 目录 nginx模块目录
/usr/sbin/nginx; /usr/sbin/nginx-debug 命令 Nginx服务的启动管理器的终端命令
/usr/share/doc/nginx-1.12.1/; /usr/share/doc/nginx-1.12.1/COPYRIGHT; /usr/share/man/man8/nginx.8.gz 文件、目录 Nginx手册和帮助文件
/var/cache/nginx/ 目录 Nginx的缓存目录
/var/log/nginx/ 目录 Nginx的日志目录

2.5 Nginx编译配置参数

  1. 查看命令: nginx -V

  2. 安装编译参数

编译选项 作用
—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 —http-proxy-temp-path=/var/cache/nginx/proxy_temp —http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp —http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp —http-scgi-temp-path=/var/cache/nginx/scgi_temp 执行对应模块时,Nginx所保留的零时性文件
—user=nginx —group=nginx 设定Nginx启动的用户和用户组
—with-cc-opt=parameters 设置额外的参数将被添加到CFLAGS变量
—with-ld-opt=parameters 设置附加的参数,链接系统库

2.6 默认配置语法

  1. 主要默认配置文件: /etc/nginx/nginx.conf; /etc/nginx/conf.d/default.conf

  2. | | 说明 | | —- | —- | | user | 设置nginx服务的系统使用用户 | | worker_processes | 工作进程数 | | error_log | nginx的错误日志 | | pid | nginx服务启动时候pid |

  1. | | | | | —- | —- | —- | | events | worker_connections | 每个进程允许最大连接数 | | | use | 工作进程数 |
  1. 默认配置语法
  1. http{
  2. }

2.7 操作

  • 启动nginx服务: nginx -c /etc/nginx/nginx.conf 或者nginx 或 systemctl start nginx.service

  • 重启: systemctl reload nginx.service

  • 检查nginx配置语法: nginx -tc /etc/nginx/nginx.conf

  • 查看nginx服务进程是否启动: ps -aux|grep nginx

  • 停止nginx: nginx -s stop. 强制停止进程: systemctl stop nginx.service

  • systemctl status nginx.service

2.8 http请求

  1. curl + 访问地址

2.9 nginx日志

11