nginx的編譯安裝
- ? ?tar xf nginx-1.12.2.tar.gz
- ? cd nginx-1.12.2
- ? ? yum install pcre-devel
- ?./configure –help
- ? groupadd -r nginx
- ? ? useradd -g nginx -r nginx
- ? ?id nginx
- ? ? ./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/log/nginx.log –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi –http-uwsgi-temp-path=/var/tmp/nginx/uwsgi
- ? ? make && make install
- ?mkdir -pv /var/tmp/nginx/{client,fastcgi,proxy,uwsgi}
- ? ss -tnl
- ? /usr/local/nginx/sbin/nginx
- ? ss -tnl
- ? ss -tnlp
- ? ?ps aux
Nginx作為web服務時使用的配置指令
1、server { … } ?定義一個虛擬主機;
server {?? # 配置虛擬主機示例
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
2、listen?? 指定虛擬主機所監聽的端口
listen address[:port]
3、server_name
指明虛擬主機的主機名稱; 還可使用正則表達式或通配符
匹配機制:
(1) 先做精確匹配
(2) 左側通配符匹配檢查
(3) 右側通配符匹配檢查
(4) 正則表達式
(5)default server 默認服務器
4、location [ = | ~ | ~* | ^~ ] {…}
功能:允許根據用戶請求的URL來匹配定義的各location;匹配到時,此請求將被相應的location配置塊中的配置所處理,例如做訪問控制等功能。
=:對URI做精確匹配;
~:對URI做正則表達式模式匹配,區分字符大小寫
~*:對URI做正則表達式模式匹配,不區分字符大小寫
^~:對URI的左半部分做匹配檢查,不區分字符大小寫
匹配優先級:=, ^~, ~/~*,不帶符號
5、alias path; ??#定義路徑別名,文檔映射的另一種機制;僅能用于location上下文
注意:location中使用root指令和alias指令的意義不同
(a) root,給定的路徑對應于location中的/uri/左側的/
(b) alias,給定的路徑對應于location中的/uri/右側的/
6、error_page code … [=[response]] uri; ?#定義默認錯誤頁面
[root@nginx1 /etc/nginx/conf.d]#vim vhost1.conf
server {
listen 80;
server_name www.ilinux.io;
root /data/nginx/vhost1;
location / {
#root /data/nginx/vhost2;
allow all;
}
location ~* \.(jpg|png)$ {
deny 172.16.250.217;
allow all;
}
location ^~/images/ {
alias /data/pictures/;
}
error_page 404 /notfound.html;???????? #如果是404就在notfound.html中
location = /notfound.html {?????????????? #如果訪問notfound.html
root /data/nginx/error_pages;??? #錯誤頁面就在/data/nginx/error_pages/notfound.html中
}
}
[root@nginx1 /etc/nginx/conf.d]#mkdir /data/nginx/error_pages
[root@nginx1 /etc/nginx/conf.d]#vim /data/nginx/error_pages/notfound.html
<h2>——————–</h2>
<h3>notfound</h3>
[root@nginx1 /etc/nginx/conf.d]#nginx -s reload
網絡連接相關的配置
7、keepalive_timeout timeout [header_timeout]; ?#設定保持連接的超時時長,0表示禁止長連接;默認為75s
8、keepalive_requests number; ?#在一次長連接上所允許請求的資源的最大數量,默認為100(使用默認值即可)
9、keepalive_disable none | browser …; ?#對哪種瀏覽器禁用長連接
10、send_timeout time; ?#向客戶端發送響應報文的超時時長,是指兩次寫操作之間的間隔時長
如客戶端發送請求后,由于斷電等等原因,無法接收到服務器發送的報文
11、client_body_buffer_size size; ?#用于接收客戶端請求報文的body部分的緩沖區大?。荒J為16k;超出此大小時,其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置
12、open_file_cache off; ?# 是否開啟緩存
open_file_cache max=N [inactive=time];
nginx可以緩存以下三種信息
(1) 文件的描述符、文件大小和最近一次的修改時間
(2) 打開的目錄結構
(3) 沒有找到的或者沒有權限訪問的文件的相關信息
max=N:可緩存的緩存項上限;達到上限后會使用LRU(最近最少使用)算法實現緩存管理
inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少于open_file_cache_min_users指令所指定的次數的緩存項即為非活動項
ngx_http_access_module模塊:
實現基于ip的訪問控制功能
13、allow address | CIDR | unix: | all;(允許)
14、deny address | CIDR | unix: | all;(禁止)
ngx_http_auth_basic_module模塊
實現基于用戶的訪問控制,使用basic機制進行用戶認證;
15、auth_basic string | off;
16、auth_basic_user_file file;
注意:htpasswd命令由httpd-tools所提供
ngx_http_stub_status_module模塊(nginx內置的內建狀態頁)
用于輸出nginx的基本狀態信息;
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections: 活動狀態的連接數;
accepts:已經接受的客戶端請求的總數;
handled:已經處理完成的客戶端請求的總數;
requests:客戶端發來的總的請求數;
Reading:處于讀取客戶端請求報文首部的連接的連接數;
Writing:處于向客戶端發送響應報文過程中的連接數;
Waiting:處于等待客戶端發出請求的空閑連接數;
17、stub_status;
18、log_format name string …; #日志格式
string可以使用nginx核心模塊及其它模塊內嵌的變量;
$bytes_sent:發送到客戶端的字節數
$connection:連接序列號
$connection_requests:目前一些通過連接發出的請求(1.1.18)
$msec:時間與一個毫秒分辨率秒日志寫入的時間
$pipe:”p”如果請求被流水線
$request_length:請求長度
$request_time:請求處理時間在毫秒分辨率秒; 第一字節之間經過的時間是從在客戶端和日志寫入讀出之后,最后的字節被發送到客戶端
$status:響應狀態
$time_iso8601:在ISO 8601標準格式的本地時間
$time_local:在通用日志格式的本地時間
19、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; ??#訪問日志文件路徑,格式及相關的緩沖的配置;
access_log off;
Default:access_log logs/access.log combined;
Context:http, server, location, if in location, limit_except
訪問日志文件路徑,格式及相關的緩沖的配置;
buffer=size
flush=time
[root@nginx1 /etc/nginx/conf.d]#vim vhost1.conf
server {
listen 80;
server_name www.ilinux.io;
root /data/nginx/vhost1;
access_log /var/log/nginx/vhost1_access.log main; 定義在server中,對整個文件有效
[root@nginx1 /etc/nginx/conf.d]#nginx -s reload
[root@nginx1 /etc/nginx/conf.d]#tail /var/log/nginx/vhost1_access.log?? 查看日志
172.16.254.217 – tom [14/Jul/2017:22:02:12 +0800] “GET /images/fish.jpg HTTP/1.1” 304 0 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0” “-”
172.16.254.217 – tom [14/Jul/2017:22:04:33 +0800] “GET /images/2560×1600.jpg HTTP/1.1” 404 48 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0” “-”
172.16.254.217 – tom [14/Jul/2017:22:05:29 +0800] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0” “-”
172.16.254.217 – tom [14/Jul/2017:22:05:43 +0800] “GET /admin/ HTTP/1.1” 304 0 “-” “Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0” “-”
對于某個location使用單獨的訪問日志,以admin為例
location ~* ^/(admin|login) {
auth_basic “admin area or login url”;
auth_basic_user_file /etc/nginx/.ngxpasswd;
access_log /var /log/nginx/vhost1_access.log main;
}
ngx_http_gzip_module:
The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.(ngx_http_gzip_module模塊是一個過濾器,壓縮響應使用“gzip”方法。這通常有助于將傳輸數據的大小減少一半甚至更多。)
20、gzip on | off;
Default: gzip off;
Context: http, server, location, if in location
Enables or disables gzipping of responses.(啟用或禁用Gzipping反應)
是否啟用gzip壓縮響應報文;不是所有瀏覽器都支持壓縮機制
原創文章,作者:nene,如若轉載,請注明出處:http://www.www58058.com/88058