nginx常见内置变量$uri和$request_uri的使用

 更新时间:2024年07月07日 15:16:41   作者:gushaolin  
本文主要介绍了nginx常见内置变量$uri和$request_uri的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

这里介绍nginx常见内置变量$uri和$request_uri代表的值,首先先看nginx配置:

[root@CentOS7-2 conf.d]# cat /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
       # index  index.html index.htm;
           index  jiade.html index.html;
                  }
    location /test {
       root /usr/share/nginx/html;
          index test.html;
              }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
                        }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

配置log形式和保存路径:

[root@CentOS7-2 nginx]# cat /etc/nginx/nginx.conf 
user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    #配置nginx使用epoll I/O模型
     use epoll ;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$uri --- $request_uri -- $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

关停nginx:nginx -s stop
启动nginx:nginx

分析过程:通过nginx日志来分析得出$uri和$request_uri值

[root@CentOS7-2 nginx]# tail -200f  /var/log/nginx/access.log
/test/test.html --- /test/ -- 192.168.128.1 - - [31/Dec/2019:08:54:43 +0800] "GET /test/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
/favicon.ico --- /favicon.ico -- 192.168.128.1 - - [31/Dec/2019:08:57:16 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" "-"
/jiade.html --- / -- 192.168.128.1 - - [31/Dec/2019:08:57:18 +0800] "GET / HTTP/1.1" 200 6 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
/favicon.ico --- /favicon.ico -- 192.168.128.1 - - [31/Dec/2019:08:57:19 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)" "-"
/res --- /res -- 192.168.128.1 - - [31/Dec/2019:08:58:34 +0800] "GET /res HTTP/1.1" 404 153 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
/favicon.ico --- /favicon.ico -- 192.168.128.1 - - [31/Dec/2019:08:58:34 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)" "-"
/jiade.html --- / -- 192.168.128.1 - - [31/Dec/2019:09:02:09 +0800] "GET / HTTP/1.1" 200 6 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
/favicon.ico --- /favicon.ico -- 192.168.128.1 - - [31/Dec/2019:09:02:09 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0)" "-"

案例1:
访问:http://192.168.128.137/test/
$uri:/test/test.html
$request_uri:/test/

案例2:
访问:http://192.168.128.137/
$uri:/jiade.html
$request_uri:/

案例3(真实名字服务器上不存在res目录):
访问:http://192.168.128.137/res
$uri:/res
$request_uri:/res
从上面三个案例就可以得出$uri和$request_uri所代表的值。

到此这篇关于nginx常见内置变量$uri和$request_uri的使用的文章就介绍到这了,更多相关nginx $uri和$request_uri内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • nginx 隐藏版本号与WEB服务器信息的解决方法

    nginx 隐藏版本号与WEB服务器信息的解决方法

    这篇文章主要介绍了nginx 隐藏版本号与WEB服务器信息的解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • nginx反向代理服务因配置文件错误导致访问资源时出现404

    nginx反向代理服务因配置文件错误导致访问资源时出现404

    这篇文章主要介绍了nginx反向代理服务因配置文件错误导致访问资源时出现404,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Nginx代理proxy pass配置去除前缀的实现

    Nginx代理proxy pass配置去除前缀的实现

    这篇文章主要介绍了Nginx代理proxy pass配置去除前缀的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Nginx中Map模块的具体使用

    Nginx中Map模块的具体使用

    Nginx的map模块是一个功能强大的工具,可以在配置Nginx时实现更高效的请求处理,本文主要介绍了Nginx中Map模块的具体使用,具有一定的参考价值,感兴趣的可以了解一下
    2024-08-08
  • Nginx代理WebSocket失败的完整排查过程

    Nginx代理WebSocket失败的完整排查过程

    在开发基于 WebSocket 的实时应用时,使用 Nginx 作为反向代理是常见做法,可是我遇见了后端直连 WebSocket 成功,通过 Nginx 代理却失败,本文将完整复盘一次真实排查过程,带你避开所有坑,需要的朋友可以参考下
    2025-11-11
  • nginx 开启 pathinfo的过程详解

    nginx 开启 pathinfo的过程详解

    这篇文章主要介绍了nginx 开启 pathinfo的过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • nginx status状态页配置方法和中文说明

    nginx status状态页配置方法和中文说明

    这篇文章主要介绍了nginx status状态页配置方法和中文说明,重点在配置例子和status的中文说明,需要的朋友可以参考下
    2014-06-06
  • 隐藏Nginx版本号的方法小结

    隐藏Nginx版本号的方法小结

    默认情况下,Nginx 会在响应头里暴露版本号信息,这无疑给潜在的攻击者提供了便利,毕竟,知道了版本号,就可以针对性地寻找漏洞进行攻击,为了让自己的服务器更安全,本文给大家介绍了如何隐藏 Nginx 的版本号,需要的朋友可以参考下
    2025-02-02
  • Nginx离线安装的保姆级教程

    Nginx离线安装的保姆级教程

    为了确保gcc-c++和Nginx的正确安装,建议使用yum工具在线下载rpm包,避免手动下载安装时出现依赖缺失的问题,本文就来介绍一下如何离线安装,感兴趣的可以了解一下
    2024-09-09
  • Nginx下SSL证书安装部署步骤介绍

    Nginx下SSL证书安装部署步骤介绍

    大家好,本篇文章主要讲的是Nginx下SSL证书安装部署步骤介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下哦,方便下次浏览
    2021-12-12

最新评论