nginx 配置文件介绍,及配置状态监控,访问控制

nginx 配置文件介绍,及配置状态监控,访问控制

一:了解Nginx 主配置文件

1.1 全局配置

#user  nobody;            #运行用户,如果没有再编译时候指定,则默认为nobody
worker_processes  1;    #工作进程数。可以设置为 核数。 如果网站访问量不大,可以设置为1

#error_log  logs/error.log;     #错误体质文件位置
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;      #pid 文件的位置

1.2 I/O 配置

events {
    use epoll;    #使用epoll模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
		worker_connections  1024;    #每个进程处理 1024个连接,如果设置超过1024,需要改系统最大打开文件数
}

#如提高每个进程的连接数还需执行"ulimit -n 65535"命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux平台上, 在进行高并发TCP连接处理时, 最高的并发数量都要受到系统对用户 单—一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。

 #ulimit -a   显示当前所有的资源限制
 #ulimit -H    设置硬件资源限制
 #ulimit -S    设置软件资源限制
 #ulimit -n    设置进程最大打开文件描述符数
ulimit -n 65535 
ulimit  -a

image-20210813100828315


#永久修改最大连接数,在 文件 /etc/security/limits.conf 里配置

vim /etc/security/limits.conf 

image-20210813101624650


1.3 HTTP 配置

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;
    
    #支持文件发送
    sendfile        on;
    #允许或禁止socket的TCP_CORK的选项(发送数据包前先缓存),此项仅在使用sendfile 时候使用
    #tcp_nopush     on;
   
    #连接保持时间,单位时秒
    #keepalive_timeout  0;
    keepalive_timeout  65;

    # gzip模块设置,设置是否开启 gzip 压缩输出
    #gzip  on;
    

   #web 服务监听配置
    server {
         #监听地址及端口
        listen       80;
        #站点域名,可以有多个,使用空格隔开
        server_name  www.mynet.com;
         
        #设置网页字符集 
        charset utf-8;

        #access_log  logs/host.access.log  main;

       #根目录配置
        location / {
            #html 为相对路劲,起始点为nginx工作目录,/usr/local/nginx
            root   html;
            #默认首页文件名
            index  index.html index.php;
        }
        #内部错误的反馈页面
         error_page   500 502 503 504  /50x.html;
         #错误页面配置
         location = /50x.html {
            root   html;
        }
     }
 }

1.4 日志格式设定

日志格式设定∶

$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地址了,通过Sremote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

image-20210813112152249


1.5 localtion 常见配置指令

location常见配置指令, root、alias、proxy_ pass

root (根路径配置)∶ 请求www.mynet.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg

alias (别名配置)∶请求www.mynet.com/test/1.jpg会返回文件/usr/local/nginx/html/1.jpg

proxy_pass (反向代理配置)∶

proxy_pass http://127.0.0.1:8080/; ------------- 会转发请求到http∶//127.0.0.1∶8080/1.jpg

proxy_pass http://127.0.0.1:8080; --------------会转发请求到http∶//127.0.0.1∶8080/test/1.jpg

使用root 定义网页文件根目录

vim /usr/local/nginx/conf/nginx.conf
#在sever 项里添加一个location 项
location /test{
						#使用root配置网页文件根目录
            root /var/www/html;
        }

nginx -s reload
mkdir -p /var/www/html
mkdir /var/www/html/test
#网页文件放在  /var/www/html/test/目录下
echo "this is root test web" >> /var/www/html/test/a.html
curl http://192.168.23.103/test/a.html

image-20210814185450620


使用alias 定义网页文件根目录

cp -p conf/nginx.conf.default  conf/nginx.conf

vim /usr/local/nginx/conf/nginx.conf
#在sever 项里添加一个location 项
location /test{
						#使用alias配置网页文件根目录
            alias /var/www/html;
        }

nginx -s reload
#网页文件放在/var/www/html目录下
 echo "this is alias web" > /var/www/html/a.html  
curl http://192.168.23.103/test/a.html

image-20210814185827849


二: 访问状态统计


2.1 安装http_stub_status模块

2.1.1 查看是否包含http_stub_status模块

nginx -V  

image-20210814190515384

2.1.2 安装http_stub_status

如果没有安装则进行此操作

cd /opt/nginx-1.12.0/   #进入解压目录
 nginx -V            #查看nginx 已经安装了哪些模块,复制下来
  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
  -with-http_stub_status_module            #重新编译时候加上 --with-http_stub_status_module

make  #注意,这里只能make ,不可以 make install ,否则就覆盖了

nginx -s stop   #停止服务
netstat -natp | grep :80

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak   #备份原文件
cp ./objs/nginx /usr/local/nginx/sbin/    #替换nginx 二进制文件

nginx -t  #检查配置文件
nginx  #启动服务

nginx  -V  #再查看安装模块

image-20210814191739594


2.2 修改配置文件

cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak

vim /usr/local/nginx/conf/nginx.conf
#在server 项里添加
 location /status {              #访问位置为/status 
            stub_status on;      #打开状态统计功能
            access_log off;      #关闭此位置的日志记录。
        }

2.3 重载配置文件,并测试

nginx  -t
nginx  -s reload
firefox http://192.168.23.103/status
#可以使用 curl -s 命令访问,配合awk 和 if 语句进行性能监控

image-20210814193735988


三 基于授权的访问控制

3.1 生成用户密码认证文件

#使用命令 htpasswd 生成密码认证文件
#查看哪个软件包 提供了htpasswd 命令
yum provides htpasswd     
# 获取结果是由 httpd-tools-2.4.6-67.el7.centos.x86_64 提供

yum -y install httpd-tools
#第一次使用要加上 -c 创建密码文件, 后续不用
htpasswd -c /usr/local/nginx/paawd.db  zhangsan  
#修改属主为nginx 工作进程的启动用户
chown nginx  /usr/local/nginx/paawd.db    
#为了安全,修改权限为400,只有nginx和root用户可以读取
chmod 400 /usr/local/nginx/paawd.db 

image-20210814194847372


3.2 修改主配置文件相对应目录,添加认证配置项

vim /usr/local/nginx/conf/nginx.conf
   location / {
            ........
            #location 项中 添加认证配置
            auth_basic "hello";   #密码提示框文字
            auth_basic_user_file  /usr/local/nginx/paawd.db;  #认证文件路径
        }


3.3 重载配置,并 访问测试

nginx -t 
nginx -s reload
firefox http://192.168.23.103

image-20210814195814251


如果不输入,或者用户密码错误则显示

image-20210814195840920


四 基于客户端的访问控制

4.1 访问规则

访问控制规则如下:

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

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

• 规则从上往下执行,匹配即停止原则


4.2 添加控制规则

 vim /usr/local/nginx/conf/nginx.conf
      #在location 项中添加配置
        location / {
            ......         
            #添加控制规则
            deny 192.168.23.13;   #拒绝192.168.23.103主机访问
            allow all;            #允许其他所有访问
        }

image-20210814200452373

posted @ 2021-08-15 06:08  知己一语  阅读(498)  评论(0编辑  收藏  举报