Nginx高級配置-變量使用
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
nginx的變量可以在配置文件中引用,作為功能判斷或者日志等場景使用,變量可以分為內置變量和自定義變量,內置變量是由nginx模塊自帶,通過變量可以獲取到眾多的與客戶端訪問相關的值。
一.查看Nginx的內置變量
1>.如下圖所示,訪問nginx的官網,並點擊"documentation"
2>.在官方文檔中找到模塊相關,點擊"Alphabetical index of variables"
3>.查看官網的變量,如下圖所示,以"$"開頭的變量都是內置變量
4>.常用的內置變量
$remote_addr; 存放了客戶端的地址,注意是客戶端的公網IP,也就是一家人訪問一個網站,則會顯示為路由器的公網IP。 $args; #變量中存放了URL中的指令,例如http://node101.yinzhengjie.org.cn/main/index.do?id=20190221&partner=search中的id=20190221&partner=search $document_root; 保存了針對當前資源的請求的系統根目錄,如/apps/nginx/html。 $document_uri; 保存了當前請求中不包含指令的URI,注意是不包含請求的指令,比如http://node101.yinzhengjie.org.cn/main/index.do?id=20190221&partner=search會被定義為/main/index.do。 $host; 存放了請求的host名稱。 $http_user_agent; 客戶端瀏覽器的詳細信息 $http_cookie; 客戶端的cookie信息。 limit_rate 10240; echo $limit_rate; 如果nginx服務器使用limit_rate配置了顯示網絡速率,則會顯示,如果沒有設置, 則顯示0。 $remote_port; 客戶端請求Nginx服務器時隨機打開的端口,這是每個客戶端自己的端口。 $remote_user; 已經經過Auth Basic Module驗證的用戶名。 $request_body_file; 做反向代理時發給后端服務器的本地資源的名稱。 $request_method; 請求資源的方式,GET/PUT/DELETE等 $request_filename; 當前請求的資源文件的路徑名稱,由root或alias指令與URI請求生成的文件絕對路徑,如/apps/nginx/html/main/index.html $request_uri; 包含請求參數的原始URI,不包含主機名,如:/main/index.do?id=20190221&partner=search 。 $scheme; 請求的協議,如ftp,https,http等。 $server_protocol; 保存了客戶端請求資源使用的協議的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等。 $server_addr; 保存了服務器的IP地址。 $server_name; 請求的服務器的主機名。 $server_port; 請求的服務器的端口號。
5>.在服務端打印nginx內置變量並返回給客戶端
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
}
location /main {
index index.html;
default_type text/html;
echo "remote_addr = $remote_addr";
echo "******";
echo "args = $args";
echo "******";
echo "document_root = $document_root";
echo "******";
echo "document_uri = $document_uri";
echo "******";
echo "host = $host";
echo "******";
echo "http_user_agent = $http_user_agent";
echo "******";
echo "http_cookie = $http_cookie"
echo "******";
limit_rate 10240;
echo "limit_rate = $limit_rate";
echo "******";
echo "remote_prot = $remote_port";
echo "******";
echo "remote_user = $remote_user";
echo "******";
echo "request_body_file = $request_body_file";
echo "******";
echo "request_method = $request_method";
echo "******";
echo "request_filename = $request_filename";
echo "******";
echo "request_uri = $request_uri";
echo "******";
echo "scheme = $scheme";
echo "******";
echo "server_protocol = $server_protocol";
echo "******";
echo "server_addr = $server_addr";
echo "******";
echo "server_name = $server_name";
echo "******";
echo "server_port = $server_port";
echo "******";
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
二.自定義Nginx的變量
1>.查看nginx的主配置文件
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
events {
worker_connections 100000;
use epoll;
accept_mutex on;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
charset utf-8;
#最大緩存10000個文件,非活動數據超時時長60s
open_file_cache max=10000 inactive=60s;
#每間隔60s檢查一下緩存數據有效性
open_file_cache_valid 60s;
#60秒內至少被命中訪問5次才被標記為活動數據
open_file_cache_min_uses 5;
#緩存錯誤信息
open_file_cache_errors on;
#隱藏Nginx server版本。
server_tokens off;
#當文件大於等於給定大小時,同步(直接)寫磁盤,而非寫緩存。
directio 4m;
#上傳文件相關參數
client_max_body_size 10m;
client_body_buffer_size 16k;
client_body_temp_path /yinzhengjie/data/web/nginx/temp 1 2 2;
#IE系列的瀏覽器禁用長連接,默認就是禁用了IE的長連接功能.
keepalive_disable msie6;
#開啟長連接后,返回客戶端的會話保持時間為60s,單次長連接累計請求達到指定次數請求或65秒就會被斷開,后面的60為發送給客戶端應答報文頭部中顯示的超時時間設置為60s:如不設置
客戶端將不顯示超時時間。 keepalive_timeout 65 60;
#在一次長連接上所允許請求的資源的最大數量
keepalive_requests 3;
#導入其他路徑的配置文件
include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
2>..配置nginx的子配置文件
[root@node101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/nginx/conf.d/share.conf
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/share.conf
server {
listen 80;
server_name node101.yinzhengjie.org.cn;
location / {
root /yinzhengjie/data/web/nginx/static;
index index.html;
}
location /nginx_status {
stub_status;
allow 172.30.1.108;
deny all;
}
location /main {
index index.html;
default_type text/html;
set $name jason; #這里我使用了set關鍵字自定義了一個name變量
set $nginx_name $server_name; #這里我依舊使用了set關鍵字自定義了一個變量,只不過該變量名的值來自於內部變量的值。
echo "姓名: $name";
echo "************";
echo "Nginx服務器名稱: $nginx_name";
}
}
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
3>.重新加載nginx的配置文件
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 18:30 ? 00:00:00 nginx: master process nginx
nginx 9598 9297 0 19:38 ? 00:00:00 nginx: worker process
nginx 9599 9297 0 19:38 ? 00:00:00 nginx: worker process
nginx 9600 9297 0 19:38 ? 00:00:00 nginx: worker process
nginx 9601 9297 0 19:38 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root 9297 1 0 18:30 ? 00:00:00 nginx: master process nginx
nginx 9635 9297 1 19:40 ? 00:00:00 nginx: worker process
nginx 9636 9297 1 19:40 ? 00:00:00 nginx: worker process
nginx 9637 9297 1 19:40 ? 00:00:00 nginx: worker process
nginx 9638 9297 1 19:40 ? 00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]#
4>.瀏覽器訪問"http://node101.yinzhengjie.org.cn/main",如下圖所示。