更新nginx官方yum源html
vim /etc/yum.repos.d/nginx.repo [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
yum安装nginx软件nginx
yum -y install nginx
启动nginx服务,并设置开机自启,打开浏览器检查nginx是否安装成功web
systemctl start nginx systemctl enable nginx
利用脚本实现切割 (定时任务)算法
#!/bin/bash mv /var/log/nginx/access.log /var/log/nginx/access_$(date +%F).log systemctl restart nginx #重启nginx是为了让其从新产生access.log文件
利用专用的切割程序-logrotateshell
vim /etc/logrotate.conf # rotate log files weekly weekly #--- 定义默认日志切割的周期 # keep 4 weeks worth of backlogs rotate 4 #--- 定义只保留几个切割后的文件 # create new (empty) log files after rotating old ones create #--- 建立出一个相同的源文件 # use date as a suffix of the rotated file dateext #--- 定义角标(扩展名称信息) # uncomment this if you want your log files compressed #compress #--- 是否对切割后的文件进行压缩处理 # RPM packages drop log rotation information into this directory include /etc/logrotate.d #--- 加载包含/etc/logrotate.d/目录中文件配置 # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { #--- 单独对某个文件进行切割配置 monthly create 0664 root utmp minsize 1M #--- 最小大小为1M,小于1M不进行切割 rotate 1 }
vim /etc/nginx/nginx.conf #--- 主配置文件 #第一个部分: 配置文件主区域配置 user www; #--- 定义worker进程管理的用户 (此时改成www 因此要确保系统上有此用户) 补充: nginx的进程 master process: 主进程 #---管理服务是否可以正常运行 boss worker process: 工做进程 #---处理用户的访问请求 员工 worker_processes 2; #---定义有几个worker进程 == CPU核数 / 核数的2倍 error_log /var/log/nginx/error.log warn; #--- 定义错误日志路径信息 pid /var/run/nginx.pid; #--- 定义pid文件路径信息 #第二个部分: 配置文件事件区域 events { worker_connections 1024; #--- 一个worker进程能够同时接收1024访问请求 } #第三个部分: 配置http区域 http { include /etc/nginx/mime.types; #--- 加载一个配置文件 default_type application/octet-stream; #--- 指定默认识别文件类型 log_format jiage '$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 jiage;# 这里的jiage 引用上面jiage 的日志格式 #--- 指定日志路径 sendfile on; ??? #tcp_nopush on; ??? keepalive_timeout 65; #--- 超时时间 #gzip on; include /etc/nginx/conf.d/*.conf; #--- 加载一个配置文件 } cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak grep -Ev '^$|#' /etc/nginx/conf.d/default.conf.bak > /etc/nginx/conf.d/default.conf vim /etc/nginx/nginx.d/default #--- 扩展配置(虚拟主机配置文件) #第四个部分: server区域信息(配置一个网站 www/bbs/blog -- 一个虚拟主机) server { listen 80; #--- 指定监听的端口 server_name www.jiage.com; #--- 指定网站域名 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; } }
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; 调用日志格式 $remote_addr 显示用户访问源IP地址信息 $remote_user 显示认证的用户名信息 [$time_local] 显示访问网站时间 "$request" 请求报文的请求行信息 $status 用户访问网站状态码信息 $body_bytes_sent 显示响应的数据尺寸信息 $http_referer 记录调用网站资源的链接地址信息(防止用户盗链) 老男孩nginx---access.log---莫文杰(荒原饮露---老男孩图片连接)---http_referer(连接) $http_user_agent 记录用户使用什么客户端软件进行访问页面的 (谷歌 火狐 IE 安卓 iphone) $http_x_forwarded_for 负载均衡相关,记录真实客户端的ip地址
错误日志: /var/log/nginx/error.log --- Core functionality Syntax: error_log file [level]; 指定错误日志路径以及错误日志记录的级别 Default: error_log logs/error.log error; Context: main, http, mail, stream, server, location error_log /var/log/nginx/error.log warn; 错误级别: debug :调试级别, 服务运行的状态信息和错误信息详细显示 信息越多 info :信息级别, 只显示重要的运行信息和错误信息 notice :通知级别: 更加剧要的信息进行通知说明 warn :警告级别: 可能出现了一些错误信息,但不影响服务运行 error :错误级别: 服务运行已经出现了错误,须要进行纠正 推荐选择 crit :严重级别: 必须进行修改调整 alert :严重警告级别: 即警告,并且必须进行错误修改 emerg :灾难级别: 服务已经不能正常运行 信息越少 PS: 日志文件信息须要作切割处理防止容量过大分析麻烦
location进行uri匹配apache
vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index jiage.html; error_page 404 /404.jpg } }
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: — Context: server, location location = / { --- 精确匹配 优先级01 最高 [ configuration A ] } location / { --- 默认匹配 优先级04 最低 [ configuration B ] } location /documents/ { --- 按照目录进行匹配 优先级03 [ configuration C ] } location ^~ /images/ { --- 优先匹配/不识别uri信息中符号信息 优先级02 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { --- 不区分大小写进行匹配 优先级03 [ configuration E ] }
例子:vim
server{ listen 80; server_name test.location.com; location = / { return 404; } location / { return 403; } location /documents/ { return 500; } location ^~ /images/ { return 502; } location ~* \.(gif|jpg|jpeg)$ { return 504; } }
vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location /jiage { root /usr/share/nginx/html; index jiage.html; } }
编写主页文件以html结尾centos
vim /usr/share/nginx/html/jiage.html <!DOCTYPE html> <html> <meta charset="utf-8"> <head> <title>这个是标题</title> </head> <body> <h1>这是一个一个简单的HTML,h1 <p>Hello </p> </h1> </h5> </body> </html>
重启nginx服务(平滑重启)浏览器
mkdir -p /usr/share/nginx/html/jiage/jiage.html systemctl reload nginx
nginx 命令参数缓存
-t : test configuration and exit
检查测试配置文件语法
-s : send signal to a master process: stop, quit, reopen, reload
控制服务中止或者从新启动
编写DNS配置信息 /etc/hosts
进行访问测试
浏览器中:http://www.jiage.com/jiage
文本界面: curl www.jiage.com/jiage
部署搭建网站常见错误:
网站服务配置文件编写不正确
404 错误
解决方法一: 修改nginx配置文件---location
解决方法二: 在站点目录中建立相应目录或文件数据信息
403 错误
解决方法一: 不要禁止访问
解决方法二: 由于没有首页文件
DNS信息配置不正确
[root@web02 conf.d]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; } } [root@web02 conf.d]# vim /etc/nginx/conf.d/bbs.conf server { listen 80; server_name bbs.jiage.com; location / { root /html/bbs; index index.html; } } [root@web02 conf.d]# vim /etc/nginx/conf.d/blog.conf server { listen 80; server_name blog.jiage.com; location / { root /htmlblog; index index.html; } } [root@web02 conf.d]#systemctl reload nginx
[root@web02 conf.d]# mkdir -p /html/{www,bbs,blog} [root@web02 conf.d]# for name in {www,bbs,blog}; do echo "10.0.0.8 $name.jiage.com" > /html/$name/index.html;done [root@web02 conf.d]# for name in {www,bbs,blog};do cat /html/$name/index.html ;done 10.0.0.8 www.jiage.com 10.0.0.8 bbs.jiage.com 10.0.0.8 blog.jiage.com
在客户端上编写hosts解析文件
10.0.0.8 www.jiage.com bbs.jiage.com blog.jiage.com
进行访问测试
注意:利用浏览器访问是要清除缓存,或者使用无痕模式访问
ps:服务配置文件中涉及到地址修改,必须重启nginx服务,不能平滑重启
例: 10.0.0.0/24 www.jiage.com/AV 不能访问
172.16.1.0/24 www.jiage.com/AV 能够访问
nginx访问模块: ngx_http_access_module 举例配置: location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; } 指令用法 Syntax: deny address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except
[root@web02 conf.d]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; } location /AV { deny 10.0.0.0/24; allow 172.16.1.0/24; root /html/www; index index.html; } }
ps: location 外面的信息,全局配置信息
location里面的信息,局部配置信息
nginx认证模块: ngx_http_auth_basic_module 举例配置: location / { auth_basic "closed site"; --- 开启认证功能 auth_basic_user_file conf/htpasswd; --- 加载用户密码文件 }
server { listen 80; server_name www.jiage.com; location / { root /html/www; index index.html; auth_basic "jiage"; #密码忘了以后的提醒信息 auth_basic_user_file password/htpasswd; }
建立密码文件(文本中密码信息必须是密文的)
使用命令: htpasswd 建立一个有密文信息的密码文件 该命令系统默认是没有安装的
[root@web02 conf.d]# provides htpaswd httpd-tools-2.4.6-89.el7.centos.x86_64 #能够看到htpasswd的安装包为这个 htpasswd命令参数说明: -c Create a new file. ***** 建立一个密码文件 -n Don't update file; display results on stdout. 不会更新文件; 显示文件内容信息 -b Use the password from the command line rather than prompting for it. ***** 免交互方式输入用户密码信息 -i Read password from stdin without verification (for script usage). 读取密码采用标准输入方式,并不作检查 ??? -m Force MD5 encryption of the password (default). md5的加密算法 -B Force bcrypt encryption of the password (very secure). 使用bcrypt对密码进行加密 -C Set the computing time used for the bcrypt algorithm (higher is more secure but slower, default: 5, valid: 4 to 31). 使用bcrypt algorithm对密码进行加密 -d Force CRYPT encryption of the password (8 chars max, insecure). 密码加密方式 -s Force SHA encryption of the password (insecure). 加密方式 -p Do not encrypt the password (plaintext, insecure). 不进行加密 -D Delete the specified user. 删除指定用户 -v Verify password for the specified user.
[root@web02 conf.d]# htpasswd -bc /html/www/password/htpasswd jiage 123456 [root@web02 conf.d]# chmod 600/html/www/password/htpasswd ps:根据配置文件建立加密文件 用户名: jiage; 密码:123456
[root@web02 password]# curl www.jiage.com -u jiage:123456 10.0.0.8 www.jiage.com
nginx模块功能: ngx_http_autoindex_module Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location server { listen 80; server_name www.jiage.com; location / { root /html/www; #index index.html; auth_basic "jiage"; #密码忘了以后的提醒信息 auth_basic_user_file password/htpasswd; autoindex on; #开启nginx站点索引功能 charset utf-8; # 修改目录结构中出现的中文乱码问题 } ps:文件共享服务器不须要有主页文件,或者修改配置文件中主页文件的名字与站点目录下的不一致。
例子:访问www.jiage.com 直接输入jia.com 就直接访问大www服务器
编写配置文件
server_name www.jiage.com jia.com;
做用:
1. 编写网站访问测试
状态模块: ngx_http_stub_status_module location = /basic_status { stub_status; }
编写配置文件
[root@web01 conf.d]# vim state.conf server { listen 80; server_name state.jiage.com stub_status; }
重启nginx服务,并在客户端写好域名解析
ps: 页面显示信息介绍
Active connections: 激活的链接数信息 4000用户 3500 accepts: 接收的链接数汇总(综合) TCP handled: 处理的链接数汇总(综合) TCP requests: 总计的请求数量 HTTP协议请求 Reading: nginx服务读取请求报文的数量 100人点餐 Writing: nginx服务响应报文信息数量 100人响应 Waiting: nginx队列机制,要处理(读取或者响应保存进行保存) 监控
利用rewrite模块实现跳转功能: http_rewrite_module
Syntax: rewrite regex replacement [flag]; rewite 匹配的正则信息 替换成什么信息
Default: —
Context: server, location, if
出现无限跳转如何解决:
第一种方法: 利用不一样server区块配置打破循环
server{ server_name jiage.com; rewrite ^/(.*) http://www.jiage.com/$1 permanent; }
第二种方法: 利用if判断实现打破循环
if ($host ~* "^jiage.com$"){ rewrite ^/(.*) http://www.jiage.com/$1 permanent; }
例子: www.jiage.com/abc/index.html ^/ 表示的是www.jia.com (.*) 表示的是abc/index.html $host 表示的是用户访问的URL信息