How to serve all existing static files directly with NGINX, but proxy the rest to a backend server.

后端 未结 3 870
臣服心动
臣服心动 2020-11-28 00:47
location / {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    if          


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 01:44

    If you use mod_rewrite to hide the extension of your scripts, or if you just like pretty URLs that end in /, then you might want to approach this from the other direction. Tell nginx to let anything with a non-static extension to go through to apache. For example:

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$
    {
        root   /path/to/static-content;
    }
    
    location ~* ^!.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
        if (!-f $request_filename) {
            return 404;
        }
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8080;
    }
    

    I found the first part of this snippet over at: http://code.google.com/p/scalr/wiki/NginxStatic

提交回复
热议问题