【问题标题】:How to redirect nginx location to different rule?如何将 nginx 位置重定向到不同的规则?
【发布时间】:2020-10-31 01:24:36
【问题描述】:

我正在使用 NGINX 并试图将所有具有子目录 FusionChart 的请求转到特殊位置,我的意图是所有带有 [ROOT_URL]/FusionChart/ 的 url 都应该转到下面的 # Rule 3。 但是,我有一个现有的 nginx 规则规定所有静态内容都应该转到# Rule 2

Nginx 配置:

server {
 listen       80;
 server_name  domain2.com www.domain2.com;
 access_log   logs/domain2.access.log  main;
 
# Rule 1
 location ~ ^/(images|javascript|js|css|flash|media|static)/
 {
  root    /var/www/virtual/big.server.com/htdocs;
  expires 30d;
 }
 
# Rule 2
 location ~* \.(js|css|png|jpg|jpeg|gif|jqGrid|images|common|ico|map|woff|woff2|ttf|html)$ {
   root /home/rcp/dev/public/others;
   expires 10y;
 }

 # Rule 3
 location ~ ^/(FusionCharts)/ {
                root /home/rcp/dev/public/charts;
                expires 10y;
 }

 location / {
  proxy_pass      http://127.0.0.1:8080;
 }
}

经过测试的网址:

http://domain2.com/FusionCharts/index.html

这会落到# Rule 2,如何修改规则,让上面的请求落到# Rule 3

【问题讨论】:

    标签: regex nginx nginx-location


    【解决方案1】:

    从头到尾检查正则表达式匹配位置,找到的第一个匹配用于请求处理。您可以交换第二个和第三个位置或使用location ^~ { ... } 语法(查看documentation 了解详细信息):

    location ^~ /FusionCharts/ {
        root /home/rcp/dev/public/charts/;
        expires 10y;
    }
    

    请注意,/FusionCharts/index.html 请求的上述位置index.html 文件将在/home/rcp/dev/public/charts/FusionCharts 目录下搜索。如果这不是所需的行为,请使用 alias /home/rcp/dev/public/charts/; 指令而不是 root 一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-02
      • 2015-12-22
      • 2018-12-20
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 2019-01-19
      相关资源
      最近更新 更多