0 安裝相關軟件:yum -y install pcre-devel zlib-devel openssl-devel 1 下載 nginx-1.14.0.tar.gz 2 安裝 tar xf nginx-1.14.0.tar.gz cd nginx-1.14.0/ ./config --help ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-debug 3 啓動: /usr/local/nginx/sbin/nginx 4 查看:ss -tlnp 查看啓動端口 ps aux 查看主進程和工做進程
nginx特性:支持epoll,aio,mmap
master/worker,支持平滑升級
vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin:$PATH . /etc/profile.d/nginx.sh nginx -t nginx -s reload nginx -h 查看命令用法
1 user USERNAME【GROUPNAME】;指定worker進程的用戶和組 user nginx nginx; 2 pid /path/to/pid_file #pid文件 3 worker_rlimit_nofile #; 一個工做(worker)進程打開的最大文件描述符數量 4 worker_rlimit_sigpending#; 每一個用戶可以發往worker進程的信號的數量
1 worker_processeshtml
1 worker_processes #; worker進程的個數,一般應該爲物理cpu核心數量減1;一個留給內核使用 能夠配置爲 worker_processes auto; 啓動後工做進程等於cpu的個數,低版本不支持 每一個worker進程所可以響應的最大併發請求數量: worker_process * worker_connections
2 work_cpu_affinitynode
2 work_cpu_affinity cpumask cpumask...; 綁定cpu cupmask: 0001 0010 0100 1000 worker_cpu_affinity 00000001 00000010 00000100 --->八顆cpu狀況
3worker_prioritynginx
3 worker_priority nice: #nice優先級
[-20,19] 默認爲0
4accept_mutexweb
4 accept_mutex[on|off];各worker接收用戶的請求的負載均衡鎖,
爲on時讓多個worker輪流的系列化的響應新請求;
5lock_file 正則表達式
5 lock_file /path/to/lock_file 鎖文件的位置
1daemon算法
1 daemon off|on
是否以守護方式啓動nginx;
2 master_processexpress
2 master_process on|off;
是否以master/worker模型運行nginx;
3 error_logvim
3 error_log /path/to/error_log level;
錯誤日誌文件以及級別;出於調式的須要,能夠設定爲debug;在編譯的時候要加上--with-debug,纔有效
1 server{}定義一個虛擬主機瀏覽器
server{ listen PORT; server_name NAME; root /path/to/documentroot; } 注意: 1:基於port: 監聽不一樣的端口 2:基於hostname server_name指令指向不一樣的主機名;
2 listen緩存
default_server:設置默認虛擬主機;用於基於IP地址, 或使用了任意不能對應於任何一個server的name時,所返回的站點 例如:listen 80 default_server; ssl:用於限制只能經過ssl鏈接提供服務 spdy:spdy protocol(speedy)編譯時必須加上spdy模塊 http2:http version 2;基於spdy開發的
3 server_name NAME[...];能夠同時指定n個servername,名稱能夠用通配符*和正則表達式(~)
匹配順序: 1 先作精確匹配:例如:www.example.com 2 左側通配符:例如:*.example.com 3右側通配符:例如:www.example.* 4 正則表達式,例如:~^.*\.example\.com$ 4 default_server
4 tcp_nodelay on|off; 對keepalive模式下的鏈接是否使用tcp_nodelay選項,默認不啓用
5 tcp_nopush on|off; 是否啓用tcp_nopush(freebse) 或tcp_cork(Linux);僅在sendfile爲on時有用;默認off
6 sendfile on |off ;直接在內核空間中響應報文,要爲on
7 root 設置web資源的路徑映射;用於指明請求的url所對應的文檔的目錄路徑
server{ ... root /data/www/vhost1;例如:http://www.example.com/images/logo.jpg-->/data/www/vhost1/images/logo.jpg } server{ ... location /images/{ root /data/imgs/; //例如:http://www.example.com/images/logo.jpg-->/data/imgs/images/logo.jpg } }
8 location[=|~|~*|^~]url{...} 容許用戶請求的uri來匹配定義的各location
location @name{...} =:URI的精確匹配 ~:作正則表達式匹配,區分字符大小寫 ~*:作正則表達式匹配,不區分字符大小寫 ^~:URI的左半部分匹配,不區分字符大小寫 匹配優先級順序:精確匹配 ,^~ ,~或~* ,不帶符號的URL;
9 alias 別名配置,定義路徑別名
只能用於location配置段 location /images/{ root /data/imgs/; } location /images/{ alias /data/imgs/; } 注意: root指令:路徑爲對應的location的「/」這個URL; /images/test.jpg -->/data/imgs/images/test.jpg alias指令:路徑爲對應的location的「/url/」這個URL; /images/test.jpg -->/data/imgs/test.jpg
10 index 通常指明首頁文件,還能夠帶變量,好比說不一樣語言的首頁,index file ...
11 error_page code ... 自定義錯誤頁面,根據http的狀態碼重定向錯誤頁面
error_page 404 /404.html;
error_page 404 =200 /404.html; 注意root路徑,其中200就是指定相應碼
12 try_files
try_files file... url; try_files file... =code; 嘗試查找第1到第N-1個文件,第一個即爲返回給請求者的資源:若是1到N-1文件都不存在,則跳轉到最後一個uri; 這個uri由其餘location定義,避免形成死循環 例如:注意下面的連個location root /data/imgs/; index index.html; location /images/{ try_files index.html /images/test1.html /images/test2.html /images/test3.html //最後一個必須存在 } location=/images/test3.html{ expires 10s; }
13 keepalive_timeout;設定keepalive鏈接的超時時長,0表示禁止長鏈接;默認75秒
14 keepalive_requests number;在keepalived鏈接上所容許請求的最大資源鏈接數量;默認爲100;
15 keepalive_disable指明禁止何種瀏覽器使用keepalive功能
16 send_timeout;發送相應報文的超時時長,默認爲60s; 精確的說時兩次寫操做之間的時長
17 client_body_buffer_size接收客戶端請求報文body的緩衝區大小,默認爲16K(32位系統爲8k);超出此指定大小時,其將被移存到磁盤上,官方文檔說是寫在temporary file,臨時文件上
18 client_body_temp_path path [level1 [level2 [level3]]]; 設定用於存儲客戶端請求body的臨時文件存放的地方以及數量
client_body_temp_path /var/tmp/client_body 2 2;
19 limit_except METHOD{...} 對指定範圍以外的其餘方法進行訪問控制
limit_except GET{ allow 172.16.0.0/16; deny all; }
20 limit_rate speed;表示客戶端每秒能傳輸的字節數,默認爲0表示無限制
21 aio on|off; 是否啓用異步io模式
22 directio size|off
23 open_file_cache off
open_file_cache max=N [inactive=time] nginx能夠緩存如下三種信息: (1)文件描述符,文件大小和最近一次的修改時間 (2)打開的目錄結構; (3)沒有找到的或者沒有權限操做的文件的相關信息; max=N 表示可緩存的最大條目上限,一旦達到上限,則會使用LRU算法從緩存中刪除最近最少使用的緩存項 inactive=time:設置多長時間內沒有被訪問過的緩存項爲非活動緩存項,所以直接刪除;
24 open_file_cache_errors on|off;
是否緩存找不到其路徑的文件,或者沒有權限訪問的文件相關信息
25 open_file_cache_valid time;
每隔多久檢查一次緩存中的緩存項的有效性,默認爲60s;
26 open_file_cache_min_uses number;緩存項在非活動期限內的最小應該被訪問的次數;
27 allow address |CIDR |unix: | all; 容許訪問的地址
28 deny address |CIDR |unix: | all; 禁止訪問的地址
ngx_http_auth_basic_module模塊的設置(basic認證)
29 auth_basic string|off; 使用http basic認證協議對用戶進行認證
30 auth_basic_user_file;實現用戶認證的帳號文件;
文件格式:
name1:password1
name2:password2:comment
密碼格式:
1 encrypted with the crypt() function;
2 md5加密;
例如:
配置文件改成: location /admin/{ auth_basic "Admin Area";
auth_basic_user_file /etc]/nginx/.ngxpasswd # 指定驗證文件的位置 }
yum install httpd -y #不用啓動httpd服務
htpasswd -c -m /etc/nginx/.ngxhtpasswd tom #第一次要加 -c
htpasswd -m /etc/nginx/.ngxhtpasswd jerry
nginx -s reload
ngx_http_log_module: 模塊的配置(訪問日誌)
31 log_format 定義日誌格式及其名稱,日誌格式通常經過內置變量來定義
32 access_log path [format[buffer=size[flush=time]]];
access_log off;
訪問日誌文件路徑,格式名稱以及緩存大小和刷寫時間間隔;建議定義緩衝以提高性能;
33 open_log_file_cache max=N [inactive=time][min_uses=N][valid=time];
ngx_http_stub_status_module模塊設置: 只能用做server和location中
34 stub_status; 經過指定的uri輸出 stub_status;
location /status/ {
stub_status;
}
內部含有敏感信息,通常不設置,要麼加上訪問控制
輸出:
active connections: 291 當前活動的客戶端鏈接數
server accepts handle requests: 已經接受的客戶端鏈接總數量 已經處理事後客戶端鏈接總數量 客戶端總的請求數據
accepts handled requests
Reading 6 Writing 179 Waiting 106 正在讀取的客戶端請求的數量 正向其發送響應報文的鏈接數量 等待其發出請求的空閒鏈接數量;
ngx_http_referer_module模塊配置(基於請求報文中的Rerefer首部作訪問控制)
35 valid_referers none|blocked|server_names|string...
none:請求報文不存在referer首部
blocked:請求報文中存在referer首部,但其沒有有效值,或者非以http://或https:// 開頭
server_names: 其值爲一個主機名;
arbitrary string;直接字符串,可使用*通配符
regular expression:以~起始的正則表達式
內置變量:$invalid_referer(全部不能符合valid_referer指定定義的引用請求均爲不合法引用
示例:防盜鏈
valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~\.google\; if($invalid_referer){ return 403; }
防盜鏈的例子
location ~* \.(gif|jpg|png|jpeg)$ { #針對防盜鏈的文件類型 expires 30d; valid_referers *.hugao8.com www.hugao8.com m.hugao8.com *.baidu.com *.google.com; #白名單,容許訪問的域名 if ($invalid_referer) { rewrite ^/ http://www.hehe/404.jpg; #讓盜鏈者連接到其餘地方 } }
ngx_http_proxy_module模塊:
server{
listen
server_name
location / {
proxy_pass http://localhost:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr; #真正訪問的客戶端ip
}
}
格式:
location /url { #這個url的路徑能夠不存在
proxy_pass http://back_server:port/newurl;
}