服务器 > 服务器 > Nginx

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

59人参与 2024-09-08 Nginx

这里介绍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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

Nginx配置多个端口进行监听的实现

09-08

windows查看nginx是否启动的四种方法

09-08

docker运行Nginx及配置方法

09-08

Nginx配置及热升级的详细介绍

09-08

Nginx超时时间的配置说明

09-08

Nginx访问控制的原理及实现

09-08

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论