Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Nginx location 配置后路径映射问题(路径替换)

location proxy_pass

Nginx配置location proxy_pass时可以实现URL路径的部分替换。

(1)proxy_pass的目标地址,默认不带/,表示只代理域名,urlquerystring部分不会变(把请求的path拼接到proxy_pass目标域名之后作为代理的URL)

(2)如果在目标地址后增加/,则表示把pathlocation匹配成功的部分剪切掉之后再拼接到proxy_pass目标地址

例子:

server {
    location /abc {
        proxy_pass http://server_url;
    }

    location /abc {
        proxy_pass http://server_url/;
    }
}

比如请求/abc/b.html

如上两个匹配成功后,实际代理的目标url分别是

  • http://server_url/abc/b.html (把/abc/b.html拼接到http://server_url之后)
  • http://server_url/b.html (把/abc/b.html/abc去掉之后,拼接到http://server_url/之后)

proxy_pass 测试

#--------proxy_pass配置---------------------
location /t1/ { proxy_pass http://servers; }    #正常,不截断
location /t2/ { proxy_pass http://servers/; }    #正常,截断
location /t3  { proxy_pass http://servers; }    #正常,不截断
location /t4  { proxy_pass http://servers/; }    #正常,截断
location /t5/ { proxy_pass http://servers/test/; }    #正常,截断
location /t6/ { proxy_pass http://servers/test; }    #缺"/",截断
location /t7  { proxy_pass http://servers/test/; }    #含"//",截断
location /t8  { proxy_pass http://servers/test; }    #正常,截断

测试脚本

for i in $(seq 8)
do
    url=http://tapi.xxxx.com/t$i/doc/index.html
    echo "-----------$url-----------"
    curl url
done

测试结果

----------http://tapi.xxxx.com/t1/doc/index.html------------
/t1/doc/index.html

----------http://tapi.xxxx.com/t2/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t3/doc/index.html------------
/t3/doc/index.html

----------http://tapi.xxxx.com/t4/doc/index.html------------
/doc/index.html

----------http://tapi.xxxx.com/t5/doc/index.html------------
/test/doc/index.html

----------http://tapi.xxxx.com/t6/doc/index.html------------
/testdoc/index.html

----------http://tapi.xxxx.com/t7/doc/index.html------------
/test//doc/index.html

----------http://tapi.xxxx.com/t8/doc/index.html------------
/test/doc/index.html

注意事项

若截断替换后的URL中包含//,可能会报如下错误:

[org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]] [log] [175] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL was not normalized.
上一篇 修改PHP和Nginx文件上传大小限制
下一篇 Linux的生成.ssh公私钥