欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx(自定義/預定義)變量,alias虛擬目錄解讀

 更新時間:2025年07月03日 08:52:46   作者:TA548464  
這篇文章主要介紹了Nginx(自定義/預定義)變量,alias虛擬目錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

1. Nginx 變量簡介

所有的 Nginx變量在 Nginx 配置文件中引用時都須帶上 $ 前綴

在 Nginx 配置中,變量只能存放一種類型的值,而且也只存在一種類型,那就是字符串類型

所有的變量值都可以通過這種方式引用 $變量名

2. Nginx 變量的定義和使用

nginx中的變量分為兩種,自定義變量與內(nèi)置預定義變量

2.1. 自定義變量

1、聲明變量可以在sever,http,location等標簽中使用set命令聲明變量,語法如下:

set $變量名 變量值

注意:

  • nginx 中的變量必須都以 $ 開頭
  • nginx 的配置文件中所有使用的變量都必須是聲明過的,否則 nginx 會無法啟動并打印相關(guān)異常日志

nginx安裝echo模塊

//查看已經(jīng)安裝的nginx的版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.24.0

//上傳或者下載一個相同版本的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 否則會覆蓋原來的文件
[root@localhost nginx-1.24.0]# make
//將原來的nignx二進制文件備份
[root@localhost nginx-1.24.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak 
//拷貝替換新的nignx二進制文件
[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
//訪問測試
[root@localhost conf.d]# curl  http://localhost/test
foo: hello

Nginx 變量的創(chuàng)建只能發(fā)生在 Nginx 配置加載的時候,或者說 Nginx 啟動的時候。

而賦值操作則只會發(fā)生在請求實際處理的時候。這意味著不創(chuàng)建變量,直接使用變量會導致啟動失敗。

2.2. 內(nèi)置預定義變量

內(nèi)置預定義變量即無需聲明就可以使用的變量,通常包括一個 http 請求或響應中一部分內(nèi)容的值,以下為一些常用的內(nèi)置預定義變量

變量名定義
$arg_PARAMETERGET請求中變量名PARAMETER參數(shù)的值。
$args這個變量等于GET請求中的參數(shù)。例如,foo=123&bar=blahblah;這個變量只可以被修改。
$binary_remote_addr二進制碼形式的客戶端地址。
$body_bytes_sent傳送頁面的字節(jié)數(shù)。
$content_length請求頭中的Content-length字段。
$content_type請求頭中的Content-Type字段。
$cookie_COOKIEcookie COOKIE的值。
$document_root當前請求在root指令中指定的值。
$document_uri與$uri相同。
$host請求中的主機頭(Host)字段,如果請求中的主機頭不可用或者空,則為處理請求的server名稱(處理請求的server的server_name指令的值)。值為小寫,不包含端口。
$hostname機器名使用 gethostname系統(tǒng)調(diào)用的值
$http_HEADERHTTP請求頭中的內(nèi)容,HEADER為HTTP請求中的內(nèi)容轉(zhuǎn)為小寫,-變?yōu)開(破折號變?yōu)橄聞澗€),例如:$http_user_agent(Uaer-Agent的值)。
$sent_http_HEADERHTTP響應頭中的內(nèi)容,HEADER為HTTP響應中的內(nèi)容轉(zhuǎn)為小寫,-變?yōu)開(破折號變?yōu)橄聞澗€),例如: $sent_http_cache_control, $sent_http_content_type…;
$is_args如果$args設置,值為"?“,否則為”"。
$limit_rate這個變量可以限制連接速率。
$nginx_version當前運行的nginx版本號。
$query_string與$args相同。
$remote_addr客戶端的IP地址。
$remote_port客戶端的端口。
$remote_user已經(jīng)經(jīng)過Auth Basic Module驗證的用戶名。
$request_filename當前連接請求的文件路徑,由root或alias指令與URI請求生成。
$request_body這個變量(0.7.58+)包含請求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比較有意義。
$request_body_file客戶端請求主體信息的臨時文件名。
$request_completion如果請求成功,設為"OK";如果請求未完成或者不是一系列請求中最后一部分則設為空。
$request_method這個變量是客戶端請求的動作,通常為GET或POST。包括0.8.20及之前的版本中,這個變量總為main request中的動作,如果當前請求是一個子請求,并不使用這個當前請求的動作。
$request_uri這個變量等于包含一些客戶端請求參數(shù)的原始URI,它無法修改,請查看$uri更改或重寫URI。
$scheme所用的協(xié)議,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect。
$server_addr服務器地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值,如果要繞開系統(tǒng)調(diào)用,則必須在listen中指定地址并且使用bind參數(shù)。
$server_name服務器名稱。
$server_port請求到達服務器的端口號。
$server_protocol請求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1。
$uri請求中的當前URI(不帶請求參數(shù),參數(shù)位于args),不同于瀏覽器傳遞的args),不同于瀏覽器傳遞的args),不同于瀏覽器傳遞的request_uri的值,它可以通過內(nèi)部重定向,或者使用index指令進行修改。不包括協(xié)議和主機名,例如/foo/bar.html

3. Nginx alias 虛擬目錄(拓展)

  • root:為 location 指定網(wǎng)站的根目錄。location 里定義的子路徑是跟這個根目錄相對的。
  • alias:為 location 指定服務的具體文件路徑。location 里定義的子路徑會被忽略 

使用alias標簽的目錄塊中不能使用 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時實際上訪問是/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,實際的路徑就是:root值 + location值。root會將完整的url映射進文件路徑,root可以看成是一個相對路徑,訪問url網(wǎng)址時,http://www.baidu.com/test,這個test必須要在/usr/share/nginx/html下有這個目錄,目錄中必須要這個文件才行。
  • 使用alias,實際的路徑就是:alias值。alias只會將localhost后的url映射到文件路徑。而使用alias時,訪問http://www.baidu.com/test,在/usr/share/nginx/html不需要有這個test目錄即可。
  • alias只能位于location塊中,root可以放在http,server,location塊中

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx安裝nginx-rtmp-module模塊的實現(xiàn)

    Nginx安裝nginx-rtmp-module模塊的實現(xiàn)

    nginx-rtmp-module是一個用于Nginx的第三方模塊,它使Nginx能夠支持實時多媒體流的傳輸和處理,本文主要介紹了Nginx安裝nginx-rtmp-module模塊,具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • 詳解nginx前端根據(jù)$remote_addr分發(fā)方法

    詳解nginx前端根據(jù)$remote_addr分發(fā)方法

    這篇文章主要介紹了詳解nginx前端根據(jù)$remote_addr分發(fā)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • Nginx部署項目上傳文件報錯413的解決方法

    Nginx部署項目上傳文件報錯413的解決方法

    本文主要介紹了Nginx部署項目上傳文件報錯413的解決方法,報錯413是因為Nginx對上傳大小做了限制,所以我們需要配置文件,下面就來解決這個問題,感興趣的可以了解一下
    2024-03-03
  • Nginx?請求超時的實現(xiàn)

    Nginx?請求超時的實現(xiàn)

    Nginx請求超時是服務器無法在規(guī)定時間內(nèi)完成對客戶端請求的響應,本文就來介紹一下Nginx?請求超時的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • Nginx日志中request_time和upstream_response_time區(qū)別

    Nginx日志中request_time和upstream_response_time區(qū)別

    Nginx日志中的request_time和upstream_response_time是關(guān)鍵的性能指標,本文就來介紹一下Nginx日志中request_time和upstream_response_time區(qū)別,具有一定的參考價值,感興趣的可以了解一下
    2024-11-11
  • keepalived監(jiān)控nginx進程的實現(xiàn)示例

    keepalived監(jiān)控nginx進程的實現(xiàn)示例

    本文主要介紹了keepalived監(jiān)控nginx進程的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-08-08
  • Nginx 緩存系統(tǒng) proxy_cache工作原理解析

    Nginx 緩存系統(tǒng) proxy_cache工作原理解析

    Nginx 的 proxy_cache 模塊允許 Nginx 作為反向代理服務器時緩存后端服務器的響應,本文給大家介紹Nginx 緩存系統(tǒng) proxy_cache的工作原理,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • 利用nginx解決cookie跨域訪問的方法

    利用nginx解決cookie跨域訪問的方法

    本篇文章主要介紹了利用nginx解決cookie跨域訪問的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • NGINX配置目錄遍歷漏洞的解決

    NGINX配置目錄遍歷漏洞的解決

    目錄遍歷是一種安全漏洞,通常會影響Nginx服務器上的Web應用程序,本文就來介紹一下NGINX配置目錄遍歷漏洞的解決,感興趣的可以了解一下
    2023-10-10
  • nginx虛擬主機防webshell完美版

    nginx虛擬主機防webshell完美版

    nginx虛擬主機防webshell完美版,使用nginx的朋友可以參考下。
    2010-11-11

最新評論