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)
上一篇 2017-08-08 13:05
下一篇 2017-08-08 13:31

相关推荐

  • N25-第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell;   grep -E “^\(root|fedora|user1\)” /etc/passwd | cut -d: f7 2、找出/etc/rc.d/init.d/functions文件中某单词后面跟一组小括号的行,形如hello();  &nb…

    Linux干货 2017-02-15
  • linux命令格式和常用命令

    Linxu命令使用格式 ~]#COMMAND OPTIONS ARGUMENTS CMMMAND:发起一命令:请求内核将某个二进制程序运行为一个进程; OPTIONS:指定命令的运行特性; 短选项:-C, 例如-l, -d 注意:有些命令的选项没有-; 如果同一命令同时使用多个短选项,多数可合并:-l -d = -ld 长选项:–word, 例如…

    Linux干货 2018-03-04
  • shell脚本2——顺序选择语句

    流程控制      顺序执行      选择执行      循环执行 顺序执行:     条件选择:if语句 if语句为选择执行 注意:if语句可嵌套 单分支 if  判断条件:t…

    Linux干货 2016-08-18
  • 内核参数修改 内核编译 第14天

    Linux内核:单内核,模块化 内核的某些模块 编译进内核本体 [*] 编译成内核模块 [M] 不选择使用     [ ] 内核的组成部分 /boot/vmlinuz-VERSION /lib/modules/VERSION/ *.ko 模块间有可能有依赖关系 内核模块管理 lsmod:显…

    Linux干货 2016-01-18
  • 20160802作业

    20160802作业 1、每日课堂笔记总结 2、预习 3、每日课堂pdf练习 4、在/data/testdir里创建的新文件自动属于g1组,组g2的成员如: alice能对这些新文件有读写权限,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。 [root@Centos7 ~]# chmod…

    Linux干货 2016-08-04
  • linux发展历程简述

     linux发展历程简述 概述       本文写的linux 的发展历史,阅读本文你可以知道:           1.   linux的发音           2. &nbs…

    Linux干货 2016-10-14