这里介绍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所代表的值。

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐