应用场景
- 静态资源服务:通过本地文件系统提供服务;
- 反向代理服务:
- nginx 的强大性能
- 缓存
- 负载均衡
- API服务:OpenResty
nginx 优点
- 高并发、高性能
- 可扩展性
- 高可靠
- 热部署
- BSD 许可证
nginx 的组成
- nginx 二进制可执行文件:由各模块源码编译出的一个文件;
- nginx.conf 配置文件:控制nginx 的行为;
- access.log 访问日志:记录每一条http请求信息;
- error.log:定位问题。
编译nginx
- 下载nginx
下载地址:http://nginx.org/en/download.html,选择Stable version。下载:wget http://nginx.org/download/nginx-1.16.1.tar.gz 解压:tar -zxvf nginx-1.16.1.tar.gz -C /usr/local/
- 目录介绍
drwxr-xr-x. 6 1001 1001 4096 Sep 3 18:37 auto -rw-r--r--. 1 1001 1001 296463 Aug 13 20:51 CHANGES 每个版本的特性和Bugfix -rw-r--r--. 1 1001 1001 452171 Aug 13 20:51 CHANGES.ru 俄罗斯版本的CHANGES drwxr-xr-x. 2 1001 1001 4096 Sep 3 18:37 conf 示例文件 -rwxr-xr-x. 1 1001 1001 2502 Aug 13 20:51 configure 生成中间文件,执行编译前的必备动作 drwxr-xr-x. 4 1001 1001 4096 Sep 3 18:37 contrib drwxr-xr-x. 2 1001 1001 4096 Sep 3 18:37 html 提供了2个标准的html文件 -rw-r--r--. 1 1001 1001 1397 Aug 13 20:51 LICENSE drwxr-xr-x. 2 1001 1001 4096 Sep 3 18:37 man nginx帮助和配置 -rw-r--r--. 1 1001 1001 49 Aug 13 20:51 README drwxr-xr-x. 9 1001 1001 4096 Sep 3 18:37 src 源代码
- configure
查看帮助: ./configure --help | more 编译: yum install pcre pcre-devel yum install zlib zlib-devel ./configure --prefix=/usr/local/nginx 输出如下: + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
- 中间文件介绍
- 编译
执行make 编译
- 安装
执行make install
进入cd /usr/local/nginx 目录,内容如下
drwxr-xr-x. 2 root root 4096 Sep 3 19:01 conf
drwxr-xr-x. 2 root root 4096 Sep 3 19:01 html
drwxr-xr-x. 2 root root 4096 Sep 3 19:01 logs
drwxr-xr-x. 2 root root 4096 Sep 3 19:01 sbin
nginx配置语法
配置文件由指令和指令块组成
每条指令以;分号结尾,指令与参数以空格符号分割
指令以{} 打括号将多条指令组织在一起
include 语句允许组合多个配置文件以提升可维护性
使用#符号添加注释,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式
nginx启动与关闭
- 启动:nginx
- 关闭:nginx -s stop
- 重启:nginx -s reload