nginx基础模块

目录:

nginx基础模块:

1.ngx_http_access_module模块:
2.ngx_http_auth_basic_module模块
3.ngx_http_stub_status_module模块
4.ngx_http_log_module模块
5.ngx_http_gzip_module:
6.ngx_http_ssl_module模块:
7.ngx_http_rewrite_module模块:
8.ngx_http_proxy_module模块:
9.ngx_http_headers_module模块
10.ngx_http_fastcgi_module模块:
11.ngx_http_upstream_module模块
12.ngx_stream_core_module模块

1、ngx_http_access_module模块:

    实现基于ip的访问控制功能
        allow address | CIDR | unix: | all;
        deny address | CIDR | unix: | all;

2、ngx_http_auth_basic_module模块:

实现基于用户的访问控制,使用basic机制进行用户认证;
示例:
    #先用htpasswd生成basic文件
    location /admin/ {
        auth_basic string | off;
        auth_basic “Admin Area”;
        auth_basic_user_file /etc/nginx/.ngxpasswd;
    }
注意:htpasswd命令由httpd-tools所提供;

3、ngx_http_stub_status_module模块:

用于输出nginx的基本状态信息;
示例:
    location /basic_status {
        stub_status;
    }

4、ngx_http_log_module模块:

示例:
    access_log /var/log/admin_access.log main;
    access_log off;

5、ngx_http_gzip_module:

示例:
    gzip on; #开启压缩
    gzip_comp_level 6; #压缩等级
    gzip_min_length 64; #最小长度压缩
    gzip_proxied any;
    gzip_types text/xml text/css application/javascript; #压缩哪种类型


6、ngx_http_ssl_module模块:

示例:
    server {
        listen 443 ssl;
        server_name www.magedu.com;
        root /vhosts/ssl/htdocs;
        ssl on; #开启ssl
        ssl_certificate /etc/nginx/ssl/nginx.crt; #证书存放位置
        ssl_certificate_key /etc/nginx/ssl/nginx.key; #证书key的存放位置
       ssl_session_cache shared:sslcache:20m; #保持会话
    }


7、ngx_http_rewrite_module模块:

示例:
    rewrite /(.*)\.png$ http://www.ilinux.io/$1.jpg; #将所有以png结尾的请求改写成jpg
    rewrite /(.*)$ https://www.ilinux.io; #将所有请求改写成https
    last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环;
    break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环;
    redirect:重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头;
    permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;

8、ngx_http_proxy_module模块:

1.proxy_pass URL;
示例:
    server {
    listen 80;
    server_name www.ilinux.io;
    location / {
        proxy http://172.16.42.1/;
    }
    location ~*\.(jpg)$ {
        proxy http://172.16.42.1;
    }
    }


2.proxy_cache缓存

示例:
    #指明缓存位置,存储大小,key的类型,等级,缓存时长;定义在http{…}中;
    proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
    #可以定义在location,server,http中
    proxy_cache pxycache; #启动缓存
    proxy_cache_key $request_uri; #key的路径
    proxy_cache_valid 200 302 301 1h; #缓存时长
    proxy_cache_valid any 1m;
    proxy_cache_methods GET HEAD; #缓存哪种请求
3.proxy_connect_timeout请求超时
示例:
    proxy_connect_timeout time; #连接超时
    proxy_read_timeout time; #接受请求超时
    proxy_send_timeout time; #发送请求超时

9、ngx_http_headers_module模块

1.设定发送给客户端自定义首部;
示例:
    add_header X-Via $server_addr; #自身IP地址
    add_header X-Accel $server_name;
2.设定发往后端主机的请求报文的请求首部的值;
示例:
    proxy_set_header X-Real-IP $remote_addr; #客户端IP地址
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host; #自身hostname

10、ngx_http_fastcgi_module模块:

1.反代php-fpm
示例:
    location ~* \.php$ {
        root /usr/share/nginx/html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        include fastcgi_params;
    }
2.fastcgi缓存
示例:
    http {
        …
        fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
        …
        server {
        …
        location ~* \.php$ {
        …
            fastcgi_cache fcgi;
            fastcgi_cache_key $request_uri;
            fastcgi_cache_valid 200 302 10m;
            fastcgi_cache_valid 301 1h;
            fastcgi_cache_valid any 1m;
            …
        }
            …
        }
            …
        }

11、ngx_http_upstream_module模块(七层调度)

示例:
1.upstream name { … }
定义后端服务器组,会引入一个新的上下文;Context: http

upstream httpdsrvs {

    server …
    server…
    #hash $request_uri consistent; #调度方法,hash调度以路径为key,同一路径访问,在同一个服务器.
    #least_conn; #最少连接调度算法,当server拥有不同的权重时其为wlc;
    #ip_hash; #源地址hash调度方法;
    hash $remote_addr;
    …
    }
    server{
    proxy_pass http://httpdsrvs;
    }

12、ngx_stream_core_module模块(四层调度)

示例:
    1.stream { … }
        定义stream相关的服务;Context:main
        stream {
        upstream sshsrvs {
        server 192.168.22.2:22;
        server 192.168.22.3:22;
        least_conn;
        }
        server {
        listen 10.1.0.6:22022;
        proxy_pass sshsrvs;
        }
        }

原创文章,作者:z long,如若转载,请注明出处:http://www.178linux.com/84191

(0)
z longz long
上一篇 2017-08-08
下一篇 2017-08-08

相关推荐

  • 8月2日作业

            1、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@localhost test…

    Linux干货 2016-08-05
  • yum命令用法及源码的编译

     一:yum 何为yum? yum是rpm的前端程序,可解决软件包相关依赖性,可在多个库之间定位软件包,是up2date 的替代工具。 1.命令格式:     yum [options] [command] [package …] 2.yum客户端配置文件: 公共配置文件:/etc/yum.conf   &…

    Linux干货 2017-08-19
  • M20 – 1- 第二周(1):课堂练习与作业

    课堂练习:  1、字符终端登录时,显示当前登录终端号,主机名和当前时间 [root@centos6 ~]# tty             #显示当前终端号 /dev/pts/0 [root@cent…

    Linux干货 2016-08-02
  • MBR分区结构

    MBR分区结构 一、硬盘主引导记录MBR由4个部分组成: 1、主引导程序(偏移地址0000H–0088H),它负责从活动分区中装载,并运行系统引导程序。 2、出错信息数据区,偏移地址0089H–00E1H为出错信息,00E2H–01BDH全为0字节。 3、分区表(DPT,Disk Partition Table):含4个分…

    2017-08-20
  • 用户权限过滤练习(w4)

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限。 [root@keyou ~]# cp -r /etc/skel/ /home/tuser1 | chmod -R g-rwx,o-rw…

    系统运维 2016-11-20
  • Tomcat基础及常见使用入门

    Tomcat的简单介绍,安装,以及简单的配置运用

    2017-09-11