如何直接使用NGINX服务所有现有的静态文件,但将其余的代理到后端服务器.

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 (-f $request_filename) {
        access_log off;
        expires 30d;
        break;
        }

    if (!-f $request_filename) {
        proxy_pass http://127.0.0.1:8080; # backend server listening
        break;
        }
    }

以上将直接使用Nginx提供所有现有文件(例如Nginx仅显示PHP源代码),否则将请求转发给Apache.我需要从规则中排除* .php文件,以便将对* .php的请求也传递给Apache并进行处理.

Above will serve all existing files directly using Nginx (e.g. Nginx just displays PHP source code), otherwise forward a request to Apache. I need to exclude *.php files from the rule so that requests for *.php are also passed to Apache and processed.

我希望Nginx处理所有静态文件,并让Apache处理所有动态文件.

I want Nginx to handle all static files and Apache to process all dynamic stuff.

有白名单方法,但是它不是很优雅,请查看所有这些扩展名,我不希望这样.

There is white list approach, but it is not very elegant, See all those extensions, I don't want this.

location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
    access_log off;
    expires 30d;
    }
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;
    proxy_pass http://127.0.0.1:8080;
    }

在较新版本的Nginx上,使用try_files代替 http://wiki.nginx.org /HttpCoreModule#try_files

EDIT 2: On newer versions of Nginx use try_files instead http://wiki.nginx.org/HttpCoreModule#try_files



 1 条回答