Nginx服务一 第1章 Nginx介绍 1.1 什么是Nginx? Nginx 是一个开源且高性能、可靠的 Http Web 服务、代理服务。 开源: 直接获取源代码 高性能: 支持海量并发 可靠: 服务稳定
1.2 为什么选择 Nginx 服务 Nginx 非常轻量 功能模块少 (源代码仅保留 http 与核心模块代码,其余不够核心代码会作为插件来安装) 代码模块化 (易读,便于二次开发,对于开发人员非常友好) 互联网公司都选择 Nginx
1.Nginx 技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本, 降低技术更新成本。
1.3 Nginx重要特性 Nginx 采用 Epool 网络模型, Apache 采用 Select 模型 Select: 当用户发起一次请求, select 模型就会进行一次遍历扫描,从而导致性能低下。 Epool: 当用户发起请求, epool 模型会直接进行处理,效率高效,并无连接限制
1.4 Nginx应用场景 Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。
第2章 Nginx安装部署 Nginx分为几种
1.源码编译(1.版本随意 2.安装复杂 3.升级繁琐)
2.epel仓库(1.版本较低 2.安装简单 3.配置不易读)
3.官方仓库(1.版本较新 2.安装简单 3.配置易读,(推荐)
下面分别介绍编译安装和yum安装方法
2.1 编译安装方法 创建www用户 1 2 3 4 [root@web01 ~] [root@web01 ~] [root@web01 ~] uid=666(www) gid=666(www) 组=666(www)
安装依赖包
下载解压软件包 1 2 3 4 [root@web01 ~] [root@web01 ~] [root@web01 /data/soft] [root@web01 /data/soft]
配置编译参数 1 2 3 [root@web01 ~] [root@web01 /data/soft/nginx-1.16.0] [root@web01 /data/soft/nginx-1.16.0]
编译安装 1 [root@web01 /data/soft/nginx-1.16.0]
创建软链接 1 2 3 4 5 [root@web01 /data/soft/nginx-1.16.0] [root@web01 /opt] total 0 lrwxrwxrwx 1 root root 12 Jul 30 10:13 nginx -> nginx-1.16.0 drwxr-xr-x 11 root root 151 Jul 30 10:14 nginx-1.16.0
检查语法 1 2 3 [root@web01 /opt/nginx] nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful
启动nginx
检查测试 1 2 3 [root@web01 /opt/nginx] tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12828/nginx: master [root@web01 /opt/nginx]
2.2 YUM安装方法 安装依赖包
配置官方yum源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [root@web01 ~] [root@web01 ~] [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever /$basearch / gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever /$basearch / gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key
安装nginx服务
启动服务并配置开机自启动 1 2 3 4 5 [root@web01 ~] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~] [root@web01 ~]
测试访问
Nginx启动方式说明 编译安装启动管理方式:
1 2 3 4 nginx -t nginx nginx -s reload nginx -s stop
yum安装启动管理方法:
1 2 3 4 5 nginx -t systemctl start nginx systemctl reload nginx systemctl restart nginx systemctl stop nginx
第3章 Nginx重要配置文件说明 3.1 查看配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@web01 ~] ................................................... /etc/logrotate.d/nginx /etc/nginx/nginx.conf /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/mime.types /etc/sysconfig/nginx /usr/lib/systemd/system/nginx.service /usr/sbin/nginx /usr/share/nginx/html /var/log /nginx ...................................................
3.2 查看已经编译的模块
3.3 配置文件注解 Nginx 主配置文件/etc/nginx/nginx.conf 是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。 Nginx 主配置文件整体分为三块进行学习,分别是 CoreModule(核心模块) EventModule(事件驱动模块) HttpCoreModule(http 内核模块)
3.3.1 第一部分:配置文件主区域配置 1 2 3 4 5 user nginx; worker_processes 1; error_log /var/log /nginx/error.log warn; pid /var/run/nginx.pid;
3.3.2 第二部分:配置文件事件区域 1 2 3 events { worker_connections 1024; }
3.3.3 第三部分:配置http区域 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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; }
3.3.4 第四部分:子配置文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 [root@web01 ~] server { listen 80; 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; } }
http server location 扩展了解项 http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location http{} 标签主要用来解决用户的请求与响应。 server{} 标签主要用来响应具体的某一个网站。 location{} 标签主要用于匹配网站具体 URL 路径
第4章 Nginx虚拟主机配置实战 4.1 基于域名的虚拟主机 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 49 50 51 52 53 54 55 56 57 58 59 [root@wufei /etc/nginx] user www; worker_processes auto; 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; server { listen 80; server_name www.oldboy.com; location / { root /usr/share/nginx/html/www; index index.html index.htm; } } server { listen 80; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } } server { listen 80; server_name bbs.oldboy.com; location / { root /usr/share/nginx/html/bbs; index index.html index.htm; } } }
4.2 基于端口的虚拟主机 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 49 50 51 52 53 54 55 56 57 58 59 [root@wufei /etc/nginx] user www; worker_processes auto; 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; server { listen 80; server_name www.oldboy.com; location / { root /usr/share/nginx/html/www; index index.html index.htm; } } server { listen 81; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } } server { listen 82; server_name bbs.oldboy.com; location / { root /usr/share/nginx/html/bbs; index index.html index.htm; } } }
4.3 基于IP的虚拟主机 添加第二IP:
1 ip addr add 10.0.1.11/24 dev eth0
配置文件:
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 49 [root@web01 ~] 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; server { listen 10.0.1.7:81; server_name www.oldboy.com; location / { root /usr/share/nginx/html/www; index index.html index.htm; } } server { listen 10.0.1.11:82; server_name blog.oldboy.com; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } } }
第5章 Nginx虚拟主机配置优化 所有配置都写入一个配置文件维护起来比较麻烦,如果修改错了,影响整个服务起不来,所以我们应该拆分nginx的配置文件为各个子配置
5.1 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 [root@wufei /etc/nginx] user www; worker_processes auto; 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; }
5.2 编辑子配置文件www、blog、bbs 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 [root@wufei /etc/nginx/conf.d] server { listen 80; server_name www.oldboy.com; access_log /var/log /nginx/www.log main; location / { root /usr/share/nginx/html/www; index index.html index.htm; } } [root@wufei /etc/nginx/conf.d] server { listen 80; server_name blog.oldboy.com; access_log /var/log /nginx/blog.log main; location / { root /usr/share/nginx/html/blog; index index.html index.htm; } } [root@wufei /etc/nginx/conf.d] server { listen 80; server_name bbs.oldboy.com; access_log /var/log /nginx/bbs.log main; location / { root /usr/share/nginx/html/bbs; index index.html index.htm; } }
5.3 创建代码目录及首页 1 2 3 4 [root@web01 /etc/nginx/conf.d] [root@web01 /etc/nginx/conf.d] [root@web01 /etc/nginx/conf.d] [root@web01 /etc/nginx/conf.d]
5.4 检查语法重启服务 1 2 3 4 [root@web01 ~] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 ~]
5.5 配置/etc/hosts文件 1 2 [root@web01 ~] 10.0.1.7 www.oldboy.com blog.oldboy.com bbs.oldboy.com
5.6 访问测试 1 2 3 4 5 6 [root@web01 ~] www [root@web01 ~] blog [root@web01 ~] bbs
第6章 Nginx状态模块 nginx状态模块: –with-http_stub_status_module
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@wufei /etc/nginx/conf.d] server { listen 80; server_name status.oldboy.com; stub_status on; access_log off; } [root@wufei /etc/nginx/conf.d] 10.0.1.7 www.oldboy.com blog.oldboy.com bbs.oldboy.com status.oldboy.com [root@wufei /etc/nginx/conf.d] nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@wufei /etc/nginx/conf.d] [root@wufei /etc/nginx/conf.d] Active connections: 1 server accepts handled requests 25 25 22 Reading: 0 Writing: 1 Waiting: 0
nginx状态信息如下:
1 2 3 4 5 6 7 8 9 10 11 12 Active connections accepts handled requests Reading Writing Waiting keepalive——timeout 0; keepalive——timeout 65;
第7章 Nginx日志 日志字段解释:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $remote_addr $remote_user $time_local $time_iso8601 $request $status $body_bytes_sent $bytes_sent $msec $http_referer $http_user_agent $http_x_forwarded_for $request_length $request_time
nginx不停服务进行切割日志:
编辑切割日志脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@web01 /server/scripts] #!/bin/bash log_path=/var/log /nginx YESTERDAY=`date -d "yesterday" +%Y-%m-%d_%S` nginx_cmd="/usr/sbin/nginx" /bin/mv ${log_path} /access.log ${log_path} /access.$YESTERDAY .log /bin/mv ${log_path} /error.log ${log_path} /error.$YESTERDAY .log ${nginx_cmd} -s reopen
设置定时任务,每天晚12点切割
1 2 3 [root@web01 /server/scripts] 0 0 * * * /bin/bash /server/scripts/nginx_log.sh