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 http://wiki.nginx.org/HttpCoreModule#try_files
Use 尝试文件 http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files和命名位置块('@apachesite')。这将删除不必要的正则表达式匹配和 if 块。更高效。
location / {
root /path/to/root/of/static/files;
try_files $uri $uri/ @apachesite;
expires max;
access_log off;
}
location @apachesite {
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;
}
Update:这个配置的假设是下面不存在任何php脚本/path/to/root/of/static/files
。这在大多数现代 PHP 框架中都很常见。如果您的旧 PHP 项目在同一文件夹中混合了 PHP 脚本和静态文件,您可能必须将您希望 nginx 提供服务的所有文件类型列入白名单。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)