如何直接使用 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 并进行处理。
我希望 Nginx 处理所有静态文件,让 Apache 处理所有动态文件。
编辑:有白名单方法,但它不是很优雅,查看所有这些扩展,我不想要这个。
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;
}
编辑2:在较新版本的Nginx上使用 try_files
代替 http://wiki.nginx .org/HttpCoreModule#try_files
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;
}
}
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.
I want Nginx to handle all static files and Apache to process all dynamic stuff.
EDIT: 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;
}
EDIT 2: On newer versions of Nginx use try_files
instead http://wiki.nginx.org/HttpCoreModule#try_files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 mod_rewrite 来隐藏脚本的扩展名,或者您只是喜欢以 / 结尾的漂亮 URL,那么您可能需要从另一个方向来解决这个问题。 告诉 nginx 让任何带有非静态扩展名的东西都通过 apache。 例如:
我在以下位置找到了该片段的第一部分: http://code.google .com/p/scalr/wiki/NginxStatic
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:
I found the first part of this snippet over at: http://code.google.com/p/scalr/wiki/NginxStatic
使用 try_files 和命名位置块('@apachesite')。 这将删除不必要的正则表达式匹配和 if 块。 更高效。
更新:此配置的假设是
/path/to/root/of/static/files
下不存在任何php脚本。 这在大多数现代 PHP 框架中都很常见。 如果您的旧 php 项目在同一文件夹中混合了 php 脚本和静态文件,您可能必须将您希望 nginx 提供服务的所有文件类型列入白名单。Use try_files and named location block ('@apachesite'). This will remove unnecessary regex match and if block. More efficient.
Update: The assumption of this config is that there doesn't exist any php script under
/path/to/root/of/static/files
. This is common in most modern php frameworks. In case your legacy php projects have both php scripts and static files mixed in the same folder, you may have to whitelist all of the file types you want nginx to serve.试试这个:
希望它有效。 正则表达式比普通字符串具有更高的优先级,因此如果仅存在相应的
.php
文件,则所有以.php
结尾的请求都应转发到 Apache。 其余部分将作为静态文件处理。 评估位置的实际算法在此处。Try this:
Hopefully it works. Regular expressions have higher priority than plain strings, so all requests ending in
.php
should be forwared to Apache if only a corresponding.php
file exists. Rest will be handled as static files. The actual algorithm of evaluating location is here.