67 Nginx的rewrite之set指令

67 Nginx的rewrite之set指令

67.1 set

set:设置新的变量

语法 set $variable value;
默认值 -
位置 server 、location 、 if

 

 

 

 

variable:变量的名称,该变量名称要用 "$" 作为变量的第一个字符,且不要与 Nginx 服务预设的全局变量名重名

value:变量的值,字符串、其他变量或变量组合等

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age";
          root html;
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server

image

67.2 Rewrite常用全局变量

 

   
   
   
   
   
   
   
   

 

 

 

 

 

 

 

 

67.3 args

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age=$args";
          root html;
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server?username=jerry&gender=1,?后面的参数,被args获取

image

67.4 http_user_agent

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age=$args=$http_user_agent";
          root html;
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server?genders=1 ,http_user_agent获取到了浏览器信息

image

67.5 host

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age=$args=$http_user_agent=$host";
          root html;
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server?genders=1 ,host 获取到了服务器IP

image

67.6 document_uri

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          root /usr/local/nginx/uri;
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age=$args=$http_user_agent=$host=$document_root";
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server?genders=1 ,document_uri 获取到了 root 配置路径

image

67.7 log_format

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    
    log_format main '$remote_addr - $request - $status - $request_uri - $http_user_agent';   

    # rewrite
    server {
        listen 8081;
        server_name localhost;
        location /server {
          root /usr/local/nginx/uri;
          access_log logs/access.log main;
          set $name Tom;
          set $age 18;
          default_type text/plain;
          return 200 "$name=$age=$args=$http_user_agent=$host=$document_root";
        }
    

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }  
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
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
View Code

浏览器访问:http://10.0.0.100:8081/server?genders=1 ,log_format 日志会记录对应信息

log_format main '$remote_addr - $request - $status - $request_uri - $http_user_agent';
[root@nginx-100 /usr/local/nginx/conf]# tail -f ../logs/access.log 
10.0.0.1 - GET /server HTTP/1.1 - 200 - /server - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
10.0.0.1 - GET /server HTTP/1.1 - 200 - /server - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-19 17:20  马俊南  阅读(6)  评论(0)    收藏  举报