目录

一、全局配置的六个模块简介

二、Nginx配置文件的详解

1)全局配置模块

 2)I/O 事件配置 

3)HTTP 配置 

4)web服务监听设置

5)其他设置

location常见配置指令:“root、alias、proxy_pass

对比: 

当设置  location /test{     },alias /var/www/html  和   root /var/www/html  有什么区别?

三、访问状态统计与控制

1)访问状态统计

①查看访问统计配置的相关模块 

②修改主配置文件,添加访问状态统计模块 

事例:用脚本一键查询并发量

2)基于授权的访问控制

①生成用户密码认证文件

②修改主配

 ③重启服务,进行访问测试

3)基于客户端的访问控制

四、Nginx的虚拟主机设置 

1)基于域名的虚拟主机

①域名准备和网页准备

②主配置文件的修改

③重启服务,访问测试 

2)基于IP 的 Nginx 虚拟主机

①设置虚拟主机IP 

②修改主配置文件

③重启服务,访问测试 

3)基于端口的 Nginx 虚拟主机

①修改主配置文件

②重启服务,测试访问测试 

一、全局配置的六个模块简介

全局块:全局配置,对全局生效 events块:配置影响 Nginx 服务器与用户的网络连接 http块:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置 server块:配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块 location块:用于配置匹配的 uri upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分

注意:location 匹配的内容来源是来自网页的URI,而不是URL(URL代表整个链接如:www.baidu.com/images/search,而URI则是/images/search。所以nginx的location匹配的是URI) 

二、Nginx配置文件的详解

1)全局配置模块

就是配置文件从头开始到 events 块之间的内容,主要设置的是影响nginx服务器整体运行的配置指令。比如 worker_process,值越大,可以支持的并发处理量也越多,但是还是和服务器的硬件相关

vim /usr/local/nginx/conf/nginx.conf

 2)I/O 事件配置 

#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数

永久修改的方式:

[root@localhost init.d]#vim /etc/security/limits.conf

注意:软硬件的事件处理都要设置才能生效,并且保存退出后,要重新连接查看才会生效

#在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。

#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。

#epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。(实现异步非阻塞)

3)HTTP 配置 

4)web服务监听设置

5)其他设置

日志格式设定:

$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址

$remote_user:用来记录客户端用户名称

$time_local: 用来记录访问时间与时区

$request: 用来记录请求的url与http协议

$status: 用来记录请求状态;成功是200

$body_bytes_sent :记录发送给客户端文件主体内容大小

$http_referer:用来记录从哪个页面链接访问过来的

$http_user_agent:记录客户浏览器的相关信息

通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址

location常见配置指令:“root、alias、proxy_pass

root(根路径配置):root /var/www/html

请求www.kgc.com/test/1.html,会返回文件/var/www/html/test/1.html

alias(别名配置):alias /var/www/html

请求www.yang.com/test/1.html,会返回文件/var/www/html/1.html

对比: 

当设置  location /test{     },alias /var/www/html  和   root /var/www/html  有什么区别?

alias是别名设置,将设置的网页放在/var/www/html下,访问root 是根目录设置 ,将设置的网页放在 /var/www/html/test 下,访问

proxy_pass(反向代理配置)

三、访问状态统计与控制

1)访问状态统计

①查看访问统计配置的相关模块 

cat /opt/nginx-1.22.0/auto/options | grep YES #可查看 nginx 已安装的所有模块

[root@localhost ~]#/usr/local/nginx/sbin/nginx -V

查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块

②修改主配置文件,添加访问状态统计模块 

#主配置备份,防止设置错误,无法还原

cd /usr/local/nginx/conf

cp nginx.conf nginx.conf.bak

修改主配配置操作:

vim /usr/local/nginx/conf/nginx.conf

server {

listen 80;

server_name www.yang.com;

charset utf-8;

#access_log logs/host.access.log main;

location / {

root html;

index index.html index.htm;

}

location /status {

stub_status on;

access_log off;

}

重启nginx服务,访问测试:

除此之外:还可以 curl -Ls http://192.168.73.105/status 结合 awk与if 语句进行性能监控

事例:用脚本一键查询并发量

要求:每10秒获取并发量大于2时,发送预警

[root@bogon ~]# vim a.sh

#!/bin/bash

while true

do

#筛选静态状态的第三部分

a=$(curl -Ls 192.168.231.102/status | awk '/Active connections/{print $3}')

if [ $a -gt 2 ];then

echo "警报!当前并发连续过高!当前并发数为:$a"

fi

sleep 10 #睡眠10秒

done

~

用另一台虚拟机登录访问,后运行脚本

#查看并发连接数的另一种方法

netstat/ss -natp | grep nginx | grep -c ESTABLISHED

2)基于授权的访问控制

①生成用户密码认证文件

yum install -y httpd-tools

htpasswd -c /usr/local/nginx/passwd.db zhangsan

chown nginx /usr/local/nginx/passwd.db

chmod 400 /usr/local/nginx/passwd.db

②修改主配

vim /usr/local/nginx/conf/nginx.conf

server {

location / {

......

##添加认证配置##

auth_basic "secret"; #设置密码提示框文字信息

auth_basic_user_file /usr/local/nginx/passwd.db;

}

}

 ③重启服务,进行访问测试

3)基于客户端的访问控制

设置方式类似于黑白名单

设置前的访问,其他主机访问测试:

访问控制规则如下:

deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问

allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问

规则从上往下执行,如匹配则停止,不再往下匹配

vim /usr/local/nginx/conf/nginx.conf

......

server {

location / {

......

##添加控制规则##

allow 192.168.73.105; #允许访问的客户端 IP

deny all; #拒绝其它IP客户端访问

}

}

设置后的访问测试:

四、Nginx的虚拟主机设置 

相比较Apache的虚拟主机设置,Nginx的设置是十分简便的只需要修改主配置中的相关配置就能实现虚拟主机的效果

1)基于域名的虚拟主机

①域名准备和网页准备

[root@localhost conf]#echo "192.168.73.105 www.test1.com www.test2.com" >> /etc/hosts

[root@localhost conf]#mkdir -p /var/www/html/test1

[root@localhost conf]#mkdir -p /var/www/html/test2

[root@localhost conf]#echo "

this is test1

" > /var/www/html/test1/index.html

[root@localhost conf]#echo "

this is test2

" > /var/www/html/test2/index.html

②主配置文件的修改

vim /usr/local/nginx/conf/nginx.conf

http {

......

server {

listen 80;

server_name www.test1.com;

charset utf-8;

access_log logs/www.test1.access.log;

location / {

root /var/www/html/test1;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name www.test2.com;

charset utf-8;

access_log logs/www.test2.access.log;

location / {

root /var/www/html/test2;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

..............

}

}

③重启服务,访问测试 

2)基于IP 的 Nginx 虚拟主机

①设置虚拟主机IP 

[root@localhost conf]#ifconfig ens33:0 192.168.73.200/24

[root@localhost conf]#ifconfig ens33:0

②修改主配置文件

vim /usr/local/nginx/conf/nginx.conf

......

http {

......

server {

listen 192.168.73.105:80;

server_name www.test1.com;

charset utf-8;

access_log logs/www.test1.access.log;

location / {

root /var/www/html/test1;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 192.168.73.200:80;

server_name www.test2.com;

charset utf-8;

access_log logs/www.test2.access.log;

location / {

root /var/www/html/test2;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

..........

}

③重启服务,访问测试 

3)基于端口的 Nginx 虚拟主机

①修改主配置文件

[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf

......

http {

......

server {

listen 192.168.73.105:666;

server_name www.test1.com;

charset utf-8;

access_log logs/www.test1.access.log;

location / {

root /var/www/html/test1;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 192.168.73.105:888;

server_name www.test2.com;

charset utf-8;

access_log logs/www.test2.access.log;

location / {

root /var/www/html/test2;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

..........

}

②重启服务,测试访问测试 

好文阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: