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