duanpie4763 2013-11-05 11:16
浏览 68
已采纳

nginx中的不同位置指令

I am new in ngnix and I don't understand very well the location directive. I have a website with the folloging configuration:

location / {
    rewrite ^(.*)$ /index.php last;
}

#assets

location /web/assets/ {
    rewrite ^(/web/assets/.*)$ $1 break;
}

location /web/assets/cache/ {
    if (!-f $request_filename) {
        rewrite ^/web/assets/cache/(.*)$ /web/assets/cache/index.php last;
    }
}

In the website, all requests are redirected to index.php, but there is an "assets" folder that I don't want to redirect (/web/assets/). Inside this folder there is a subfolder called "cache". If any file inside this subfolder is requested and the file doesn't exist, the request is redirected to a php file that create the file and save it in cache. This is useful for example for preprocessed css, js, etc, the files are created the first time they are required.

This configuration works well, but I'd like to send some headers to assets files, according to html5 boilderplate suggestions, for example expires rules for static content (https://github.com/h5bp/server-configs-nginx/blob/master/conf/expires.conf), and when I add these directives:

location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}

The previous redirection doesn't work. I guest it's because nginx doesn't execute all matches locations but only the first one. My question is how to combine rewrite and header directives in ngnix config.

  • 写回答

1条回答 默认 最新

  • dougui1977 2013-11-05 11:54
    关注

    Each request can only be handled by one location block. Also, the usage of if is not a good practice. try_files can be much more efficient. Also, you have a rewrite rule that rewrites to the same uri (totally useless).

    Allow me to rewrite your conf to what I believe is more efficient for your needs and please lt me know if I got something wrong

    #this is just fine as it was
    location / {
      rewrite ^(.*)$ /index.php last;
    }
    
    #web assets should be served directly
    location /web/assets/ {
      try_files $uri $uri/ @mycache;
    }
    
    #this is the mycache location, called when assets are not found
    location @mycache {
      expires 1y;
      access_log on;
      add_header Cache-Control "public";
      rewrite ^/web/assets/(.*)$ /web/assets/cache/index.php last;
    }
    
    #some specific files in the web/assets directory. if this matches, it is preferred over the location web/assets because it is more specific
    location ~* /web/assets/.*\.(jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
      expires 1M;
      access_log off;
      add_header Cache-Control "public";
      try_files $uri $uri/ @mycache;
    }
    
    # CSS and Javascript
    location ~* /web/assets/.*\.(css|js)$ {
      expires 1y;
      access_log off;
      add_header Cache-Control "public";
      try_files $uri $uri/ @mycache;
    }
    

    I may have typos or errors, I don't have means to test right now. Let me know

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路