欢迎来到 Kong Xiangqun 的博客

01-Nginx基础知识

一、什么是Nginx

# nginx 是一个 web服务器 ( 静态资源 )  代理服务器...

二、Nginx应用场景

"""
1.web服务器
2.反向代理 代理服务 ( PHP Python Golang Java )
    2.1) 负载均衡
    2.2) 缓存
        3.安全服务https
"""

三、Nginx组成结构

"""
二进制文件:         Nginx主体框架能实现基本的请求与响应功能 ( 源码 + 模块 = 编译 )汽车的基本框架,提供驾驶功能
nginx配置文件:      控制Nginx的行为,能做什么不能做什么,控制汽车前往的目的地
access_log日志:    记录每一条用户的http 请求,GPS记录行动轨迹
error_log日志:     服务无法运行,或出现异常时,可以通过error_log定位故障,黑匣子,分析故障,和定位故障
"""

1、配置官方yum源

epel源获取,使用过程中就会出现权限不足的问题

2、执行命令安装

[root@kkk-pythonedu ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@kkk-pythonedu ~]# yum install nginx -y
[root@kkk-pythonedu ~]# rpm -q nginx
nginx-1.16.1-1.el7.x86_64

3、安装后的目录结构

[root@kkk-pythonedu ~]# rpm -ql nginx
/etc/logrotate.d/nginx            # 日志轮转 ( 日志切割 )
/etc/nginx/                        # nginx配置文件目录
/etc/nginx/nginx.conf            # nginx主配置文件
/var/log/nginx                    # 日志目录 

4、Nginx配置文件

[root@kkk-pythonedu ~]# systemctl stop httpd
[root@kkk-pythonedu ~]# systemctl disable httpd
[root@kkk-pythonedu ~]# systemctl start nginx
[root@kkk-pythonedu ~]# netstat -lntp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26551/nginx: master 

cat /etc/nginx/nginx.conf

user nginx;                                         # 运行nginx的用户身份
worker_processes auto;                              # worker进程运行多少个, auto自动与cpu核心保持一致
error_log /var/log/nginx/error.log;                 # 错误日志
pid /run/nginx.pid;                                 # 进程运行后,在该目录下存放一个pid文件,文件中记录的是该进程的ID编号

include /usr/share/nginx/modules/*.conf;            # 包含所有的模块配置文件

events {
    worker_connections 1024;                        # worker的最大连接数   [  worker_connections *  worker_processes ]
}

    
http {            # 负责http的请求与响应
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;            # 存放的都是nginx能支持的文件类型
    default_type        application/octet-stream;        # 当 nginx 不支持该类型时,默认以下载的方式回传给用户

    include /etc/nginx/conf.d/*.conf;                    # 包含conf.d/*.conf结尾的文件    [ 定义的一个又一个的网站 ]
}

第一步: 关闭防火墙  

  firewalld
    systemctl stop firewalld
    systemctl disable firewalld
  selinux
    setenforce 0
    sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

清除nginx.conf中所有的注释,以及server整个段的内容

第二步:新建一个站点

# [root@kkk-pythonedu ~]# cat /etc/nginx/conf.d/test.kkk.com.conf
server {
    listen 80;
    server_name test.kkk.com;

    location / {
        root /code/html;
        index index.html;
    }
}

根据配置文件定义的路径,创建该路径,并放入代码

[root@kkk-pythonedu ~]# mkdir /code/html -p 
[root@kkk-pythonedu ~]# echo "test-kkk.com....." > /code/html/index.html

检查配置文件是否有错误的地方

[root@kkk-pythonedu ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重载服务,并生效

[root@kkk-pythonedu ~]# systemctl restart nginx    

配置Hosts解析  ( 假的域名  )

Windows:  C:\Windows\System32\drivers\etc
            10.0.0.201 test.kkkedu.com

MacOS:    /etc/hosts
            10.0.0.201 test.kkk.com

5、服务器在新增一个游戏的站点

nginx.conf 主配置文件一般不动

[root@kkk-pythonedu ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}

第一步、编辑nginx配置文件

[root@kkk-pythonedu ~]# cat /etc/nginx/conf.d/game.kkkedu.com.conf

 server {
   listen 80; # 监听的端口
   server_name game.kkkedu.com; # 申明域名

   location / { # 匹配用户请求的uri路径
   root /code/html; # 告诉 nginx 站点的代码在哪里目录下
   index index.html index.htm; # 定义 默认返回的主页面
 }

第二步、根据配置,创建目录,上传代码

[root@kkk-pythonedu ~]# mkdir /code/game -p
[root@kkk-pythonedu ~]# cd /code/game/
[root@kkk-pythonedu game]# rz
[root@kkk-pythonedu game]# unzip html5_\(1\).zip 

第三步、检查nginx配置文件语法,然后重载服务

[root@kkk-pythonedu game]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@kkk-pythonedu game]# systemctl restart nginx

第四步、配置Hosts解析(假的)

Windows电脑没有Hosts文件,  新建出来.

Windows:  C:\Windows\System32\drivers\etc
                10.0.0.200 test.kkkedu.com
                10.0.0.200 game.kkkedu.com

5、Nginx整个流程

"""
1.用户通过浏览器请求game.kkkedu.com   
2.浏览器会添加一些协议头信息,携带上默认的请求uri,  http://game.kkkedu.com/
3.浏览器会发起DNS解析,解析game.kkkedu.com  对应的真实IP地址   
4.浏览器获取到真实的IP地址后,  通过  IP+Prot的方式请求应用程序Nginx
5.Nginx接受到请求后,会进行事件的处理, 将用户请求的 Request 信息中 包含的 Host 字段, 与 Server_name 字段进行匹配
    5.1) 如果匹配不成功,则会随机返回一个站点的页面给用户.
    5.2) 可以通过 在 listen 80 default_server; 方式来定义具体默认返回哪个站点给用户.
    5.3) 如果碰到非法的域名,可以拒绝,也可以做跳转.
            拒绝:
                return 500;
            跳转:
                return 302 https://www.jd.com;
6.如果匹配成功, 接下来进行location uri地址匹配, 获取要返回的文件所在的路径 + 默认返回的页面 
7.Nginx获取磁盘上的文件, 然后构建响应报文,回传给浏览器,浏览器加载并且渲染最终的结果呈现给用户.
"""

 四、Nginx基础模块

1、目录索引 auto_index

能实现和阿里云mirrors.aliyun.com  一样的效果.

[root@kkk-pythonedu ~]# cat /etc/nginx/conf.d/mirror.kkkedu.com.conf
server {
    listen 80;
    server_name mirror.kkkedu.com;
    charset utf8;

    location / {
        root /code/mirror;
        autoindex on;                    # 开启目录索引功能
        autoindex_exact_size off;        # 显示文件具体大小
        autoindex_localtime on;          # 显示本地服务器时间 
    }
}
[root@kkk-pythonedu ~]# nginx -t
[root@kkk-pythonedu ~]# systemctl restart nginx

[root@kkk-pythonedu ~]# mkdir -p /code/mirror
[root@kkk-pythonedu ~]# cd /code/mirror 
[root@kkk-pythonedu ~]# rz

1.1、访问限制

给予来源IP限制

"""
1) 仅允许 10.0.0.1 访问,其他访问全部拒绝

server {
    ...
    allow 10.0.0.1/32;
    deny all;
    ...
}

2) 拒绝10.0.0.1 访问, 其他全部允许

server {
    ...
    deny 10.0.0.1/32;
    allow all;
    ...
}
"""

测试的curl命令:
[root@kkk-pythonedu mirror]# curl -HHost:mirror.kkkedu.com http://10.0.0.200/

 

1.2、基于用户与密码 auth_basic_module

[root@kkk-pythonedu ~]# yum install httpd-tools -y
[root@kkk-pythonedu nginx]# htpasswd -c -b password_file kkk 123
[root@kkk-pythonedu nginx]# cat password_file 
kkk:$apr1$7dYbXvco$LSJaBM3HqlK3k1kkRt2Ya.

nginx配置文件:

server {
    listen 80;
    server_name mirror.kkkedu.com;
    charset utf8;

    auth_basic "hello,nginx";                # 描述信息
    auth_basic_user_file password_file;        # 定义密码文件名称

    location / {
        root /code/mirror;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
}

1.3、登录后work需要密码 、public无需密码

[root@kkk-pythonedu ~]# mkdir /basic/{work,public} -p
[root@kkk-pythonedu mirror]# cat /etc/nginx/conf.d/basic.kkkedu.com.conf 
server {
    listen 80;
    server_name basic.kkkedu.com;
    root /basic;
    autoindex on;

    location / {
    }

    location /work {
        auth_basic "test_work";
        auth_basic_user_file password_file;
    }
}
[root@kkk-pythonedu mirror]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@kkk-pythonedu mirror]# systemctl restart nginx

点击public

点击work

1.4、限制链接 limit_conn

场景: 下载

[root@kkk-pythonedu mirror]# cat  /etc/nginx/conf.d/mirror.kkkedu.com.conf 
limit_conn_zone $binary_remote_addr zone=addr:10m;            # 定义限制的key, 分配区域大小
server {
    listen 80;
    server_name mirror.kkkedu.com;
    charset utf8;
    limit_conn addr 1;            # 调用区域限制,限制key只可以出现1次, 相当于限制来源客户端IP的连接数为1
    limit_conn_status 500;        # 限制成功后,会返回500的错误状态码,默认返回503
    
    limit_rate_after 200m;        # 全速下载200m资源
    limit_rate       300k;        # 达到200m以后,限制300k的速度

    error_page 500 = @testerror;    # 如果 出现500错误,则让其跳转到内部的 @testerror 
    
    location @testerror {            # 定义 @testerror, 返回具体的动作 
        default_type text/html;
        return 200 '$remote_addr 你超过了最大连接限制, 请充值VIP解封!';
    }
    location / {
        root /code/mirror;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
}

1.5、状态监控 stub_status

location = /status {
    stub_status;
}
"""
Active connections: 2                 
server accepts handled requests
 74 74 104 
Reading: 0 Writing: 1 Waiting: 1 


Active connections: # 活跃的连接数
accepts:             # 接受的总TCP连接数
handled:             # 总处理的TCP连接数
requests:             # 总的 http 请求数
"""

 

posted @ 2020-10-13 20:26  kongxiangqun20220317  阅读(159)  评论(0编辑  收藏  举报