目錄:
nginx基礎模塊:
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; #開啟sslssl_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結尾的請求改寫成jpgrewrite /(.*)$ https://www.ilinux.io; #將所有請求改寫成httpslast:重寫完成后停止對當前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:mainstream {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