1125笔记

1、状态监控

# 创建虚拟主机文件,查看status功能

[root@web-8 /etc/nginx/conf.d]#cat status.conf
# 作用就是让你访问 ip:9999可以精确定位到这个虚拟主机
server{
  listen 9999;
  server_name _;
stub_status on;
access_log off; # 因为它不是一个基于http请求响应的网站,仅仅是展示连接的信息,都不需要写location。
}


主配置文件include

vim /etc/nginx/nginx.conf 

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

重新加载 nginx -t nginx -s reload

 

浏览器测试访问

http://192.168.1.62:9999/

 

可以用ab命令多发一些请求测试

[root@c1 conf.d]# ab -c 100 -n 10000 http://192.168.1.62/

2、基于ip地址的访问限制

2.1 限制10网段请允许访问

先建一个虚拟网卡

ifconfig ens33:1 10.0.0.1 netmask 255.255.255.0

创建配置文件

cd /etc/nginx/conf.d/
vim deny-allow.conf
server {
listen 2233;
server_name _;
location / {
  allow 10.0.0.0/24;
  deny all;
      root /www/deny-allow;
      index index.html;
}
}

主配置文件include

/etc/nginx/nginx.conf

    include /etc/nginx/conf.d/deny-allow.conf;
}

创建网页目录,和网页文件

mkdir -p /www/deny-allow
cat > /www/deny-allow/index.html << EOF
<!DOCTYPE html>
<html>
<head>
<title>deny-allow</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to deny all.............allow 10</h1>
</body>
</html>
EOF

重新加载nginx

nginx -t

nginx -s reload

测试访问

通过win的浏览器访问,能打开吗,打不开。。因为网段是172

通过命令行访问

curl 172.16.5.51:2233 # 打不开,从172网卡出去的

curl 10.0.0.1:2233 #可以,因为是从10网卡出去的

2.2 允许172,拒绝所有

就把上边的配置稍微改一下

server {

listen 2233;
server_name _;

location / {
  #allow 10.0.0.0/24;
  allow 172.16.5.0/24;
  deny all;
      root /www/deny-allow;
      index index.html;
}

}

2.3 允许具体IP访问,拒绝其他所有

server {

listen 2233;
server_name _;

location / {
  #allow 10.0.0.0/24;
  allow 172.16.5.1;
  deny all;
      root /www/deny-allow;
      index index.html;
}

}

 

2.4 拒绝10.0.0.0/24网段

server {
listen 2233;
server_name _;
location / {
  #allow 10.0.0.0/24;
  #allow 172.16.5.51;
  deny 10.0.0.0/24;
      root /www/deny-allow;
      index index.html;
}
}

2.5 拒绝具体一个IP

server {
listen 2233;
server_name _;
location / {
  #allow 10.0.0.0/24;
  #allow 172.16.5.51;
  deny 172.16.5.1;
      root /www/deny-allow;
      index index.html;
}
}

3、基于用户名密码的验证

安装密码工具,和创建用户名密码

1. 创建密码,密码不是一个简单的纯文本文件,得基于密码数据库的存储
有工具帮你完成htpasswd这个工具去创建密码文件
yum -y install httpd-tools

创建密码文件
# -b 免交互,输入账号密码即可
# -c 设置密码写入到什么文件
只有第一个用户用-c,之后不需要了。
htpasswd -b -c /etc/nginx/auth_passwd hk 123456
htpasswd -b /etc/nginx/auth_passwd hk1 123456
htpasswd -b /etc/nginx/auth_passwd hk2 123456

创建虚拟主机配置文件

vim auth_basic.conf 

server {
      listen 172.16.5.51:3344;
      charset utf-8;
      server_name _;
      location / {
          auth_basic "please input your account password";
      auth_basic_user_file /etc/nginx/auth_passwd;
              root /www/auth-html;
              index index.html;
      }
}

mkdir /www/auth-html

vim /www/auth-html/index.html

<h1>这是密码验证。。。。</h1>

nginx主文件include

    include /etc/nginx/conf.d/auth_basic.conf;

nginx -t

nginx -s reload

 

4.nginx开启echo

清理服务器,卸载原yum安装的nginx

yum remove nginx -y

安装之前,准备好编译环境。

yum install pcre pcre-devel openssl openssl-devel  zlib zlib-devel gzip  gcc gcc-c++ make wget httpd-tools vim -y

 

# 下载echo模块的源代码
# 通过个git命令,去下载github代码仓库中的源码目录

yum install git -y #

cd /opt/
# 由于某些不可描述的因素,代码可能下载不了。。
# 去windows中下好了,发给linux

git clone https://github.com/openresty/echo-nginx-module.git
我的下载地址
https://codeload.github.com/openresty/echo-nginx-module/zip/refs/heads/master

 

mv echo-nginx-module-master /opt/

 

下载安装nginx

http://nginx.org/download/

cd /data/install/
wget http://nginx.org/download/nginx-1.20.2.tar.gz

 

创建www用户

useradd www

cd /data/install/
tar zxvf nginx-1.20.2.tar.gz
./configure --user=www --group=www --prefix=/data/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --add-module=/opt/echo-nginx-module-master
make && make install

验证,并加入环境变量

vim /etc/profile

export NGINX_HOME=/data/server/nginx
export MYSQL_HOME=/data/server/mysql-57
export SCRIPT_HOME=/data/script
export NODE_HOME=/data/server/node-v16.17.0-linux-x64
export PATH=$NODE_HOME/bin:$MYSQL_HOME/bin:$NODE_HOME/bin:$SCRIPT_HOME:$JAVA_HOME/bin:$PATH
#export JAVA_HOME=/usr/local/jdk-11.0.17
export JAVA_HOME=/usr/local/jdk1.7.0_80
export PATH=$NGINX_HOME/sbin:$SCRIPT_HOME:$NGINX_HOME/sbin:$JAVA_HOME/bin:$MYSQL_HOME/bin:$PATH

 

source /etc/profile
[root@c1 ~]# nginx -v
nginx version: nginx/1.20.2
[root@c1 ~]# 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: --user=www --group=www --prefix=/data/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --add-module=/opt/echo-nginx-module-master

 

 

#### by 2023-11-25
user www;
worker_processes auto;
#worker_processes 2;
#worker_cpu_affinity 01 10;

error_log logs/error.log notice;
pid       logs/nginx.pid;


events {
  worker_connections 1024;
}


http {
  include       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 logs/access.log main;
  #access_log off;
  sendfile       on;
  #tcp_nopush     on;

  keepalive_timeout 65;

  #gzip on;

  include vhost/*.conf;
}

cd /data/server/nginx/conf

mkdir vhost

#############
server {
  listen       11444;
  server_name localhost;
  charset utf-8;
  location / {
      echo "hello 80 welcome you!";
      echo $uri;
      echo $document_uri;
      echo $remote_addr;
      echo $remote_port;
      echo $http_user_agent;
  }
}

 

测试

[root@template ~]# curl 172.16.5.51:11444/haha
hello 80 welcome you!
/haha
/haha
172.16.5.50
34148
curl/7.29.0

5、location

优先级
匹配符匹配规则优先级
= 定义 URI 和位置的精确匹配。 1
^~ 以某个字符串开头,不检查正则(,区分大小写) 2
~ 区分大小写的正则匹配 (认识正则,区分url大小写) 3
~* 不区分大小写的正则匹配(认识正则,不区分url大小写) 4

 

 

 
     

验证优先级

vim location-1.conf 
# 配置文件如下
server {
  listen 22333;
  server_name _;

  # 最低级匹配,不符合其他locaiton就来这
  # 属于通用url规则
  location / {
      return 200 "location / \n";
  }


  # 优先级最高,等于号后面可以指定url
  location = / {
      return 200 "location = / \n";
  }


  #以/documents/开头的url,来这里,如符合其他locaiton,则以其他优先
  location /documents/ {
      return 200 "location /documents/ \n";
  }
  #匹配任何以.gif结尾的请求,支持正则
  location ~* \.(gif|jpg|jpeg)$ {
      return 200 "location ~* \.(gif|jpg|jpeg) \n";
  }

  #匹配任何以/images/开头的请求,不匹配正则
  location ^~ /images/ {
      return 200 "location ^~ /images/ \n";
  }


  access_log off;

}

 

验证

[root@c1 vhost]# curl 172.16.5.51:22333/fdsfdsfds
location /  
[root@c1 vhost]# curl 172.16.5.51:22333/images
location /  
[root@c1 vhost]# curl 172.16.5.51:22333/images/
location ^~ /images/  
[root@c1 vhost]# curl 172.16.5.51:22333/images
location /  
[root@c1 vhost]# curl 172.16.5.51:22333/images/fdsfdsfds
location ^~ /images/  
[root@c1 vhost]# curl 172.16.5.51:22333/images/ha.jpg
location ^~ /images/  
[root@c1 vhost]# curl 172.16.5.51:22333/imagesdfds/ha.jpg
location ~* \.(gif|jpg|jpeg)

 

root和alias

server {


listen 33555;
server_name _;

# 你现在需要设置
# 需要访问 172.16.5.51:33555/static/xx.jpg
# 你有什么写法,可以返回这个数据,给用户看到呢?
# 当前有一个代码目录,叫做 /huya/ 要求静态数据放在这个目录下
# 已知有一个静态图片,放在如下的目录中 /huya/static/caixukun.jpg
# 要求,要进行静态请求匹配,匹配/static/ url开头
# 到这还看懂扣1 不懂 3
# 等于匹配用户访问的url形式是 10.0.0.8:33555/static/xx.jpg
location ^~ /static/ {
# 两种写法第一个,写root,root特点是会将该url(/static)填充到网页根目录下,认为它也是一个目录
# 测一测是否能让你访问到 xx.jpg

# root /huya/static/; # 错误写法
root /huya/;

# 第二种写法,alias别名用法
# alias /huya/static/;

}

}

 

 

 

6、rewrite

7 、return

 

cat return.conf 
server {

  listen 22666;
  server_name _;
  root html;

  # 精确匹配,客户端只访问了网页根目录
  location = / {
      echo "welcome to haha linux course.";
  }

  location /test-return {

          # 客户端完全匹配
          if ($http_user_agent = huawei){
            return 200 "agent is $http_user_agent \n";
          }

          # 限制必须是GET方法
          if ($request_method != GET){

            return 405 "必须是GET方法!其他方法不允许\n";
          }

          # 如果是IE浏览器,就重定向
          if ($http_user_agent ~* IE){
            return 301 http://hello.com/xx.jpeg;
          }

                      # 没有if条件匹配到
          return 404 "sorry, nothing ....\n";
    }

      # 默认匹配,如果没有匹配到任意内容,跳转到首页 jd.com就是这个做法
      location / {
    return 301 http://hello.com/xx.jpeg;
      }

      location /ji {
          return 500 "鸡你太美\n";
      }

}

8、set

server {

  listen 22777;
  server_name _;
  root html;
   
  set $my_url http://hello.com/xx.jpeg;

  location /test-set {
      return 301 $my_url;
  }

}

 

9、break

server {

      listen 22888;
      server_name _;
      root html;

      location / {

              set $my_website http://hello.com/xx.jpeg;
              echo "welcome to my website:" $my_website;
              break;

              set $my_name yuchao;
              echo "my name is" $my_name;

      }

}
posted @ 2023-11-29 21:13  yuyongqi  阅读(58)  评论(0)    收藏  举报