目录

  • ​​一: Nginx三个重要的使用场景​​
  • ​​二: Nginx的优点​​
  • ​​三:Nginx文件的组成部分​​
  • ​​四:编译Nginx​​

一: Nginx三个重要的使用场景

  • 静态资源服务
  • 疑问:为什么需要静态资源服务?
  • 答: 在整个的web请求流程中(浏览器—> Nginx ----> Web服务器 ----> 数据库/缓存)有些静态资源, 没必要通过Web服务器再去寻找到这些资源, 比如: HTML, CSSS, JS, 图片等。因此我们可以将这些静态资源, 放在服务器上, 当服务请求到Nginx服务器的时候, 由Nginx服务器直接访问服务器上的资源, 然后立刻返回即可。
  • 反向代理服务
  • 疑问: 什么是反向代理?
  • 答:简言之, 代理多台web服务器, 浏览器对多台Web服务器是无感知的, 只知道将请求转发到代理服务器(Nginx)上即可。
  • 反向代理流程:由客户端发起对代理服务器的请求,代理服务器在中间将请求转发给某一个服务器,服务器将结果返回给代理服务器,代理服务器再将结果返回给客户端。
  • 疑问:为什么需要反向代理?
  • 答:随着业务的不断发展, 应用服务一般都是集群模式的, 因此会带来两个需求。 1: 横向扩容 2: 容灾。横向扩容就需要部署多台服务器, 而请求需要转发到那台服务器呢? 因此就需要Nginx具备负载均衡的能力。 容灾就是指的, 一旦某个业务服务出现问题, Nginx就将原本转发到这个服务器的请求, 转发给其他的服务器。基于这两点需求, Nginx必须具备反向代理业务服务器的功能。
  • API服务
  • 疑问:为什么需要API服务?
  • 答:有些请求, 功能简单, 没必要用业务服务器来实现, 而是直接访问数据库可以查询到。 而且数据库服务无论是稳定性还是并发能力, 都比业务服务器高的多。

二: Nginx的优点

  • 高并发, 高性能
  • nginx 可以达到数千万的并发能力
  • 可扩展性好
  • 模块化设计, 有丰富的生态圈
  • 高可靠
  • 持续运行, 不会挂掉
  • 热部署
  • 不用停止服务,进行升级。
  • BSD许可证
  • 开源, 可以对源码进行修改, 并应用到商业软件中

三:Nginx文件的组成部分

  • 二进制可执行文件
  • 各个模块编译出来的一些文件
  • 配置文件
  • nginx.conf
  • 日志文件
  • access.log
  • error.log

四:编译Nginx

  • 下载Nginx源码并解压
  • wget http://nginx.org/download/nginx-1.14.0.tar.gz
  • tar -zxvf nginx-1.14.0.tar.gz
  • 了解Nginx源码目录结构
drwxr-xr-x  13 kj  staff   416B  4 17  2018 .
drwx------+ 24 kj staff 768B 12 31 16:00 ..
-rw-r--r-- 1 kj staff 280K 4 17 2018 CHANGES # 版本特性和bugfix
-rw-r--r-- 1 kj staff 427K 4 17 2018 CHANGES.ru # 俄罗斯版本的
-rw-r--r-- 1 kj staff 1.4K 4 17 2018 LICENSE #
-rw-r--r-- 1 kj staff 49B 4 17 2018 README
drwxr-xr-x 25 kj staff 800B 4 17 2018 auto # 目录中存储:编译cc,依赖lib, 操作系统os, 类型types
drwxr-xr-x 11 kj staff 352B 4 17 2018 conf # 配置案例文件
-rwxr-xr-x 1 kj staff 2.4K 4 17 2018 configure # 编译前的执行脚本文件
drwxr-xr-x 6 kj staff 192B 4 17 2018 contrib # 高亮显示nginx配置
drwxr-xr-x 4 kj staff 128B 4 17 2018 html # 提供了两个标准HTML: index.html和50x.html
drwxr-xr-x 3 kj staff 96B 4 17 2018 man # 帮助文档
drwxr-xr-x 9 kj staff 288B 4 17 2018 src # 源代码
  • 高亮显示nginx配置
  • cp -r contrib/vim/* ~/.vim/
  • 了解configure支持哪些参数?
  • ./configure --help
# 第一类:制定输出文件位置, 一般制定前缀即可。
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
# 第二类:默认不会编译到nginx中的, 需要指定下面的参数, 才会编译到nginx模块中。
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_http_mp4_module
--with-http_gunzip_module enable ngx_http_gunzip_module
--with-http_gzip_static_module enable ngx_http_gzip_static_module
--with-http_auth_request_module enable ngx_http_auth_request_module
--with-http_random_index_module enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module
# 第三类:默认会编译到nginx模块中的, 如果指定下面的参数, 则不会编译到nginx中。  
--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_mirror_module disable ngx_http_mirror_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_grpc_module disable ngx_http_grpc_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_conn_module disable ngx_http_limit_conn_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_hash_module
disable ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module
--without-http_upstream_zone_module
disable ngx_http_upstream_zone_module
# 第四类:编译过程中一些优化参数: 例如输出debug日志等。
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64

--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation support

--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro

--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources

--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL

--with-debug enable debug logging
  • 执行编译:
  • ./configure --prefix=/Users/kj/Desktop/nginx
  • 了解编译后的文件
  • 编译完成后回多一个目录:objs
XYSZW00468:objs kj$ tree
.
├── autoconf.err
├── ngx_auto_config.h
├── ngx_auto_headers.h
└── ngx_modules.c # 这个文件存放着一会安装的时候需要安装哪些模块
  • 执行make编译
  • 执行make install 进行安装
  • 安装成功后, 在自己指定的路径下回多出很多文件, 包括: log等
  • mac 安装与启动nginx:
  • 安装: brew install nginx
  • 启动:brew services start/restart/stop nginx
  • 查看安装相关信息: brew info nginx