nginx 别名与位置正则表达式获取错误的文件名

nginx 别名与位置正则表达式获取错误的文件名

我的位置配置:

location ~ ^/maat/(js|css|images)/ {
       alias /usr/local/services/gdt-frontend-test-1.0/maat/$1/;
       expires  1y;
    }

完整的服务器配置:

server {
    listen 80;
    server_name audit2.test.qq.com;
    charset utf-8;
    
    log_not_found on;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    access_log /usr/local/services/tnginx_1_0_0-1.0/log/audit2_access.log;
    error_log /usr/local/services/tnginx_1_0_0-1.0/log/audit2_error.log debug;
    #rewrite_log on;

    location ^~ /maat/api/ {
            proxy_pass http://localhost:8351/maat/api/;
    }

    location ^~ /maat/mo/ {
            proxy_pass http://localhost:8351/maat/mo/;
    }

    location ^~ /maat/logOut {
            proxy_pass http://localhost:8351/maat/logOut;
    }
    
    location ^~ /maat/reviewStatusReuse/ {
            proxy_pass http://localhost:8351/maat/reviewStatusReuse/;
    } 


    #location ^~ /maat/ {
     #   proxy_pass http://localhost:8765;
    #}

    
    location ~ ^/maat/(js|css|images)/ {
       alias /usr/local/services/gdt-frontend-test-1.0/maat/$1/;
       expires  1y;
    }

    location /maat/ {
        root /usr/local/services/gdt-frontend-test-1.0/;
        add_header Cache-Control no-cache;
        add_header Cache-Control private;        
    }

}

当我请求 http://xxxx/maat/js/entry_xxx.js 时,我得到了 301,位置为:http://xxxx/maat/js/entry_xxx.js/

调试日志如下:

2021/02/22 10:57:14 [debug] 3423#0: *19 http header: "Host: audit2.test.qq.com"
2021/02/22 10:57:14 [debug] 3423#0: *19 http header: "Connection: keep-alive"
2021/02/22 10:57:14 [debug] 3423#0: *19 http header: "Accept-Encoding: gzip"
2021/02/22 10:57:14 [debug] 3423#0: *19 http header done
2021/02/22 10:57:14 [debug] 3423#0: *19 event timer del: 531: 28164571712
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 __mydebug_menshen. ngx_http_menshen_handler is 
called r: 00000000029F9630 nginx_version: 1016001
2021/02/22 10:57:14 [debug] 3423#0: *19 <1>status: MENSHEN_STATUS_CTX_CREATE_INIT. uri: 
/maat/js/entry_e731dc7.js. args:  r: 00000000029F9630 r->main: 00000000029F9630 r->count: 1
2021/02/22 10:57:14 [debug] 3423#0: *19 http cleanup add: 00000000029FA4E0
2021/02/22 10:57:14 [debug] 3423#0: *19 server menshen_module: -1 0000000002A56378
2021/02/22 10:57:14 [debug] 3423#0: *19 host and cgi not match ,ptr_conf_rule null
2021/02/22 10:57:14 [debug] 3423#0: *19 rewrite phase: 2
2021/02/22 10:57:14 [debug] 3423#0: *19 rewrite phase: 3
2021/02/22 10:57:14 [debug] 3423#0: *19 test location: "/maat/"
2021/02/22 10:57:14 [debug] 3423#0: *19 test location: "mo/"
2021/02/22 10:57:14 [debug] 3423#0: *19 test location: "logOut"
2021/02/22 10:57:14 [debug] 3423#0: *19 test location: "api/"
2021/02/22 10:57:14 [debug] 3423#0: *19 test location: ~ "^/maat/(js|css|images)/"
2021/02/22 10:57:14 [debug] 3423#0: *19 using configuration "^/maat/(js|css|images)/"
2021/02/22 10:57:14 [debug] 3423#0: *19 http cl:-1 max:8388608
2021/02/22 10:57:14 [debug] 3423#0: *19 rewrite phase: 5
2021/02/22 10:57:14 [debug] 3423#0: *19 rewrite phase: 6
2021/02/22 10:57:14 [debug] 3423#0: *19 post rewrite phase: 7
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 8
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 9
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 10
2021/02/22 10:57:14 [debug] 3423#0: *19 access phase: 11
2021/02/22 10:57:14 [debug] 3423#0: *19 access phase: 12
2021/02/22 10:57:14 [debug] 3423#0: *19 access phase: 13
2021/02/22 10:57:14 [debug] 3423#0: *19 post access phase: 14
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 15
2021/02/22 10:57:14 [debug] 3423#0: *19 generic phase: 16
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 17
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 18
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 19
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 20
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 21
2021/02/22 10:57:14 [debug] 3423#0: *19 content phase: 22
2021/02/22 10:57:14 [debug] 3423#0: *19 http script copy: "/usr/local/services/gdt-frontend-test-1.0/maat/"
2021/02/22 10:57:14 [debug] 3423#0: *19 http script capture: "js"
2021/02/22 10:57:14 [debug] 3423#0: *19 http script copy: "/"
2021/02/22 10:57:14 [debug] 3423#0: *19 http filename: "/usr/local/services/gdt-frontend-test-1.0/maat/js/"
2021/02/22 10:57:14 [debug] 3423#0: *19 add cleanup: 00000000029FA550
2021/02/22 10:57:14 [debug] 3423#0: *19 http static fd: -1
2021/02/22 10:57:14 [debug] 3423#0: *19 http dir
2021/02/22 10:57:14 [debug] 3423#0: *19 http finalize request: 301, "/maat/js/entry_e731dc7.js?" a:1, c:1
2021/02/22 10:57:14 [debug] 3423#0: *19 http special response: 301, "/maat/js/entry_e731dc7.js?"
2021/02/22 10:57:14 [debug] 3423#0: *19 http set discard body
2021/02/22 10:57:14 [debug] 3423#0: *19 charset: "" > "utf-8"
2021/02/22 10:57:14 [debug] 3423#0: *19 HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 22 Feb 2021 02:57:14 GMT
Content-Type: text/html
Content-Length: 162
Location: http://audit2.test.qq.com/maat/js/entry_e731dc7.js/
Connection: close
Expires: Tue, 22 Feb 2022 02:57:14 GMT
Cache-Control: max-age=31536000

2021/02/22 10:57:14 [debug] 3423#0: *19 write new buf t:1 f:0 0000000002B4F7D0, pos     0000000002B4F7D0, size: 287 file: 0, size: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 http write filter: l:0 f:0 s:287
2021/02/22 10:57:14 [debug] 3423#0: *19 http output filter "/maat/js/entry_e731dc7.js?"
2021/02/22 10:57:14 [debug] 3423#0: *19 http copy filter: "/maat/js/entry_e731dc7.js?"
2021/02/22 10:57:14 [debug] 3423#0: *19 http postpone filter "/maat/js/entry_e731dc7.js?" 0000000002B4FA10
2021/02/22 10:57:14 [debug] 3423#0: *19 write old buf t:1 f:0 0000000002B4F7D0, pos     0000000002B4F7D0, size: 287 file: 0, size: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 write new buf t:0 f:0 0000000000000000, pos 0000000000B97440, size: 116 file: 0, size: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 write new buf t:0 f:0 0000000000000000, pos 0000000000B97200, size: 46 file: 0, size: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 http write filter: l:1 f:0 s:449
2021/02/22 10:57:14 [debug] 3423#0: *19 http write filter limit 0
2021/02/22 10:57:14 [debug] 3423#0: *19 writev: 449 of 449
2021/02/22 10:57:14 [debug] 3423#0: *19 http write filter 0000000000000000
2021/02/22 10:57:14 [debug] 3423#0: *19 http copy filter: 0 "/maat/js/entry_e731dc7.js?"
2021/02/22 10:57:14 [debug] 3423#0: *19 http finalize request: 0, "/maat/js/entry_e731dc7.js?" a:1, c:1
2021/02/22 10:57:14 [debug] 3423#0: *19 http request count:1 blk:0
2021/02/22 10:57:14 [debug] 3423#0: *19 http close request
2021/02/22 10:57:14 [debug] 3423#0: *19 __mydebug. menshen cleanup r: 00000000029F9630
2021/02/22 10:57:14 [debug] 3423#0: *19 http log handler
2021/02/22 10:57:14 [debug] 3423#0: *19 http monitor handler
2021/02/22 10:57:14 [debug] 3423#0: *19 free: 00000000029F95E0, unused: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 free: 0000000002B4F380, unused: 2096
2021/02/22 10:57:14 [debug] 3423#0: *19 close http connection: 531
2021/02/22 10:57:14 [debug] 3423#0: *19 reusable connection: 0
2021/02/22 10:57:14 [debug] 3423#0: *19 free: 0000000002B6F400
2021/02/22 10:57:14 [debug] 3423#0: *19 free: 0000000002C032C0, unused: 136
2021/02/22 10:57:14 [debug] 3429#0: *20 http header: "Host: audit2.test.qq.com"
2021/02/22 10:57:14 [debug] 3429#0: *20 http header: "Connection: keep-alive"
2021/02/22 10:57:14 [debug] 3429#0: *20 http header: "Accept-Encoding: gzip"
2021/02/22 10:57:14 [debug] 3429#0: *20 http header done
2021/02/22 10:57:14 [debug] 3429#0: *20 event timer del: 22: 28164571836
2021/02/22 10:57:14 [debug] 3429#0: *20 generic phase: 0
2021/02/22 10:57:14 [debug] 3429#0: *20 __mydebug_menshen. ngx_http_menshen_handler is called r: 00000000029F9630 nginx_version: 1016001
2021/02/22 10:57:14 [debug] 3429#0: *20 <1>status: MENSHEN_STATUS_CTX_CREATE_INIT. uri: /maat/js/entry_e731dc7.js/. args:  r: 00000000029F9630 r->main: 00000000029F9630 r->count: 1
2021/02/22 10:57:14 [debug] 3429#0: *20 http cleanup add: 00000000029FA4E0
2021/02/22 10:57:14 [debug] 3429#0: *20 server menshen_module: -1 0000000002A56378

http 文件名:“/usr/local/services/gdt-frontend-test-1.0/maat/js/”为什么 http 文件名是别名路径?

答案1

您的语句无效。但在这种情况下alias您不应该使用。请使用。例如:aliasroot

location ~ ^/maat/(js|css|images)/ {
    root /usr/local/services/gdt-frontend-test-1.0/;
    expires  1y;
}

您可以将root语句移到块中server,以便两个块都继承相同的值location


声明alias中的正则表达式 location块需要提供文件的完整路径。这是通过在正则表达式中捕获 URI 的完整尾部来实现的。例如:

location ~ ^/maat/((js|css|images)/.*)$ {
    alias /usr/local/services/gdt-frontend-test-1.0/maat/$1;
    expires  1y;
}

这个文件了解详情。

相关内容