今日内容

1、了解web服务
2、部署Nginx
    Nginx和Apache的对比
3、Nginx虚拟主机
    - 基于多IP的方式
    - 基于多端口的方式
    - 基于多域名的方式
4、日志配置
5、Nginx访问控制模块
6、Nginx访问认证模块
7、Nginx状态监控模块

一、什么是web服务

web就是B/S架构

二、web服务器软件

1、apache
网络模型
    select
    poll
    epoll

2、Nginx

官网:https://nginx.org/
软件:https://nginx.org/download/

三、部署Nginx

1、yum安装
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx

2、编译安装
[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 nginx-1.20.2]# ./configure
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install

四、平滑增加Nginx模块

增加模块必须重新编译。
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web01 ~]# cd nginx-1.20.2
[root@web01 nginx-1.20.2]# ./configure  --with-http_ssl_module
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install

五、Nginx的命令

1、-v : 打印版本号
[root@web01 ~]# nginx -v
nginx version: nginx/1.20.2

2、-V :打印版本号和配置项
[root@web01 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx

3、-t :检查配置文件
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、-T :测试配置文件并启动

5、-q :打印错误日志

6、-s :操作进程
    stop	:停止
    quit	:退出
    reopen	:重启
    reload	:重载
7、-p :指定nginx的工作目录
8、-e :指定错误日志路径
9、-c :指定配置文件的路径
10、-g :设置一个全局的Nginx配置项
[root@web01 ~]# nginx -g 'daemon off;'

六、Nginx配置文件

全局配置和模块配置

1、全局配置
    1、user : 指定Nginx的启动用户
    2、worker_processes : 定义Nginx的worker进程数
	auto === CPU数量
    3、error_log : 错误日志路径
    4、pid : pid的存放文件路径
    5、events : 模块配置
	5.1、worker_connections :每一个worker进程最多同时接入多少个请求
	5.2、use : 指定Nginx的网络模型
    6、http : web服务的模块
	6.1、include : 加载外部的配置项
	6.2、default_type : 如果找不到文件的类型,则按照指定默认类型处理
	6.3、log_format : 定义日志格式
		log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
		access_log /var/log/nginx/access.log json ;
	6.4、sendfile : 高效读取文件
	6.5、keepalive_timeout : 长连接保持连接的
		HTTP 1.0 短链接
		HTTP 1.1 长连接
	6.6、server : 网址模块
	    6.6.1、listen : 监听的端口
	    6.6.2、server_name : 定义域名
	    6.6.3、location : 访问路径
		6.6.3.1、root : 指定网址路径
		6.6.3.2、index : 指定网址的索引文件

七、超级玛丽和象棋

1、上传代码

2、编辑配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3、测试配置文件是否正常
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、重启Nginx
[root@web01 conf.d]# systemctl restart nginx

5、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com

八、Nginx虚拟机

1.基于多IP的方式
[root@web01 conf.d]# cat game2.conf
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
    root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 80;
    server_name 172.16.1.7;
    location / {
        root /opt/tank;
        index index.html;
    }
}

2、基于多端口的方式
[root@web01 conf.d]# cat game3.conf
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 81;
    server_name 192.168.15.7;
    location / {
        root /opt/tank;
        index index.html;
    }
}

3、基于多域名的方式
[root@web01 conf.d]# cat game4.conf 
server {
    listen 80;
    server_name www.game.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}
server {
    listen 80;
    server_name www.game1.com;
    location / {
        root /opt/tank;
        index index.html;
    }
}

九、Nginx日志

应用:
网站状态码是500的比例
网站的访问来源
网站排错

nginx日志位置: /var/log/nginx/,有access.log(访问日志)和error.log(错误日志)

$remote_addr		客户端地址
$remote_user		客户端用户名称
[$time_local]		访问时间和时区
$request		请求的url和http协议
$status			http请求状态
$body_bytes_sent	发送给客户端的文件内容的大小
$http_referer		url跳转来源 (客户端ip)
$http_user_agent	用户终端浏览器等信息
$http_x_forwarded_for	真实的客户端ip(在反向代理中生效)

十、Nginx访问控制模块

  • ngx_http_access_module
允许或者拒绝某些IP访问
deny	:拒绝
allow	:允许

案例1:允许192.168.15.1访问,不允许其他IP访问
    allow 192.168.15.1;
    deny all;

案例2:允许192.168.15.0这个网段访问,不允许其他网段访问
    allow 192.168.15.0/24;
    deny all;

案例3:只允许通过VPN来访问
   allow 172.16.1.81;
   deny all;
  • ngx_http_auth_basic_module
访问之前需要登录

1、安装httpd-tools
[root@web01 ~]# yum install httpd-tools -y

2、生成用户名密码文件
[root@web01 ~]# htpasswd -c /etc/nginx/auth chenyang
New password:
Re-type new password:
Adding password for user chenyang

3、将文件路径加入Nginx配置
[root@web01 ~]# vim /etc/nginx/conf.d/game4.conf
auth_basic "Welcome To Login";
auth_basic_user_file /etc/nginx/auth;

4、重启Nginx
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
  • ngx_http_autoindex_module
展示目录索引。
autoindex off | on;				启用或禁用目录列表输出
autoindex_exact_size on | off;			指定是否应在目录列表中输出确切的文件大小(四舍五入)
autoindex_localtime off | on;			指定目录列表中的时间是否应以本地时区或UTC输出
autoindex_format html | xml | json | jsonp;	设置目录列表的格式

十一、Nginx状态监控模块

监控Nginx运行状态。
[root@web01 conf.d]# cat game5.conf 
server {
    listen 80;
    server_name 192.168.15.7;
    location / {
        stub_status;
    }
}

十二、访问连接控制模块

1、控制Nginx连接数

    1、安装ab测试命令
	yum install httpd-tools -y 

    2、ab 参数
	-n : 总共需要访问多少次
	-c : 每次访问多少个

[root@web01 conf.d]# cat game5.conf 
# limit_req_zone $remote_addr zone=one:10m rate=1r/s;
limit_conn_zone $remote_addr zone=addr:10m;
server {
    listen 80;
    server_name 192.168.15.7;
    # limit_req zone=one burst=5;
    limit_conn addr 1;
    location / {
        root /opt/Super_Marie;
	index index.html;
    }
}

2、控制Nginx访问量

    1、连接池
	limit_req_zone $remote_addr zone=one:10m rate=1r/s;
	声明连接池       变量          名称  连接池的大小  速率
    2、限制数

案例1:要求每秒只能有一个访问。
[root@web01 conf.d]# cat game5.conf
limit_req_zone $remote_addr zone=one:10m rate=1r/s;
server {
    listen 80;
    server_name 192.168.15.7;
    limit_req zone=one burst=5;
    location / {
        root /opt/Super_Marie;
    index index.html;
    }
}
posted on 2021-12-31 17:06  幽星  阅读(69)  评论(1编辑  收藏  举报