Nginx(自定義/預(yù)定義)變量,alias虛擬目錄解讀
1. Nginx 變量簡(jiǎn)介
所有的 Nginx變量在 Nginx 配置文件中引用時(shí)都須帶上 $ 前綴
在 Nginx 配置中,變量只能存放一種類型的值,而且也只存在一種類型,那就是字符串類型
所有的變量值都可以通過這種方式引用 $變量名
2. Nginx 變量的定義和使用
nginx中的變量分為兩種,自定義變量與內(nèi)置預(yù)定義變量
2.1. 自定義變量
1、聲明變量可以在sever,http,location等標(biāo)簽中使用set命令聲明變量,語法如下:
set $變量名 變量值
注意:
- nginx 中的變量必須都以
$開頭 - nginx 的配置文件中所有使用的變量都必須是聲明過的,否則 nginx 會(huì)無法啟動(dòng)并打印相關(guān)異常日志
nginx安裝echo模塊
//查看已經(jīng)安裝的nginx的版本 [root@localhost ~]# nginx -v nginx version: nginx/1.24.0 //上傳或者下載一個(gè)相同版本的nginx包 [root@localhost ~]# wget http://nginx.org/download/nginx-1.24.0.tar.gz //下載echo模塊的安裝包 [root@localhost ~]# wget https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz [root@localhost ~]# ls anaconda-ks.cfg nginx-1.24.0.tar.gz v0.63.tar.gz //解壓到相同路徑下: [root@localhost ~]# tar xzvf nginx-1.24.0.tar.gz -C /usr/local/ [root@localhost ~]# tar xzvf v0.63.tar.gz -C /usr/local/ //安裝編譯工具 [root@localhost ~]# cd /usr/local/ [root@localhost local]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel gd-devel //添加模塊: [root@localhost local]# cd nginx-1.24.0/ //添加上原來已經(jīng)有的參數(shù)和新添加的模塊: [root@localhost nginx-1.24.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.63 //編譯,千萬不要make install 否則會(huì)覆蓋原來的文件 [root@localhost nginx-1.24.0]# make //將原來的nignx二進(jìn)制文件備份 [root@localhost nginx-1.24.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak //拷貝替換新的nignx二進(jìn)制文件 [root@localhost nginx-1.24.0]# cp objs/nginx /usr/sbin/ [root@localhost nginx-1.24.0]# systemctl restart nginx //重啟nginx //查看模塊是否添加成功 [root@localhost nginx-1.24.0]# nginx -V nginx version: nginx/1.24.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.63
2、配置 $foo=hello
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim echo.conf
server {
listen 80;
server_name localhost;
location /test {
set $foo hello;
echo "foo: $foo";
}
}
[root@localhost conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# systemctl restart nginx
//訪問測(cè)試
[root@localhost conf.d]# curl http://localhost/test
foo: hello
Nginx 變量的創(chuàng)建只能發(fā)生在 Nginx 配置加載的時(shí)候,或者說 Nginx 啟動(dòng)的時(shí)候。
而賦值操作則只會(huì)發(fā)生在請(qǐng)求實(shí)際處理的時(shí)候。這意味著不創(chuàng)建變量,直接使用變量會(huì)導(dǎo)致啟動(dòng)失敗。
2.2. 內(nèi)置預(yù)定義變量
內(nèi)置預(yù)定義變量即無需聲明就可以使用的變量,通常包括一個(gè) http 請(qǐng)求或響應(yīng)中一部分內(nèi)容的值,以下為一些常用的內(nèi)置預(yù)定義變量
| 變量名 | 定義 |
|---|---|
| $arg_PARAMETERGET | 請(qǐng)求中變量名PARAMETER參數(shù)的值。 |
| $args | 這個(gè)變量等于GET請(qǐng)求中的參數(shù)。例如,foo=123&bar=blahblah;這個(gè)變量只可以被修改。 |
| $binary_remote_addr | 二進(jìn)制碼形式的客戶端地址。 |
| $body_bytes_sent | 傳送頁面的字節(jié)數(shù)。 |
| $content_length | 請(qǐng)求頭中的Content-length字段。 |
| $content_type | 請(qǐng)求頭中的Content-Type字段。 |
| $cookie_COOKIE | cookie COOKIE的值。 |
| $document_root | 當(dāng)前請(qǐng)求在root指令中指定的值。 |
| $document_uri | 與$uri相同。 |
| $host | 請(qǐng)求中的主機(jī)頭(Host)字段,如果請(qǐng)求中的主機(jī)頭不可用或者空,則為處理請(qǐng)求的server名稱(處理請(qǐng)求的server的server_name指令的值)。值為小寫,不包含端口。 |
| $hostname | 機(jī)器名使用 gethostname系統(tǒng)調(diào)用的值 |
| $http_HEADERHTTP | 請(qǐng)求頭中的內(nèi)容,HEADER為HTTP請(qǐng)求中的內(nèi)容轉(zhuǎn)為小寫,-變?yōu)開(破折號(hào)變?yōu)橄聞澗€),例如:$http_user_agent(Uaer-Agent的值)。 |
| $sent_http_HEADER | HTTP響應(yīng)頭中的內(nèi)容,HEADER為HTTP響應(yīng)中的內(nèi)容轉(zhuǎn)為小寫,-變?yōu)開(破折號(hào)變?yōu)橄聞澗€),例如: $sent_http_cache_control, $sent_http_content_type…; |
| $is_args | 如果$args設(shè)置,值為"?“,否則為”"。 |
| $limit_rate | 這個(gè)變量可以限制連接速率。 |
| $nginx_version | 當(dāng)前運(yùn)行的nginx版本號(hào)。 |
| $query_string | 與$args相同。 |
| $remote_addr | 客戶端的IP地址。 |
| $remote_port | 客戶端的端口。 |
| $remote_user | 已經(jīng)經(jīng)過Auth Basic Module驗(yàn)證的用戶名。 |
| $request_filename | 當(dāng)前連接請(qǐng)求的文件路徑,由root或alias指令與URI請(qǐng)求生成。 |
| $request_body | 這個(gè)變量(0.7.58+)包含請(qǐng)求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比較有意義。 |
| $request_body_file | 客戶端請(qǐng)求主體信息的臨時(shí)文件名。 |
| $request_completion | 如果請(qǐng)求成功,設(shè)為"OK";如果請(qǐng)求未完成或者不是一系列請(qǐng)求中最后一部分則設(shè)為空。 |
| $request_method | 這個(gè)變量是客戶端請(qǐng)求的動(dòng)作,通常為GET或POST。包括0.8.20及之前的版本中,這個(gè)變量總為main request中的動(dòng)作,如果當(dāng)前請(qǐng)求是一個(gè)子請(qǐng)求,并不使用這個(gè)當(dāng)前請(qǐng)求的動(dòng)作。 |
| $request_uri | 這個(gè)變量等于包含一些客戶端請(qǐng)求參數(shù)的原始URI,它無法修改,請(qǐng)查看$uri更改或重寫URI。 |
| $scheme | 所用的協(xié)議,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect。 |
| $server_addr | 服務(wù)器地址,在完成一次系統(tǒng)調(diào)用后可以確定這個(gè)值,如果要繞開系統(tǒng)調(diào)用,則必須在listen中指定地址并且使用bind參數(shù)。 |
| $server_name | 服務(wù)器名稱。 |
| $server_port | 請(qǐng)求到達(dá)服務(wù)器的端口號(hào)。 |
| $server_protocol | 請(qǐng)求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1。 |
| $uri | 請(qǐng)求中的當(dāng)前URI(不帶請(qǐng)求參數(shù),參數(shù)位于args),不同于瀏覽器傳遞的args),不同于瀏覽器傳遞的args),不同于瀏覽器傳遞的request_uri的值,它可以通過內(nèi)部重定向,或者使用index指令進(jìn)行修改。不包括協(xié)議和主機(jī)名,例如/foo/bar.html |
3. Nginx alias 虛擬目錄(拓展)
root:為 location 指定網(wǎng)站的根目錄。location 里定義的子路徑是跟這個(gè)根目錄相對(duì)的。alias:為 location 指定服務(wù)的具體文件路徑。location 里定義的子路徑會(huì)被忽略
使用alias標(biāo)簽的目錄塊中不能使用 rewrite 的 break 。
server {
listen 80;
server_name localhost;
location /test {
root /var/www/html;
index index.html;
}
location /qfedu {
alias /var/www/nginx;
#訪問http://x.x.x.x/qfedu時(shí)實(shí)際上訪問是/var/www/nginx/index.html
index index.html;
}
}
//創(chuàng)建location匹配的目錄和文件
[root@localhost ~]# mkdir /var/www/html/test
[root@localhost ~]# echo "location /test" >> /var/www/html/test/index.html
//創(chuàng)建alias匹配的目錄和文件
[root@localhost ~]# mkdir /var/www/nginx
[root@localhost ~]# echo "alias /qfedu" >> /var/www/nginx/index.html
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx
root 和 alias 的主要區(qū)別是:
- 使用root,實(shí)際的路徑就是:
root值 + location值。root會(huì)將完整的url映射進(jìn)文件路徑,root可以看成是一個(gè)相對(duì)路徑,訪問url網(wǎng)址時(shí),http://www.baidu.com/test,這個(gè)test必須要在/usr/share/nginx/html下有這個(gè)目錄,目錄中必須要這個(gè)文件才行。 - 使用alias,實(shí)際的路徑就是:
alias值。alias只會(huì)將localhost后的url映射到文件路徑。而使用alias時(shí),訪問http://www.baidu.com/test,在/usr/share/nginx/html不需要有這個(gè)test目錄即可。 - alias只能位于location塊中,root可以放在http,server,location塊中
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx安裝nginx-rtmp-module模塊的實(shí)現(xiàn)
nginx-rtmp-module是一個(gè)用于Nginx的第三方模塊,它使Nginx能夠支持實(shí)時(shí)多媒體流的傳輸和處理,本文主要介紹了Nginx安裝nginx-rtmp-module模塊,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
詳解nginx前端根據(jù)$remote_addr分發(fā)方法
這篇文章主要介紹了詳解nginx前端根據(jù)$remote_addr分發(fā)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法
本文主要介紹了Nginx部署項(xiàng)目上傳文件報(bào)錯(cuò)413的解決方法,報(bào)錯(cuò)413是因?yàn)镹ginx對(duì)上傳大小做了限制,所以我們需要配置文件,下面就來解決這個(gè)問題,感興趣的可以了解一下2024-03-03
Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn)
Nginx請(qǐng)求超時(shí)是服務(wù)器無法在規(guī)定時(shí)間內(nèi)完成對(duì)客戶端請(qǐng)求的響應(yīng),本文就來介紹一下Nginx?請(qǐng)求超時(shí)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
Nginx日志中request_time和upstream_response_time區(qū)別
Nginx日志中的request_time和upstream_response_time是關(guān)鍵的性能指標(biāo),本文就來介紹一下Nginx日志中request_time和upstream_response_time區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-11-11
keepalived監(jiān)控nginx進(jìn)程的實(shí)現(xiàn)示例
本文主要介紹了keepalived監(jiān)控nginx進(jìn)程的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08
Nginx 緩存系統(tǒng) proxy_cache工作原理解析
Nginx 的 proxy_cache 模塊允許 Nginx 作為反向代理服務(wù)器時(shí)緩存后端服務(wù)器的響應(yīng),本文給大家介紹Nginx 緩存系統(tǒng) proxy_cache的工作原理,感興趣的朋友跟隨小編一起看看吧2024-12-12

