nnn
第1章 nginx特性
第2章 安装nginix
第3章 nginx配置
第4章 练习 验证server标签的优先级,测试结果是谁在上面谁优先
4.1 nginx主配置文件
[root@m01 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf;
4.2 站点code目录有以下文件
[root@m01 code]# ll
total 0
drwxr-xr-x 2 root root 24 Dec 19 00:18 server1
drwxr-xr-x 2 root root 24 Dec 19 00:25 server2
drwxr-xr-x 2 root root 24 Dec 19 00:50 server3
[root@m01 code]# tree ./*
./server1
└── index.html
./server2
└── index.html
./server3
└── index.html
4.3 站点目录下有文件 index.html.分别是以下内容
[root@m01 code]# cat /code/server*/index.html
server1
server2
server3
4.4 nginx.conf配置文件
[root@m01 conf]# cat conf.d/*.conf
server {
listen 80;
server_name server1;
location / {
root /code/server1;
index index.html;
}
}
server {
listen 80;
server_name server2 ;
location / {
root /code/server2;
index index.html;
}
}
server {
listen 80;
server_name server3 ;
location / {
root /code/server3;
index index.html;
}
}
4.5 测试结果
[root@m01 conf]# curl server1
server1
[root@m01 conf]# curl server2
server2
[root@m01 conf]# curl server3
server3
第5章 设置默认域名 curl 命令默认域名第一显示
5.1 编写.conf配置文件
[root@m01 conf]# cat conf.d/*.conf
server {
listen 80;
server_name server1;
location / {
root /code/server1;
index index.html;
}
}
server {
listen 80;
server_name server2;
location / {
root /code/server2;
index index.html;
}
}
server {
listen 80 default_server;
server_name server3;
location / {
root /code/server3;
index index.html;
}
}
5.2 测试结果 添加defautl_server 标签的访问浏览器输入IP地 优先 访问域名的顺序不变
[root@m01 conf]# curl server1
server1
[root@m01 conf]# curl server2
server2
[root@m01 conf]# curl server3
server3
[root@m01 conf]# curl 10.0.0.61
server3
第6章 root和alias
6.1 测试
6.1.1 创建目录,写站点文件
mkdir /local_path/code/request_path/code/ -p
echo "Root" > /local_path/code/request_path/code/index.html
6.1.2 nginx server文件
[root@m01 conf.d]# cat root.conf
server{
listen 80;
server_name root.oldboy.com;
location /request_path/code/ {
root /local_path/code/;
index index.html;
}
}
请求location里的/request_path/code/ 把站点目录指到/local_path/code/;
6.1.3 windows hosts里解析10.0.0.61 root.oldboy.com alias.oldboy.com
6.1.3.1 windows ping域名检查
6.1.4 测试结果
第7章 alias
7.1 写站点目录文件
echo "Alias" > /local_path/code/index.html
7.2 写server配置文件
[root@m01 conf.d]# cat alias.conf
server{
listen 80;
server_name alias.oldboy.com;
index index.html;
location /request_path/code/ {
alias /local_path/code/;
}
}
7.3 windows hosts解析
7.3.1 ping alias.oldoby.com 检查连通性
7.4 测试结果
同样访问的一个资源.显示的内容不同
第8章 location配置
第9章 nginx status监控
[root@m01 conf]# cat conf.d/status.conf
server {
listen 80;
server_name root.oldboy.com;
location /nginx_status {
stub_status;
}
}
9.1 测试结果
9.2 字段解释
第10章 nginx 网页下载站点
模块
10.1 配置文件编写
[root@m01 centos6]# cat /application/nginx/conf/conf.d/status.conf
server {
listen 80;
server_name root.oldboy.com;
root /code;
location /nginx_status {
stub_status;
}
location / {
autoindex on;
autoindex_exact_size off; # off后显示kb,MB G
autoindex_localtime on; # on显示 文件时间为服务器时间
#charset utf-8,gbk;
}
}
第11章 访问控制Module ngx_stream_access_module
The rules are checked in sequence until the first match is found. In this example, access is allowed only for IPv4 networks 10.1.1.0/16 and 192.168.1.0/24 excluding the address 192.168.1.1, and for IPv6 network 2001:0db8::/32.
11.1 Directives 允许访问语法
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: stream, server
Allows access for the specified network or address. If the special value unix: is specified, allows access for all UNIX-domain sockets.
11.2 拒绝访问语法
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: stream, server
11.3 示例
拒绝某个ip 其他全部允许
设置 windown 浏览器 不能访问 nginx_status模块. 地址是10.0.0.1
11.3.1 写配置文件
[root@m01 ~]# cat /application/nginx/conf/conf.d/access.conf
server {
listen 80;
server_name access.oldboy.com;
root /code;
index index.html;
location /nginx_status {
stub_status;
deny 10.0.0.1; #拒绝 10.0.0.1
allow all; #允许 其他
}
}
11.3.2 设置首页文件
[root@m01 code]# echo "Access..." > /code/index.html
11.3.3 windows 解析 access.oldboy.com域名
11.3.4 测试结果
访问首页文件可用显示 Acc.....
访问首页下的nginx_statu路径 报错403
11.3.5 由此可见 屏蔽了10.0.0.1IP地址
11.4 允许所有人访问,拒绝10.0.0.1
allow 10.0.0.1 要放在上面,拒绝的放在下面, 否则没用,
[root@m01 ~]# cat /application/nginx/conf/conf.d/access.conf
server {
listen 80;
server_name access.oldboy.com;
root /code;
index index.html;
location /nginx_status {
stub_status;
allow 10.0.0.1;
deny all;
}
}
第12章 用户输入密码登陆
Module ngx_http_auth_basic_module
12.1 官方示例 Example Configuration
location / {
auth_basic "closed site"; #标注提示信息
auth_basic_user_file conf/htpasswd; #密码放在哪
}
12.2 Directives
Syntax: auth_basic string | off;
Default: auth_basic off; 关闭这个功能
Context: http, server, location, limit_except
Enables validation of user name and password using the “HTTP Basic Authentication” protocol. The specified parameter is used as a realm. Parameter value can contain variables (1.3.10, 1.2.7). The special value off allows cancelling the effect of the auth_basic directive inherited from the previous configuration level.
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
12.3 密码写法
Specifies a file that keeps user names and passwords, in the following format:
comment
name1:password1
name2:password2:comment
name3:password3
12.4 安装依赖组件
yum -y install httpd-tools
创建用户是sb 密码是sb123 放到 conf下面
[root@m01 ~]# htpasswd -b -c /application/nginx/conf/pass_file sb sb123
写配置文件
[root@m01 conf.d]# cat status.conf
server {
listen 80;
server_name root.oldboy.com;
root /code;
location /nginx_status {
stub_status;
}
location / {
autoindex on;
autoindex_exact_size off; #off后显示kb,MB G
autoindex_localtime on; #on显示 文件时间为服务器时间
charset utf-8,gbk;
auth_basic "输入密码"; #标注提示信息
auth_basic_user_file /application/nginx/conf/pass_file;
}
}
12.4.1 站点目录文件有以下内容
[root@m01 code]# ll
drwxr-xr-x 11 root root 284 Dec 19 23:47 centos6
-rw-r--r-- 1 root root 10 Dec 20 00:57 index.html.sss
drwxr-xr-x 2 root root 58 Dec 19 23:29 logs
drwxr-xr-x 2 root root 24 Dec 19 01:00 server1
drwxr-xr-x 2 root root 24 Dec 19 00:25 server2
drwxr-xr-x 2 root root 24 Dec 19 00:50 server3
[root@m01 code]# pwd
/code
12.5 访问,输入密码后进入下载页面
第13章 nginx 对外访问限制
13.1 官方语法
Example Configuration
http {
limit_conn_zone $binary_remote_addr zone=addr:10m;
KEY ZONE=名字 开辟多大内存
server {
location /download/ {
limit_conn addr 1; # location 调用区域名称
}
13.2 编写配置文件
[root@m01 conf.d]# cat limit_con.conf
limit_conn_zone $binary_remote_addr zone=conn_zone:100m;
server{
listen 80;
server_name conn.oldboy.com;
root /code;
index index.html;
location / {
limit_conn conn_zone 1;
}
}
13.3 windows 做解析
13.4 windows ping 连通性
13.5 linux测试
找一台机器把域名解析到/etc/hots
echo "10.0.0.61 access.oldboy.com" >> /etc/hosts
13.5.1.1 压测
[root@db01 conf.d]# ab -n 10000 -c 20 http://conn.oldboy.com/
13.5.1.2 服务器查看nginx error日志
[root@m01 conf.d]# tail ../../logs/error.log
2018/12/20 12:51:43 [error] 5642#0: *962 limiting connections by zone "conn_zone", c
lient: 10.0.0.51, server: conn.oldboy.com, request: "GET / HTTP/1.0", host: "conn.oldboy.com"2018/12/20 1
13.5.2 查看正确日志
503 被拒绝
[root@m01 ~]# tail -f /application/nginx/logs/access.log
10.0.0.51 - - [20/Dec/2018:12:51:43 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench
/2.3" "-"10.0.0.51 - - [20/Dec/2018:12:51:43 +0800] "GET / HTTP/1.0" 503 213 "-" "ApacheBench
第14章 nginx请求限制
14.1 Module ngx_http_limit_req_modu
14.2 官方语法
Example Configuration
Directives
limit_req
limit_req_log_level
limit_req_status
limit_req_zone
The ngx_http_limit_req_module module (0.7.21) is used to limit the request processing rate per a defined key, in particular, the processing rate of requests coming from a single IP address. The limitation is done using the “leaky bucket” method.
Example Configuration
http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
#每1秒中访问只能是1次请求,多1次都不行
server {
location /search/ {
limit_req zone=one burst=5; #在location里应用这个规则
}
14.3 编写配置文件
[root@m01 conf.d]# cat limit_req.conf
[root@m01 conf.d]# cat limit_req.conf
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
server{
listen 80;
server_name req.oldboy.com;
root /code;
index index.html;
location / {
limit_req zone=one;
}
}
14.4 windows 解析域名
14.5 浏览器测试访问
1秒超过1次访问就报错503
14.6 另一台服务器测压
[root@db01 ~]# ab -n 20 -c 20 http://req.oldboy.com/
Time taken for tests: 0.008 seconds
Complete requests: 20
Failed requests: 19
14.7 10.0.0.61 查看错误日志
[root@m01 conf.d]# tail -f ../../logs/error.log
出现请求error limting requertes
2018/12/20 13:23:54 [error] 5642#0: *1013 limiting requests, excess: 0.995 by zone "
one", client: 10.0.0.51, server: req.oldboy.com, request: "GET / HTTP/1.0", host: "req.oldboy.com"
14.7.1 添加
burest 延时, burst=3请求3次,超过3次就拒绝
server{
listen 80;
server_name req.oldboy.com;
root /code;
index index.html;
location / {
limit_req zone=one burst=3 nodelay;
}
}
14.8 压测
另一台服务器测压req.oldboy.com
[root@db01 ~]# ab -n 20 -c 20 http://req.oldboy.com/
压测结果 请求20个失败16个,因为配置文件里
Server Hostname: req.oldboy.com
Server Port: 80
Document Path: /
Document Length: 10 byte
Concurrency Level: 20
Time taken for tests: 0.010 seconds
Complete requests: 20 请求20个
Failed requests: 16 失败16个
web服务器查看error.log
[root@m01 ~]# tail -1 /application/nginx/logs/error.log
2018/12/20 13:29:20 [error] 5799#0: *1077 limiting requests, excess: 3.992 by zone "one", client: 10.0.0.51, server: req.oldboy.com, request: "GET / HTTP/1.0", host: "req.o
ldboy.com"
14.9 结论
第15章 nginx日志
日志放在server层,每个网站,都有自己的日志
15.1 日志格式
15.2 日志字段含义
10.0.0.1 '$remote_addr IP地址
- sb - $remote_user 用户名
[20/Dec/2018:14:07:57 +0800] [$time_local] 时间
"GET / HTTP/1.1" $request 请求头
200 $status 状态码
772 $status $body_bytes_sent 返回数据的大小
"-" $http_referer 防盗链
"Mozilla/5.0 (Windows NT 10 $http_user_agent 浏览器
"-" 用户通过代理访问,获取到的用户真实ip
15.3 开启日志开关
access_log{server/location}2个位置
15.4 定义日志格式
在http层
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_time';
在server里引用
server{
access_log log/hostname.log main;
}
比如
server {
listen 80;
server_name basic.oldboy.com;
root /code;
index index.html;
access_log logs/basic.oldboy.log main;
location / {
stub_status;
auth_basic "Od";
auth_basic_user_file /application/nginx/conf/pass1_file;
}
}
默认所有server日志都存放在access.log里
access_log日志指令
15.5 日志文件缓存设置
15.6 错误日志
看状态码
403: 一般是目录权限
第16章 ningx虚拟机
虚拟机就是1台服务器上配置多个网站
16.1 基于域名的虚拟主机(域名必须能解析)
公司都是这个
16.2 基于端口的虚拟机,
公司内部由于内部没用dns,适用于公司内部,域名是假的还得在windows上做解析,
nginx 配置文件
[root@m01 conf.d]# cat port_8080.conf
server {
listen 8080;
root /code;
index 8080.index.html;
access_log logs/8080.log main;
}
[root@m01 conf.d]# cat port_8081.conf
server {
listen 8081;
root /code;
index 8081.index.html;
access_log logs/8081.log main;
}
站点目录配置文件
[root@m01 code]# ll
total 16
-rw-r--r-- 1 root root 12 Dec 21 01:19 8080.index.html
-rw-r--r-- 1 root root 12 Dec 21 01:10 8081.index.html
[root@m01 code]# cat 8081.index.html
Access 8081
[root@m01 code]# cat 8080.index.html
Access 8080
第17章 虚拟主机别名
17.1 配置
第18章 多server_name优先级
第19章 location
19.1.1 第一次访问
第20章 http
第21章 lnmp
21.1 nginx连接php
[root@web01 /application/nginx/conf/conf.d]# cat docs.conf
server {
server_name docs.oldboy.com;
listen 80;
root /code;
index index.php index.html;
location / {
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000; # 请求被location匹配到 ,通过fastcgi_pass(php服务端)交给本地9000端口
fastcgi_index index.php; # 默认的php文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
21.1.1 测试结果
21.2 2 测试php与mysql的连接(使用浏览器测试是否连接成功)
注意:php版本
php5.6版本测试命令是mysql_connect
$conn = mysql_connect($servername, $username, $password)
php7版本测试命令是mysqli_conncet
$conn = mysqli_connect($servername, $username, $password)
测试时候选则对应的命令否则报错
[root@web01 /application/nginx/conf/conf.d]# tail -f ../../logs/error.log
2018/12/23 22:55:05 [error] 7040#0: *13 FastCGI sent in stderr: "PHP message: PHP Fatal error: Call to undefined function mysqli_connect() in /code/mysql.php on line 7" wh
ile reading response header from upstream, client: 10.0.0.1, server: docs.oldboy.com, request: "GET /mysql.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "docs.oldboy.com"
21.2.1 测试php连接nginx代码
[root@web02 ~]# cat /code/wordpress/mysql.php
21.2.2 测试结果
第22章 数据库迁移
22.1 准备一台新的服务器,并安装好数据库服务。
# 1.软件包是mysql.tar.gz (群里下载)
tar xf mysql.tar.gz
cd mysql/
yum localinstall -y *.rpm #安装本地rpm包,自动解决依赖关系。
#2.使用官方的yum进行安装(网络畅通情况下,优先选择)
yum install -y mysql-community-server
2.启动数据库,并初始化密码
[root@db01 mysql]# systemctl start mysqld
[root@db01 mysql]# systemctl enable mysqld
3.查找临时初始化后的登录密码
[root@db01 mysql]# grep 'password' /var/log/mysqld.log
2018-08-09T01:42:16.161719Z 1 [Note] A temporary password is generated for root@localhost: EmXrJBS92
4.登录mysql数据库修改密码
mysql> alter user root@'localhost' identified by 'Bgx123.com';
Query OK, 0 rows affected (0.00 sec)
22.2 旧的数据进行导出,然后打包
#1.备份操作(Bgx123.com是数据库密码)
[root@web02 ~]# mysqldump -uroot -p'Bgx123.com' --databases edusoho wordpress zh --single-transaction > date +%F%H-mysql-all.sql
3.货拉拉 --> 传输--> 新服务器。
[root@web02 zh]# scp 2018-08-0909-mysql-all.sql root@10.0.0.51:~
2018-08-0909-mysql-all.sql 100% 3823KB 37.0MB/s 00:00
4.新服务器解包,导入,权限变更。
# 给新服务器进行数据导入
[root@db01 ~]# mysql -uroot -p'Bgx123.com' < 2018-08-0909-mysql-all.sql
#登录数据库
[root@db01 ~]# mysql -uroot -pBgx123.com
# 查看数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| edusoho |
| mysql |
| performance_schema |
| sys |
| wordpress |
| zh |
+--------------------+
5.关闭旧数据库上的数据库服务
[root@web02 zh]# systemctl stop mysqld
6.在新服务器的数据库上开启一个账户,允许远程连接访问。
mysql> grant all privileges on . to 'all'@'%' identified by 'Bgx123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)
22.3 修改产品连接数据库
1修改wordpress的数据库连接信息
[root@web02 ~]# cd /code/wordpress
[root@web02 wordpress]# vim wp-config.php
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'all');
/** MySQL数据库密码 */
define('DB_PASSWORD', 'Bgx123.com');
/** MySQL主机 */
define('DB_HOST', '172.16.1.51');
2.修改edu
[root@web02 code]# cat edu/app/config/parameters.yml
parameters:
database_driver: pdo_mysql
database_host: 172.16.1.51
database_port: 3306
database_name: edusoho
database_user: all
database_password: 'Bgx123.com'
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
清除缓存
[root@web02 code]# rm -fr edu/app/cache/*
3.修改zg连接数据库
[root@web02 config]# cat /code/zh/system/config/database.php

浙公网安备 33010602011771号