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.www58058.com/84191

(0)
z longz long
上一篇 2017-08-08 13:05
下一篇 2017-08-08 13:31

相關推薦

  • keepalived的安裝和配置

    一、簡介 Keepalived是一個免費開源的,用C編寫的類似于layer3, 4 & 7交換機制軟件,具備我們平時說的第3層、第4層和第7層交換機的功能。主要提供loadbalancing(負載均衡)和 high-availability(高可用)功能,負載均衡實現需要依賴Linux的虛擬服務內核模塊(ipvs),而高可用是通過VRRP協議實現多臺…

    Linux干貨 2017-10-30
  • 馬哥教育網絡班22期+第6周課程練習

    請詳細總結vim編輯器的使用并完成以下練習題 1、vi的基本概念 基本上vi可以分為三種狀態,分別是命令模式(command mode)、插入模式(Insert mode)和底行模式(last line mode),各模式的功能區分如下: 1) 命令行模式command mode) 控制屏幕光標的移…

    Linux干貨 2016-09-26
  • 關于大型網站技術演進的思考(五):存儲的瓶頸(5)

    原文出處: 夏天的森林    上文里我遺留了兩個問題,一個問題是數據庫做了水平拆分以后,如果我們對主鍵的設計采取一種均勻分布的策略,那么它對于被水平拆分出的表后續的查詢操作將有何種影響,第二個問題就是水平拆分的擴容問題。這兩個問題在深入下去,本系列就越來越技術化了,可能最終很多朋友讀完后還是沒有找到解決實際問題的啟迪,而且我覺得…

    Linux干貨 2015-03-11
  • 配置yum服務器——以centOS 6.9系統為例

    準備工作 關閉防火墻  關閉防火墻service iptables stop  設置防火墻開機不啟動chkconfig iptables off  查看一下防火墻狀態 iptables -vnL 如下圖,可以看到已經關閉 關閉SElinux 使用命令 vim /etc/selinux/config 將SELINUX=enable…

    Linux干貨 2017-08-05
  • Linux基礎與命令解釋

    Linux基礎與命令 Linux起源     ? 1984 年:Richard Stallman 發起GNU 項目和自由軟件基金會 創建開源的UNIX 實用工具版本 創建通用公共許可證(GPL) ) 開源軟件許可實施原則 ? 1991 年:Linus Torvalds 發布Linux 創建開放源碼,類Unix 的內核,在GPL 下發布 下…

    Linux干貨 2017-03-17
  • Linux運維學習歷程-第三天-初識Linux

    初識Linux 本章內容    初安裝Linus的網絡配置    防火墻的關閉    用戶    終端    shell    命令紀要 安裝linux之后,linux默認網卡開機不是自動激活,并且防火墻開啟的,這對初期我們學習linux會…

    Linux干貨 2016-08-03
欧美性久久久久