安装编译参数
- 安装目的目录或路径
–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
- 执行对应模块时,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配置
- 实现单网卡多ip的绑定
除本机ip地址外,打开网络偏好设置,添加一个虚拟的ip地址。
$ ifconfig #查看本机IP地址
$ ping #查看ip地址是否能ping通(网卡是否添加成功)
- 更改nignx配置
- nginx.conf文件末尾,
include servers/*.conf;
表示在 /nginx/servers/ 文件夹中可以创建多个server的配置文件(.conf后缀),若servers文件夹不存在则自行创建。 - 各server的配置文件中,修改监听(listen)为不同ip的相同端口。
- 为不同server配置不同的测试界面:修改各server文件中的location参数,root参数对应测试页面html文件所在目录。
- 重新启动nginx,测试。例:http://192.168.xxx.17/server.html与http://192.168.xxx.18/server.html呈现不同的界面。
基于端口的虚拟主机配置
- 更改nginx配置(与单网卡多IP类似)
各server配置文件中,修改监听为不同端口。 - 重新启动nginx,测试。例:http://192.168.xxx.xxx:80/server.html与http://192.168.xxx.xxx:81/server.html呈现不同的界面。
基于host名称的虚拟主机配置(http协议)
- 在系统host文件($ sudo vi /etc/hosts)进行域名和ip对应关系的配置(添加例:192.168.xxx.xxx yuming.com)
- 修改各server配置的文件的server_name为相应域名
- 重新启动nginx,测试。例:http://1.yuming/server.html与http://2.yuming/server.html呈现不同的界面。
Nginx日志变量
error_log
记录错误信息,在配置文件error_log参数下有相应路径
access_log
- HTTP请求变量 - arg_PARAMETER、http_HEADER、sent_http_HEADER
- 内置变量 - Nginx内置
- 配置文件下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模块