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

相关推荐

  • 解决线上服务器httpd无法反向代理resin的解决方法

    一、简述   前两天有后端的小伙伴跟我反映说使用httpd反向代理resin时遇到的问题,在做网站管理后台的时候,发起的http的put,delete的请求无法经由resin传递到后面的dbserver。当时的第一反应就是httpd的反向代理设置出现的问题,但是关于httpd反代resin的内容在网上机会搜索不到。于是,查看httpd反代tomca…

    Linux干货 2017-01-11
  • 2018第六届中国网络安全大会即将于6月强势登陆!

    2018年6月13日,由赛可达实验室、国家计算机病毒应急处理中心、国家网络与信息系统安全产品质量监督检验中心、首都创新大联盟共同举办的第六届中国网络安全大会(NSC 2018)将在北京国家会议中心盛大召开。

    2018-03-15
  • 马哥教育网络班21期-第一周课程练习

    第一周课程练习 1、描述计算机的组成及其功能?         1.1 现代主流的计算机(冯·诺依曼体系)主要由五部分组成,分别是控制器、运算器、存储器、输入和输出设备。控制器和运算器对应我们现实计算机看到的CPU,存储器对应内存和硬盘等存储设备,输入设备对应鼠标键盘等其它外设,输出设备对应显示器、音响、打印机等其它外…

    Linux干货 2016-06-26
  • linux终端类型

           关键词:linxu终端类型,串行端口终端,伪终端,控制终端,控制台终端,虚拟终端        摘要:终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备。tty是Teletype的缩写。在Linux系统的设备特殊文件目录/dev/下,终端特殊…

    Linux干货 2016-10-19
  • DNS

    简介     DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串。通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析)。DNS协议运行在UDP协议之上,使用端口号5…

    Linux干货 2016-11-15
  • iptables基本知识

    1)Linux下的防火墙概念 a、一般谈到Linux下的防火墙,我们都会首先想到iptables,其实更确切的叫法应该是Netfilter/iptables,iptables和Netfilter其实是存在差别的。 b、尽管它们经常被用来相互替换使用,Netfilter是用来实现Linux内核中防火墙的Linux内核空间程序代码段,它要么被直接编译进内核,要么…

    Linux干货 2016-07-16