• 官网:https://nginx.org/
  • 官方文档:
    1. Building nginx from Sources - http://nginx.org/en/docs/configure.html
  • 视频:
  1. https://www.bilibili.com/video/av58516750
  2. https://www.bilibili.com/video/BV1ov41187bq?p=2

文章目录

  • 常用模块、核心祖成
  • 安装、编译
    • # 方法1:工具安装nginx
    • # 方法2:centos源码安装nginx
      • 坑: 编译错误 cd.current_salt[0] = ~salt[0]
      • 问题: 工具安装和源码安装的差异
    • # 方法2:ubuntu源码安装nginx ⭐️
    • # 移除nginx安装
    • # 配置文件 configure 解释
    • # 配置成系统服务 systemctl enable ⭐️
    • # 配置 软链接 ln or PATH ⭐️
  • 线程模型
  • 启停
    • # 进程管理/信号SIGNAL
  • 升级(不停机/不停nginx服务)
    • # 环境准备
    • # 方案一: 使用Nginx服务信号进行升级
    • # 方案二: 使用Nginx安装目录的make命令完成升级
  • 目录结构
  • 配置:nginx.conf ⭐️
    • # 全局块: 用户 user
    • # 全局块: 线程数 master_process、worker_processes
    • # 全局块: daemon
    • # 全局块: pid
    • # 全局块: 错误日志 error_log
      • 坑: 关闭日志
    • # 全局块: 引入配置 include
    • # events块: 事件配置
    • # events块: 接收锁 accept_mutex
    • # events块: 接收多个 multi_accept
    • # events块: worker_connections
    • # events块: 事件驱动方式 use
    • # http块: 默认响应类型 default_type、mime.type
    • # http块: 自定义服务日志:access.log、error.log
    • # http块: sendfile (静态文件传输优化)
    • # http块: keepalive_timeout
    • # http块: keepalive_requests
    • # server块和location块 ⭐️
    • # server块: 监听端口 listen ⭐️
      • 标识符:default_server
    • # server块:server_name ⭐️⭐️
      • 配置方式一:精确匹配
      • 配置方式二:通配符 `*`
        • 坑:通配符不能出现在域名的中间 ❗
      • 配置方式三:正则表达式 `~`
        • 注意: 命名的正则表达式捕获组在后面可以作为变量使用
      • 问题: 匹配执行顺序
    • # server块: 变量设置 set ⭐️
    • # location 块 ⭐️
    • # loaction块: 资源目录 root(拼接)、alias(替换)
    • # location块: 首页 index
    • # location块: 错误页 error_page
    • # location块: 逻辑判断 ⭐️
      • if (condition) {...}
      • break
      • 常用全局变量 ⭐️⭐️⭐️
    • # location块: 返回 return
    • # location块: 地址重写 rewrite ⭐️⭐️⭐️
      • 总结: last、break、redirect、permanent区别
      • 重写日志 rewrite_log
      • 例子: 域名重写
      • 例子: 重写时,携带请求URI
      • 例子: 多域名重写为一个
      • 例子: 一个域名重写为多个
      • 例子: 独立域名 ⭐️
      • 例子: 对外接口统一加上前缀(rewrite+proxy_pass) ⭐️
      • 相对路径请求的处理 absolute_redirect 、server_name_in_redirect、port_in_redirect
      • 坑: 自动添加"`/`"并301重定向
      • 例子: 合并目录(SEO优化)
      • 例子: 防盗链
        • 实现一: Referer 头信息 valid_referers (简陋)
        • 实现一(优化): rewrite 优化显示
        • 实现二: ngx_http_accesskey_module
    • # 代理配置 ⭐️⭐️⭐️
      • 代理目的地 proxy_pass
      • 坑: 是否加 / ⚠️⚠️⚠️
      • 例子: 正向代理
      • 例子: 反向代理
      • proxy_set_header
      • proxy_redirect
      • 反向代理系统调优
      • 反向代理系统调优:proxy_buffering
      • 反向代理系统调优:proxy_buffers
      • 反向代理系统调优:proxy_buffer_size
      • 反向代理系统调优:proxy_busy_buffers_size
      • 反向代理系统调优:proxy_temp_path
      • 反向代理系统调优:proxy_temp_file_write_size
  • 案例
    • # 静态资源传输优化
      • sendfile(开启高效的文件传输模式)
      • tcp_nopush(提升网络包的传输'效率')
      • tcp_nodelay(提高网络包传输的'实时性')
    • # 静态资源压优化
      • Gzip模块配置:gzip
      • Gzip模块配置:gzip_types
      • Gzip模块配置:gzip_comp_level
      • Gzip模块配置:gzip_vary
      • Gzip模块配置:gzip_buffers
      • Gzip模块配置:gzip_disable
      • Gzip模块配置:gzip_http_version
      • Gzip模块配置:gzip_min_length
      • Gzip模块配置:gzip_proxied
      • Gzip压缩功能的实例配置
      • Gzip和sendfile共存问题
      • gzip_static模块配置
    • # 静态资源的缓存
      • 浏览器缓存相关指令:expires
      • 浏览器缓存相关指令:add_header
    • # 跨域问题:add_header
    • # Nginx的安全控制 ⭐️
      • ngx_http_ssl_module
      • ssl、ssl_certificate、ssl_certificate_key
      • 申请证书:openssl
      • 申请证书:免费ssl证书供应商
      • ssl_session_cache
      • ssl_session_timeout
      • ssl_ciphers
      • ssl_prefer_server_ciphers
      • 开启SSL实例
  • 可能遇到的问题

常用模块、核心祖成

Nginx常用模块

  1. 静态资源部署
  2. Rewrite地址重写
    正则表达式
  3. 反向代理
  4. 负载均衡
    轮询、加权轮询、ip_hash、url_hash、fair
  5. web缓存
  6. 环境部署
    高可用
  7. 用户认证模块…

Nginx核心组成

  1. nginx 二进制可执行文件
  2. nginx.conf 配置文件
  3. error.log 错误日志记录
  4. access.log 访问日志记录

安装、编译

确保linux内核2.6以上,因为linux2.6以上内核才支持epool。Ngnix需要用到epoll解决高并发压力问题。

uname -a # 确定内核版本

停用selinux

# 方法1:工具安装nginx

https://nginx.org/en/linux_packages.html

# 方法2:centos源码安装nginx

# centos
yum install -y gcc pcre-devel zlib zlib-devel openssl openssl-devel
# ubuntu
# https://www.cnblogs.com/schips/p/12309201.html
sudo  apt-get install gcc build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libpcrecpp0v5 -y
  • gcc
    c语言编译器
  • pcre http://www.pcre.org/
    PCRE库(perl Compatible Regular Expression 兼容正则表达式库)。
    nginx在Rewrite模块和http核心模块都会使用
  • zlib
    zlib库提供了压缩算法,在nginx的各个模块中需要使用gzip压缩
  • OpenSSL
    SSL:Secure Sockets Layer 安全套接协议
# 包管理
mkdir -p nginx/core
cd nginx/core
# 下源码
wget http://nginx.org/download/nginx-1.12.2.tar.gz
# 解压缩
tar -xzf nginx-1.12.2.tar.gz
cd nginx-1.12.2/# 配置1
./configure
# Configuration summary
#   + 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"# 配置2
./configure --prefix=/usr/local/nginx
# Configuration summary
#   + 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"
# # 配置3:对于centos似乎要下面这样指定了,安装后才会有配置
./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock
#Configuration summary
#  + 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 # make -f objs/Makefile install
# make[1]: Entering directory '/home/lawsssscat/Downloads/nginx/core/nginx-1.12.2'
# test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
# test -d '/usr/local/nginx/sbin' \
#   || mkdir -p '/usr/local/nginx/sbin'
# test ! -f '/usr/local/nginx/sbin/nginx' \
#   || mv '/usr/local/nginx/sbin/nginx' \
#       '/usr/local/nginx/sbin/nginx.old'
# cp objs/nginx '/usr/local/nginx/sbin/nginx'
# test -d '/usr/local/nginx/conf' \
#   || mkdir -p '/usr/local/nginx/conf'
# cp conf/koi-win '/usr/local/nginx/conf'
# cp conf/koi-utf '/usr/local/nginx/conf'
# cp conf/win-utf '/usr/local/nginx/conf'
# test -f '/usr/local/nginx/conf/mime.types' \
#   || cp conf/mime.types '/usr/local/nginx/conf'
# cp conf/mime.types '/usr/local/nginx/conf/mime.types.default'
# test -f '/usr/local/nginx/conf/fastcgi_params' \
#   || cp conf/fastcgi_params '/usr/local/nginx/conf'
# cp conf/fastcgi_params \
#   '/usr/local/nginx/conf/fastcgi_params.default'
# test -f '/usr/local/nginx/conf/fastcgi.conf' \
#   || cp conf/fastcgi.conf '/usr/local/nginx/conf'
# cp conf/fastcgi.conf '/usr/local/nginx/conf/fastcgi.conf.default'
# test -f '/usr/local/nginx/conf/uwsgi_params' \
#   || cp conf/uwsgi_params '/usr/local/nginx/conf'
# cp conf/uwsgi_params \
#   '/usr/local/nginx/conf/uwsgi_params.default'
# test -f '/usr/local/nginx/conf/scgi_params' \
#   || cp conf/scgi_params '/usr/local/nginx/conf'
# cp conf/scgi_params \
#   '/usr/local/nginx/conf/scgi_params.default'
# test -f '/usr/local/nginx/conf/nginx.conf' \
#   || cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
# cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
# test -d '/usr/local/nginx/logs' \
#   || mkdir -p '/usr/local/nginx/logs'
# test -d '/usr/local/nginx/logs' \
#   || mkdir -p '/usr/local/nginx/logs'
# test -d '/usr/local/nginx/html' \
#   || cp -R html '/usr/local/nginx'
# test -d '/usr/local/nginx/logs' \
#   || mkdir -p '/usr/local/nginx/logs'
# make[1]: Leaving directory '/home/lawsssscat/Downloads/nginx/core/nginx-1.12.2'
坑: 编译错误 cd.current_salt[0] = ~salt[0]

解决方法:https://blog.csdn.net/wuekun/article/details/106842216

  1. 删除 objs/Makefile 里 CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused -Werror -g 后面的 后面的 -Werror -g
  2. vi src/os/unix/ngx_user.c

编译包里有什么?

  • auto
    编译相关脚本文件
  • CHANGES
    变更记录
  • conf
    配置文件
    绝大多数功能在 conf/nginx.con 配置
  • configure
    c语言检测环境、生成makefile
  • contrb
    介绍 contribute
  • html
    相关html页面
  • LICENSE
    许可证
  • man
    nginx帮助文档
  • objs、Makefile
    ./configure生成的内容
  • README
  • src
    源代码

问题: 工具安装和源码安装的差异

工具安装和源码安装的差异:主要差异在于缺少了配置记录

  • 源码编译安装(centos)

    源码编译安装(ubuntu)
  • 工具安装(centos)

    工具安装(ubuntu)

# 方法2:ubuntu源码安装nginx ⭐️

《Ubuntu下编译安装Nginx及相关设置》 - https://www.linuxidc.com/Linux/2017-09/147157.htm

  1. 安装依赖项

    sudo apt-get update
    sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxslt1-dev libxml2-dev libgd2-xpm-dev libgeoip-dev libgoogle-perftools-dev libperl-dev libtool
    sudo apt-get install openssl
    
  2. 下载新版本,到官网复制下载链接
    wget http://nginx.org/download/nginx-1.13.5.tar.gz
    
  3. 解压
    tar -zxvf nginx-1.13.5.tar.gz
    
  4. 编译安装
    # 进入解压目录:
    cd nginx-1.13.5
    # 配置:这里额外安装几个模块
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream --with-mail=dynamic
    # 编辑nginx:
    sudo make
    # 安装nginx:
    sudo make install
    # 启动nginx:
    sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    #注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
    # 查看nginx进程:
    ps -ef|grep nginx
    

    【笔记】nginx - 上:安装、配置(基础、rewrite 重写、proxy_pass 代理、ssl)相关推荐

    1. codeigniter在nginx安装配置及URL重写

      目录 [hide] 1 codeigniter修改 2 修改nginx配置 3 访问url codeigniter(CI)是一个轻量型的PHP优秀框架,但是它是在apache服务器下开发的,在ngin ...

    2. 如何在Debian上安装配置ownCloud

      如何在Debian上安装配置ownCloud 据其官方网站,ownCloud可以让你通过一个Web界面或者WebDAV访问你的文件.它还提供了一个平台,可以轻松地查看.编辑和同步您所有设备的通讯录.日 ...

    3. Nginx的location匹配与rewrite重写跳转

      目录 Nginx的location匹配与rewrite重写跳转 一.常用的Nginx正则表达式 二.location 分类 location常用的匹配规则 优先级 location示例说明 Rewri ...

    4. nginx下安装配置modsecurity waf防火墙(附完整编译、配置、排错、详细规则)

      ModSecurity是一个免费.开源的Apache模块,可以充当Web应用防火墙(WAF).ModSecurity是一个入侵探测与阻止的引擎.它主要是用于Web应用程序所以也可以叫做Web应用程序防 ...

    5. linux 安装nginx php mysql 配置文件在哪_linux下 php+nginx+mysql安装配置

      我主要是用来安装php,以及nginx和php的交互. 一 安装插件 可以选择YUM安装或者源码编译安装gccgcc-c++zlib pcre pcre-devel libevent libevent ...

    6. 在CentOS7上安装配置Corosync高可用集群过程全记录

      在CentOS7上安装配置Corosync高可用集群过程全记录 一.环境.拓朴及其他准备工作: 1-1:准备网络YUM源环境: All Nodes OS CentOS 7.3 x86_64: # wg ...

    7. 404 not found nginx是什么意思_如何在BT面板基于nginx环境安装配置Perfex CRM网站

      如何在BT面板基于nginx环境安装配置Perfex CRM网站 第一次接触到这个小众CRM,源于一哥们以30块大洋就收买了我,谁让我穷呢? 官网:https://www.perfexcrm.com ...

    8. 在windows上安装配置msys2

      在windows上安装配置msys2 msys2安装 启动msys2 msys2中文乱码问题 MSYS2 目录映射问题 MSYS2 ln -s软连接会复制目录的问题 fastab配置文件目录的方法 m ...

    9. 在 Mac OSX 版的 LispBox 环境上安装配置 SBCL 详细过程

      2019独角兽企业重金招聘Python工程师标准>>> 在 Mac OSX 版的 LispBox 环境上安装配置 SBCL 详细过程 今天在OSX版的 LispBox(使用CCL) ...

    10. linux ubuntu安装 mono,在 Ubuntu Server 上安装配置 Mono 生产环境

      在 Ubuntu Server 上安装配置 Mono 生产环境 在 Ubuntu Server 上安装和配置 Apache2 + Mono 生产环境的记录. 服务器环境是 Ubuntu Server ...

    最新文章

    1. 持续集成(一)为什么我们迫切需要持续集成
    2. Prewitt 边缘检测
    3. 英特尔更新人工智能产品线,新品性能达到竞品6倍
    4. PHP编写命令行脚本和后台运行程序的注意事项
    5. 使用pandas进行量化回测(akshare)
    6. ubuntu窗口最小化消失,任务栏上无法找到的解决方法
    7. 需求跟踪矩阵模板_需求可追溯性矩阵(RTM)生成程序使用说明
    8. c语言学习-输入一个十进制数,输出其对应的八进制数据
    9. 关于Ajax的一揽子工程(2)
    10. 移动边缘计算——MEC
    11. 如何将绿色软件制作成云端平台中的软件包
    12. SpringCloud 学习(二)-2 :Securing The Eureka Server
    13. js实现canvas在线画板
    14. Android 缩放动画 ScaleAnimation
    15. 2014中秋节,用java为QQ游戏美女找茬写辅助
    16. 我是如何一步步获取房东的WiFi后台管理密码的【社工思路】
    17. 从零开始搭建完整的电影全栈系统(六)——影片Api示例、说明及应用
    18. 异次元发卡网邮箱SMTP配置教程
    19. 中断处理过程示意图_中断和中断处理流程
    20. 4 种最安全的 MacBook 电池更换选项

    热门文章

    1. AESECB加密算法 C 语言代码实现
    2. Android Studio 卡在Download fastutil-8.4.0-sources jar以及New Module为灰色的问题及解决方法
    3. 不定积分 定积分 计算方法
    4. python中美元人汇率_Python获取美元人民币实时汇率
    5. 标准C++为什么没有垃圾回收(Garbage Collection)
    6. 修改element ui tree 搜索功能,实现分级搜索,关键字高亮
    7. kali 触摸板手势之fusuma
    8. 我的2020年度总结 “既往不恋,纵情向前”
    9. 为pr视频文件添加字幕
    10. Java删除Maven下的.lastUpdated文件