andybest
V2EX  ›  问与答

Nginx 如何优雅的实现这个需求?

  •  
  •   andybest · Jun 16, 2015 · 3662 views
    This topic created in 4027 days ago, the information mentioned may be changed or developed.
    访问 URL: http://localhost/abc.jpg
    如果静态资源 abc.jpg 图片不存在,则转至: http://localhost:91/create.php?pic=abc.jpg

    create.php 会生成 abc.jpg 并直接输出该图片
    (所以下次访问会直接访问静态资源 abc.jpg 而不会再去请求 create.php)

    Nginx 中如何实现这个需求?如果可能请简单写下配置方案,谢谢!
    20 replies    2015-06-17 13:29:53 +08:00
    sophymax
        1
    sophymax  
       Jun 16, 2015 via iPad
    openresty
    ksword
        2
    ksword  
       Jun 16, 2015   ❤️ 1
    可以试下try_files $uri /create.php?pic=$uri =404;
    ryd994
        4
    ryd994  
       Jun 16, 2015 via Android   ❤️ 1
    其实想缓存的话并不需要这样
    动态页面直接输出不管创建文件的事
    利用fastcgi_store来保存
    利用try_files来检查

    http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_store

    当然,如果你需要的只是对热页面的缓存的话,fastcgi_cache会好用得多
    pupboss
        5
    pupboss  
       Jun 16, 2015
    rewrite ^/(.*.jpg)$ :91/create.php?pic=$1 permanent;
    cevincheung
        6
    cevincheung  
       Jun 16, 2015
    难道这不是个rewrite完成的工作么?
    ryd994
        7
    ryd994  
       Jun 17, 2015   ❤️ 1
    @cevincheung
    @pupboss
    明明就是个静态化缓存,用什么rewrite
    LazyZhu
        8
    LazyZhu  
       Jun 17, 2015   ❤️ 1
    这个只需要try_files配合一个命名location来proxy_pass就可以了:

    https://gist.github.com/LazyZhu/2cbfbeea75eae63a450e

    具体参数自己修正.
    gs038538
        9
    gs038538  
       Jun 17, 2015
    rewrite
    重定向应该可以的。
    andybest
        10
    andybest  
    OP
       Jun 17, 2015
    @pupboss @cevincheung

    rewrite 似乎无法实现当静态资源存在的时候访问静态资源,而是永远请求 create.php ,不符合需求啊似乎
    andybest
        11
    andybest  
    OP
       Jun 17, 2015
    @gamexg 非常感谢,请问 if (!-f $request_filename) 中的 !-f 是什么意思?
    gamexg
        12
    gamexg  
       Jun 17, 2015   ❤️ 1
    msg7086
        13
    msg7086  
       Jun 17, 2015   ❤️ 1
    就是try_files嘛。要复杂的实现可以写lua。
    我之前写过一个论坛头像CDN就是用lua比对本地和远程服务器上的写入时间,本地新就返回本地,本地和远程一样就重定向到远程CDN去。
    jasontse
        14
    jasontse  
       Jun 17, 2015 via iPad   ❤️ 1
    @andybest
    if not file
    pupboss
        15
    pupboss  
       Jun 17, 2015   ❤️ 1
    @andybest 你先判断一个404,再重定向也行

    自己的项目没用过静态缓存,所以不太清楚楼上说的
    ryd994
        17
    ryd994  
       Jun 17, 2015   ❤️ 1
    @cevincheung if很好玩么?if is evil,更别说用if判断文件存在了,
    早就该用try_files代替的东西,各种文档吐槽无数遍,偏有半桶水: http://wiki.nginx.org/Pitfalls#Check_IF_File_Exists

    fastcgi_cache 或 fastcgi_store + try_files,根据需求使用即可
    ryd994
        18
    ryd994  
       Jun 17, 2015   ❤️ 1
    @LazyZhu 配合proxy_store食用,效果更佳
    ryd994
        19
    ryd994  
       Jun 17, 2015   ❤️ 1
    https://gist.github.com/renyidong/f93ca0a3beed67abe02a

    两种风格任选
    proxy_cache 部分在内存,性能更好,适合比较简单的缓存规则
    proxy_store 读的是硬盘,而且要自己删文件来实现过期,但规则可以更复杂

    如果是fcgi的话就用换成fcgi的指令,反正用法基本一样的
    andybest
        20
    andybest  
    OP
       Jun 17, 2015
    @ryd994 感谢!这的确是最优雅并且性能最佳的解决方案 :)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1213 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 17:36 · PVG 01:36 · LAX 10:36 · JFK 13:36
    ♥ Do have faith in what you're doing.