Nginx学习笔记(二)

安装编译参数
  1. 安装目的目录或路径

–prefix=/usr/local/Cellar/nginx/1.17.3_1
–sbin-path=/usr/local/Cellar/nginx/1.17.3_1/bin/nginx
–conf-path=/usr/local/etc/nginx/nginx.conf
–pid-path=/usr/local/var/run/nginx.pid
–lock-path=/usr/local/var/run/nginx.lock

  1. 执行对应模块时,Nginx所保留的临时性文件

–http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp
–http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp
–http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp
–http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp
–http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp

Nginx虚拟主机及其实现方式

单网卡多IP配置
  1. 实现单网卡多ip的绑定
    除本机ip地址外,打开网络偏好设置,添加一个虚拟的ip地址。
 $ ifconfig #查看本机IP地址
 $ ping #查看ip地址是否能ping通(网卡是否添加成功)
  1. 更改nignx配置
  • nginx.conf文件末尾,include servers/*.conf; 表示在 /nginx/servers/ 文件夹中可以创建多个server的配置文件(.conf后缀),若servers文件夹不存在则自行创建。
  • 各server的配置文件中,修改监听(listen)为不同ip的相同端口。
  • 为不同server配置不同的测试界面:修改各server文件中的location参数,root参数对应测试页面html文件所在目录。
  1. 重新启动nginx,测试。例:http://192.168.xxx.17/server.html与http://192.168.xxx.18/server.html呈现不同的界面。
基于端口的虚拟主机配置
  1. 更改nginx配置(与单网卡多IP类似)
    各server配置文件中,修改监听为不同端口。
  2. 重新启动nginx,测试。例:http://192.168.xxx.xxx:80/server.html与http://192.168.xxx.xxx:81/server.html呈现不同的界面。
基于host名称的虚拟主机配置(http协议)
  1. 在系统host文件($ sudo vi /etc/hosts)进行域名和ip对应关系的配置(添加例:192.168.xxx.xxx yuming.com)
  2. 修改各server配置的文件的server_name为相应域名
  3. 重新启动nginx,测试。例:http://1.yuming/server.html与http://2.yuming/server.html呈现不同的界面。

Nginx日志变量

error_log

记录错误信息,在配置文件error_log参数下有相应路径

access_log
  1. HTTP请求变量 - arg_PARAMETER、http_HEADER、sent_http_HEADER
  2. 内置变量 - Nginx内置
  3. 配置文件下access_log参数解释:
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

remote_addr:客户端地址
remote_user:客户端请求用户名
time_local:时间
request:request头的请求行
status:response返回状态
body_bytes_sent:返回body信息大小
http_referer:上一级页面信息

Nginx模块

sub_status
编译选项–with-http_stub_status_module
作用Nginx客户端状态
配置语法Syntax: stub_status;
Default: -
Context: server, location

在配置文件默认server下加入以下代码块

location /mystatus {
   stub_status;
       }

重新加载配置文件,浏览器中运行http://192.168.xxx.xxx/mystatus

Active connections: 2 	#当前活跃连接数
server accepts handled requests
6 6 5 	#握手数-连接数-请求数;正常情况下握手数 = 连接数,表明未丢失
Reading: 0 Writing: 1 Waiting: 1
random_index
编译选项–with-http_random_index_module
作用目录中选择随机主页
配置语法Syntax: random_index on/off ;
Default: random_index off;
Context: location

修改配置文件的location

location / {
	root	/不同html文件地址/;
	random_index on;
}

重新加载配置文件,浏览器中运行http://192.168.xxx.xxx,会随机出现不同的主页(隐藏文件除外)

sub_module
编译选项–with-http_sub_module
配置语法Syntax: sub_filter string replacement ;
Default: - ;
Context: http, server, location
作用http内容替换
配置语法Syntax: sub_filter_last_modified on/off ;
Default: sub_filter_last_modified off ;
Context: http, server, location
作用浏览器发送请求时校验服务器内容是否有变更,记录时间,判断是否更新;如果发生更新,返回给用户最新的内容,用于缓存
配置语法Syntax: sub_filter_once on/off ;
Default: sub_filter_once on ;
Context: http, server, location
作用匹配html中第一个(on)/所有(off)字符串

修改配置文件的location,将sub替换成SUB

location / {
	root	/html文件地址/;
	index index.html index.htm;
	sub_filter 'sub' 'SUB';
	sub_filter_once off; #不加则只替换第一个匹配的字符串
}

连接限制&请求限制

连接频率限制limit_conn_module
配置语法Syntax: limit_conn_zone key zone = name:size;
Default: - ;
Context: http
作用存储连接状态,key表示限制的内容(如:ip) ,name空间名,size空间大小
配置语法Syntax: limit_conn zone number;
Default: - ;
Context: http, server, location
作用zone为空间名,number并发的限制数量
请求频率限制limit_req_module
配置语法Syntax: limit_req_zone key zone = name:size rate = rate;
Default: - ;
Context: http
作用与连接限制类似,rate对于请求的限制多大,通常以秒为单位
配置语法Syntax: limit_req zone=name [burst=number] [nodelay];
Default: - ;
Context: http, server, location
作用与连接限制类似

配置文件

limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server{
	...
	location / {
		root	/html文件地址/;
		index index.html index.htm;
		#limit_conn conn_zone 1;
		#limit_req zone=req_zone burst=3 nodelay;
	}
}

访问控制

基于IP的访问控制http_access_module
配置语法Syntax: allow address/CIDR/unix:/all ;
Default: - ;
Context: http, server, location, limit_except
作用配置允许的地址/网段/Linux中Socket使用/所有
配置语法Syntax: deny address/CIDR/unix:/all ;
Default: - ;
Context: http, server, location, limit_except
作用配置不允许的地址/网段/Linux中Socket使用/所有

配置文件(allow与deny成对出现)

#只拒绝某ip
location ~ ^/admin.html {
	root /路径/;
	deny 168.192.xxx.xxx;
	allow all;
	index index.html index.htm;
}
#只允许某ip
location ~ ^/admin.html {
	root /路径/;
	allow 168.192.xxx.xxx;
	deny all;
	index index.html index.htm;
}

局限性:若Nginx(IP3)与用户(IP1)中存在一层代理(IP2),则通过remote_addr只能限制到代理IP2(直接建立连接的IP)的访问

解决方式:

  • 通过http_x_forwarded_for进行信息控制访问,可以查到所有的访问IP1和IP2
  • 结合geo模块作
  • 通过HTTP西定义变量传递
基于用户的信任登陆http_auth_basic_module
配置语法Syntax: auth_basic string/off;
Default: auth_basic off ;
Context: http, server, location, limit_except
作用string为前端的登录提示
配置语法Syntax: auth_basic_user_file file;
Default: - ;
Context: http, server, location, limit_except
作用file为文件路径,用于存储用户名密码信息

配置文件

location ~ ^/admin.html {
	root /路径/;
	auth_basic "Auth access test.";
	auth_basic_user_file /路径/;
	index index.html index.htm;
}

局限性:

  • 用户信息依赖文件方式
  • 操作管理机械,效率低下

解决方案

  • Nginx结合LUA实现高效验证
  • Nginx结合LDAP,利用nginx-auth-ldap模块
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值