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

相关推荐

  • N26 第二周

    本周学习了: linux的目录层级结构、常见文件类型、bash基本特性、文件的基本管理命令、globbing文件通配符、用户管理命令 开始作业! 1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。    文件管理命令有:ls,cp,mv,pwd,mkdir,rmdir,touch  …

    Linux干货 2017-01-12
  • N22-第五周作业

    1、显示当前系统上root、fedora或user1用户的默认shell;  egrep "^(root|fedora|user1)" /etc/passwd|awk -F: '{printf "%-15s:%-s\n",$1,$7}' 2…

    Linux干货 2016-09-15
  • 学习宣言

    不积跬步无以至千里,不积小流无以成江海。 在Linux的世界,我还只是一个顽童,我会通过我的努力证明,我可以攀登峻峰,笑看风云!

    Linux干货 2016-12-28
  • ansible学习笔记

    简介:  在日常服务器维护中,从系统安装到程序部署再到发布应用,在大规模的生产环境中,如果需要手动的每台服务器进行安装配置将会给运维人员带来许多繁琐而又重复的工作。这就促使了在每个运维层次中出现了不同的自动化运维工具。 常见的自动化运维工具分类有以下几类:  系统安装运维工具(OS Provisioning):    …

    Linux干货 2015-08-17
  • Linux基础目录命名的法则及规定功能

    Filesystem Hierarchy Standard (FHS):重点在于规范每个特定的目录下应该要放置什么样子的数据。 这样做好处是,在Linux操作系统就能够在既有的面貌下(目录架构不变)发展出开发者想要的独特风格。 /bin:系统有很多放置执行文件的目录,但/bin比较特殊。因为/bin放置的是在单人维护模式下还能够被操作的指令。 在/bin底下…

    Linux干货 2017-07-02
  • MySQL系列之一键安装脚本—-单实例/多实例

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nolinux.blog.51cto.com/4824967/1440414       最近在搞MySQL,由于经常测试一些东西。因此,就写了一个一键安装脚本。 脚本用途: 用于在…

    Linux干货 2016-08-15