栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

基础服务篇之Nginx配置文件详解与常用模块(二)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基础服务篇之Nginx配置文件详解与常用模块(二)

Nginx配置文件详解及常用模块 1.简介

不同方式安装nginx其配置文件路径也不相同,本次采用源码安装,其配置文件路径为/usr/local/nginx/conf目录下。yum安装配置文件默认在/etc/nginx下。通常用源码安装的配置文件一般为以下结构:

...
events {
		...
}
http {
		...
		server {
				....
			location / {
				root html;
				...
			}
		}
		server {
			....
			location / {
				root html;
				...
				}
			}
}

nginx配置指令可以分为两类:指令块和单个指令。指令块就想上面的events,http,server等;单个指令就像 root html;这样的,nginx规定指令块可以嵌套,如http块中可以嵌套server指令,server块中可以嵌套location指令,指令可以同时出现在不同的指令块,如root指令可以同时出现在http、server和location指令块,需要注意的是在location中定义的指令会覆盖server,http的指令。

2.配置文件详解
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf

#user  nobody;		##指定nginx的工作进程的用户及用户组,默认是nobody用户
worker_processes  1;	##指定工作进程的个数,默认是1个。具体可以根据服务器cpu数量进行设置,比如cpu有4个,可以设置为4。如果不知道cpu的数量,可以设置为auto。nginx会自动判断服务器的cpu个数,并设置相应的进程数

#error_log  logs/error.log;					##设置nginx的错误日志路径,并设置相应的输出级别。如果编译时没有指定编译调试模块,那么 info就是最详细的输出模式了。如果有编译debug模块,那么debug时最为详细的输出模式。这里设置为默认就好了
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;	##指定nginx进程pid的文件路径


events {
    worker_connections  1024;	##定义每个工作进程的最大连接数,默认是1024
}


http {
    include       mime.types;	##定义数据类型,如果用户请求lianxi.png,服务器上有lianxi.png这个文件,后缀名是png;根据mime.types,这个文件的数据类型应该是image/png;将Content-Type的值设置为image/png,然后发送给客户端
    default_type  application/octet-stream;	##设定默认类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置PHP环境时,Nginx是不予解析的,此时,用浏览器访问PHP文件就会变成下载

    #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;	##用于开启高效文件传输模式。直接将数据包封装在内核缓冲区,然后返给客户,将tcp_nopush和tcp_nodelay两个指令设置为on用于防止网络阻塞
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;	##	设置客户端连接保持活动的超时时间。在超过这个时间之后,服务器会关闭该连接

    #gzip  on;	##开启压缩功能,减少文件传输大小,节省带宽

    server {							##定义虚拟主机
        listen       80;	##设置监听端口,默认为80端口
        server_name  localhost;	##域名

        #charset koi8-r;	##定义字符集,防止乱码改为utf-8

        #access_log  logs/host.access.log  main;

        location / {					##定义请求匹配规则
            root   html;			##文件的存放目录
            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;	##定义访问错误返回的页面,凡是状态码是500 502 503 504 都会返回这个页面
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.常用模块的运用 1.access模块

access模块:访问控制模块 ,该模块可以实现简单的防火墙功能,过滤特定的主机。这个模块在我们编译nginx时会
默认编译进nginx的二进制文件中。

allow: 允许访问的IP或者网段。
deny: 拒绝访问的ip或者网段。

从语法上看,它允许配置在http指令块中,server指令块中还有locatio指令块中,这三者的作用域有所不同。
如果配置在http指令段中,将对所有server(虚拟主机)生效;配置在server指令段中,对当前虚拟主机生效;配置在location指令块中,对匹配到的目录生效。ps: 如果server指令块,location指令块没有配置限制指令,那么将会继承http的限制指令,但是一旦配置了会覆盖http的限制指令。或者说作用域小的配置会覆盖作用域大的配置。

[root@localhost ~]# echo "test access page" > /usr/local/nginx/html/index.html 
[root@localhost ~]# nginx -s reload

用另一台主机可以正常访问:

[root@node03 ~]# ip a show ens33
2: ens33:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:7b:67:d1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.202.130/24 brd 192.168.202.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d8aa:1f62:21f3:fbf3/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::7c25:2724:d542:aab8/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@node03 ~]# curl 192.168.202.132
test access page

更改主机192.168.202.132的配置文件

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

[root@localhost ~]# nginx -s reload

重新访问被拒绝

[root@node03 ~]# curl 192.168.202.132

403 Forbidden

403 Forbidden


nginx/1.20.2
[root@node03 ~]#
2.autoindex模块

Nginx默认是不允许列出整个目录的。如有需要,可以nginx的配置文件中,server或location 段里添加上autoindex on; 参数。

开启目录访问权限
autoindex on;
以人类易懂的方式显示文件大小
autoindex_exact_size off;
文件时间设置为本地时间
autoindex_localtime on;

注意:要想有目录产生,需要把index.html文件移除再修改配置后可访问到目录

[root@localhost ~]# mv /usr/local/nginx/html/index.html /tmp/

修改配置文件,写在location模块或server模块都可以
访问结果

3.auth_basic模块

用户认证模块,对访问目录进行加密,需要用户权限认证:
这个功能特性是由ngx_http_auth_basic_module提供的,默认编译nginx时会编译好。
认证的配置可以在http指令块,server指令块,location指令块配置。
auth_basic string :定义认证的字符串,会通过响应报文返给客户端,也可以通过这个指令关闭认证。auth_basic_user_file :定义认证文件。

对匹配目录进行加密:
安装httpd-tools工具后生成认证文件

[root@localhost ~]# yum install -y httpd-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: ftp.sjtu.edu.cn
base                                                                                                                                                | 3.6 kB  00:00:00     
extras                                                                                                                                              | 2.9 kB  00:00:00     
updates                                                                                                                                             | 2.9 kB  00:00:00     
Package httpd-tools-2.4.6-97.el7.centos.5.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# htpasswd -c /usr/local/nginx/conf/auth_passwd admin
New password: 	##输入密码
Re-type new password: 	##确认密码
Adding password for user admin

访问网页

4.stub_status 模块

状态查看模块 ,该模块可以输出nginx的基本状态信息,可配置在server,location模块指令中

查看模块是否编译

[root@localhost ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

修改配置文件
访问页面

[root@node03 ~]# curl 192.168.202.132/status
Active connections: 1 
server accepts handled requests
 22 22 49 
Reading: 0 Writing: 1 Waiting: 0 

Active connections:当前活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842132.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号