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

相关推荐

  • Linux的启动流程

    启动流程  POST: Power-On-Self-Test,加电自检,是BIOS功能的一个主要部分。负责完成对CPU、主板、内存、硬盘子系统、显示子系统、 串并行接口、键盘、 CD-ROM光驱等硬件情况的检测。 ROM: BIOS, Basic Input and Output System,保存着有关计算机系统最重要的基本输入输出程序,系统信息设置、 …

    Linux干货 2016-09-13
  • Linux系统启动流程

    对于linux系统的初学者来说,理解并掌握linux系统启动流程能够使你够深入的理解linux系统,还可以通过系统的启动过程来分析问题解决问题。   Linux系统的启动流程   关于linux系统的启动流程可以分为以下步骤:     POST(加电自检)–>加载BIOS(Basic Inp…

    Linux干货 2015-05-18
  • 文件系统层次标准FHS

    FHS针对目录树架构仅定义出三层目录下应该放置哪些数据,分别是下面三个目录: /(根目录):与开机系统有关; /usr(unix software resource):与软件安装执行有关; /var(variable):与系统运作过程有关。   下面分别对上述三层目录进行详细的阐述。   (1) /(根目录)   根目录是整个系统最重要的一个目录,…

    Linux干货 2016-10-19
  • http协议基础(一)

    web服务:      Apache      Nginx      LVS      http协议 网站运维:      web站点      游戏网站web服务 高性能,高可用…

    Linux干货 2017-04-18
  • Python之入门篇

    一、环境准备 环境准备 –> pyenv (安装python解释器,管理Python版本,管理python虚拟环境) https://github.com/pyenv/pyenv https://github.com/yyuu/pyenv-installer # yum -y install git # curl -L https://ra…

    Linux干货 2017-02-05
  • 简单shell脚本编程示例

    1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 脚本内容: 执行结果: 2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中 脚本内容: 执行结果: 3、编写脚本/roo…

    Linux干货 2016-08-15