Nginx配置文件詳解:
配置文件參考:http://blog.csdn.net/tjcyjd/article/details/50695922
Nginx虛擬主機(三種方式):
一個server標籤就是一個虛擬主機
1、基於域名的虛擬主機。通過域名來區分虛擬主機
===》應用:外部網站(重要)
小例子:
去掉註釋和空白符:
egrep -v "#|^$" nginx.conf.default > nginx.conf
nginx配置文件:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.test.com;
location / {
root html/test/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name bbs.test.com;
location / {
root html/test/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
創建目錄以及賦予權限:
[root@localhost nginx]# mkdir /usr/local/nginx/html/test/{bbs,www}
[root@localhost nginx]# echo "www.test.com" > /usr/local/nginx/html/test/www/index.html
[root@localhost nginx]# echo "bbs.test.com" > /usr/local/nginx/html/test/bbs/index.html
[root@localhost nginx]# chmod +x -R /usr/local/nginx/html/test/
檢測配置文件:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重新加載配置文件:
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
修改host文件:
172.16.27.92 www.test.com bbs.test.com
測試:
[root@localhost conf]# curl www.test.com
www.test.com
[root@localhost conf]# curl bbs.test.com
bbs.test.com
基於域名的虛擬主機訪問原理: 是通過請求頭的host來辨別
簡單測試方法:
步驟一:在win7 host文件中修改爲:
172.16.27.XX www.test.com bbs.test.com
瀏覽器訪問 www.test.com bbs.test.com 172.16.27.XX
(1)、當訪問172.16.27.XX時,得到的是www.test.com,主機是基於域名的虛擬主機,所以訪問ip之後,請求後host傳參是172.16.27.xx,默認讀取第一個虛擬主機
(2)、訪問www.test.com
(3)訪問bbs.baidu.com
2、基於端口的虛擬主機。通過端口區分虛擬主機
===》應用:公司內部網站(重要)
nginx.conf配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8001;
server_name www.test.com;
location / {
root html/test/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8002;
server_name www.test.com;
location / {
root html/test/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /usr/local/nginx/sbin/nginx -s reload
訪問網址:
[root@localhost conf]# curl www.test.com:8001
www.test.com
[root@localhost conf]# curl www.test.com:8002
bbs.test.com
3、基於IP的虛擬主機。幾乎不用。不支持ifconfig別名,需要用ip命令,配置文件可以。
用include指令實現nginx多虛擬主機配置
編輯nginx.conf
cd /usr/local/nginx/conf
vim nginx.conf
nginx.conf 如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/
include extra/bbs.conf;
}
#也可以使用 include extra/*.conf 來代替的,表示所有文件,這裏支持通配符.
創建虛擬主機配置文件
mkdir extra
cat -n nginx.conf.basename.bak
sed -n '12,23p' nginx.conf.basename.bak #查看該文件12至23行
sed -n '12,23p' nginx.conf.basename.bak > extra/www.conf
sed -n '25,36p' nginx.conf.basename.bak > extra/bbs.conf
檢測配置文件與虛擬主機訪問測試
cat extra/www.conf
cat extra/bbs.conf
../sbin/nginx -t
../sbin/nginx -s reload
curl www.test.com
netstat -lanp|grep nginx
curl www.test.com:8001
curl www.test.com:8002
Nginx別名作用及配置
當你訪問baidu.com的時候能跳轉到www.baidu.com 類似這樣的實例,一般有兩種方式:
1、捕捉301代碼實現跳轉
2、Nginx別名的配置與DNS中A記錄添加域名
nginx.conf
server { listen 8001; server_name www.test.com test.com; location / { root html/test/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
測試
修改host文件,添加test.com
vim /etc/hosts #如果是DNS則要添加A記錄 172.16.27.XX www.test.com bbs.test.com test.com /usr/local/nginx/sbin/nginx -t #檢查nginx.conf文件 /usr/local/nginx/sbin/nginx -s reload #重載配置文件 curl :8001 curl test.com:8001
Nginx狀態信息配置及信息詳解
說明:
http_stub_status模塊能夠獲取Nginx的併發連接,請求等。
因 此模塊非核心模塊,所以需要在編譯的時候需手動添加編譯參數–with-http_stub_status_module
編譯:
# ./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_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 \ --http-scgi-temp-path=/var/tmp/nginx/scgi --with-http_stub_status_module make && make install
nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/status.conf; }
extra目錄下的status.conf文件
server { listen 8001; server_name status.test.com; location / { stub_status on; access_log off; } }
檢查:(測試前修改host文件或DNS的A記錄)
/usr/local/nginx/sbin/nginx -t //測試配置是否正確 /usr/local/nginx/sbin/nginx -s reload [root@localhost extra]# curl status.test.com:8001 Active connections: 1 server accepts handled requests 12 12 12 Reading: 0 Writing: 1 Waiting: 0
分析:
Active connections: 291 server accepts handled requests : 16630948 16630948 31070465 Reading: 6 Writing: 179 Waiting: 106 active connections — 對後端發起的活動連接數 server accepts handled requests — nginx 總共處理了 16630948 個連接, 成功創建 16630948 次握手 (證明中間沒有失敗的), 總共處理了 31070465 個請求 (平均每次握手處理了 1.8個數據請求) reading — nginx 讀取到客戶端的Header信息數 writing — nginx 返回給客戶端的Header信息數 waiting — 開啓 keep-alive 的情況下,這個值等於 active – (reading + writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連接
nginx日誌配置指令詳解
日誌對於統計排錯來說非常有利的。本文總結了nginx日誌相關的配置如access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
nginx有一個非常靈活的日誌記錄模式。每個級別的配置可以有各自獨立的訪問日誌。日誌格式通過log_format命令來定義。ngx_http_log_module是用來定義請求日誌格式的。
1. log_format指令
注意:日誌格式可參考nginx.conf.defualt文件
語法: log_format name string …;
默認值: log_format combined “…”;
配置段: http
name表示格式名稱,string表示等義的格式。log_format有一個默認的無需設置的combined日誌格式,相當於apache的combined日誌格式,如下所示:
nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/status.conf; }
如上格式的訪問日誌:
172.16.14.151 - - [01/Apr/2017:10:13:18 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
如果nginx位於負載均衡器,squid,nginx反向代理之後,web服務器無法直接獲取到客戶端真實的IP地址了。 $remote_addr獲取反向代理的IP地址。反向代理服務器在轉發請求的http頭信息中,可以增加信息$http_x_forwarded_for,用來記錄 客戶端IP地址和客戶端請求的服務器地址
日誌格式允許包含的變量註釋如下:
$remote_addr, $http_x_forwarded_for 記錄客戶端IP地址 $remote_user 記錄客戶端用戶名稱 $request 記錄請求的URL和HTTP協議 $status 記錄請求狀態 $body_bytes_sent 發送給客戶端的字節數,不包括響應頭的大小; 該變量與Apache模塊mod_log_config裏的“%B”參數兼容。 $bytes_sent 發送給客戶端的總字節數。 $connection 連接的序列號。 $connection_requests 當前通過一個連接獲得的請求數量。 $msec 日誌寫入時間。單位爲秒,精度是毫秒。 $pipe 如果請求是通過HTTP流水線(pipelined)發送,pipe值爲“p”,否則爲“.”。 $http_referer 記錄從哪個頁面鏈接訪問過來的 $http_user_agent 記錄客戶端瀏覽器相關信息 $request_length 請求的長度(包括請求行,請求頭和請求正文)。 $request_time 請求處理時間,單位爲秒,精度毫秒; 從讀入客戶端的第一個字節開始,直到把最後一個字符發送給客戶端後進行日誌寫入爲止。 $time_iso8601 ISO8601標準格式下的本地時間。 $time_local 通用日誌格式下的本地時間。
2. access_log指令
語法: access_log path [format [buffer=size [flush=time]]];
access_log path format gzip[=level] [buffer=size] [flush=time];
access_log syslog:server=address[,parameter=value] [format];
access_log off;
默認值: access_log logs/access.log main;
配置段: http, server, location, if in location, limit_except
gzip壓縮等級。
buffer設置內存緩存區大小。
flush保存在緩存區中的最長時間。
不記錄日誌:access_log off;
使用默認main格式記錄日誌:access_log logs/access.log 或 access_log logs/access.log main;
注意:根據如上include extra/www.conf
server { listen 8001; access_log /var/log/nginx/access_log/www/access_www.log main; server_name www.test.com test.com; location / { root html/test/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Nginx下的rewrite規則
一.正則表達式匹配,其中:
* ~ 爲區分大小寫匹配
* ~* 爲不區分大小寫匹配
* !~和!~*分別爲區分大小寫不匹配及不區分大小寫不匹配
二.文件及目錄匹配,其中:
* -f和!-f用來判斷是否存在文件
* -d和!-d用來判斷是否存在目錄
* -e和!-e用來判斷是否存在文件或目錄
* -x和!-x用來判斷文件是否可執行
三.rewrite指令的最後一項參數爲flag標記,flag標記有:
1.last 相當於apache裏面的[L]標記,表示rewrite。
2.break本條規則匹配完成後,終止匹配,不再匹配後面的規則。
3.redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址。
4.permanent 返回301永久重定向,瀏覽器地址會顯示跳轉後的URL地址。
實例:
nginx 301跳轉到帶www域名方法rewrite
www.conf
server { listen 8001; access_log /var/log/nginx/access_log/www/access_www.log main; server_name www.test.com test.com; location / { root html/test/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; access_log /var/log/nginx/access_log/www/access_www.log main; server_name test.com; location / { root html/test/www; index index.html index.htm; } rewrite ^/(.*)$ http://www.test.com:8001/$1 permanent; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }