upstream app {
server localhost:3000;
}
server {
...
# If I comment this location out, images are displayed on the website
location ~* \.(?:jpg|jpeg|png|gif|swf|xml|txt|css|js)${
expires 6004800;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
...
location /app {
alias /path/to/app/public/;
try_files $uri $uri @app;
}
location @app {
rewrite /app(.*) $1 break;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $proxy_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app;
proxy_redirect http://app/ /app/;
}
...
}
我有一段时间在努力奋斗.我在nginx下的子文件夹中有一个快速应用程序.上面是/ sites-available /中我的nginx文件中的代码.当我删除静态文件的位置时,应用程序的图像和css被代理,但如果静态文件缓存在nginx文件中,那么express应用程序的图像和css文件不会显示在网站上.
有人可以帮帮忙吗?
解决方法:
正则表达式位置优先于nginx请求处理中的前缀位置块.以下是nginx的location directive documentation的相关摘录.
我强烈建议你仔细阅读,因为许多人不这样做,并错过了基础知识.
在理解关键字之前的几个例子:
>前缀位置:location / toto {[…]}
>正则表达式位置:location~ / toto {[…]}
[ … ]
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
[ … ]
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
[ … ]
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. […]
一些其他示例来说明修改位置查找顺序的两个运算符:
> location ^〜/ toto {[…]}:优先级高于正则表达位置的前缀位置
> location = / toto {[…]}:确切的前缀位置(完全匹配,最高优先级)
总而言之,传入请求URI的位置选择期间的优先级列表是:
> location = / too
>位置^〜/ toto
> location~ / toto
> location / toto
因此,解决您的问题的更简洁方法是使用:
location ^~ /app {
alias /path/to/app/public/;
try_files $uri $uri @app;
}
原文地址:https://codeday.me/bug/20190624/1276625.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。