在nginx中通过proxy_redirect替换图片和javascript绝对路径 [英] Replace image and javascript absolute paths through proxy_redirect in nginx
问题描述
我有一个场景如下.
Nginx 被用作侦听端口 8080 的 apache 服务器的反向代理.nginx 在端口 80 上运行.有一个 wsgi 应用程序正在由 apache 服务器运行.
Nginx is being used as a reverse proxy to a apache server listening at port 8080. nginx is running at port 80. There is a wsgi application which is being run by the apache server.
现在,我在 nginx 配置中添加了一个 proxy_pass,这样无论来自 localhost/(nginx 端口是默认端口 80)的请求都会被重定向到 localhost:8080.
Now, I have added a proxy_pass to the nginx configuration such that whatever requests come to localhost/ (nginx port is the default port 80) they get redirected to localhost:8080.
这里是 nginx conf 文件的摘录:
Here is an excerpt from the nginx conf file:
server {
listen 80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
location <app_name> {
proxy_pass http://localhost:8080/
proxy_redirect http://localhost/ http://localhost:8080/
}
}
我添加了代理重定向来处理来自 apache 服务器的任何重定向,以便任何映射到 http://localhost/
的请求都被重定向到 http://localhost:8080/
因为应用程序的资源将在端口 8080 下可用.
I have added the proxy redirect to take care of any redirects from the apache server so that any request mapping to http://localhost/<something>
gets redirected to http://localhost:8080/<something>
because the application's resources are going to be available under port 8080.
现在,这里的问题是 wsgi 应用程序生成了一个 html,其中图像和 javascript 位置路径是绝对路径,如/img/image.png 和/js/javascript.js.现在,它们是 HTML 的重要组成部分,因此不会发送任何带有完整 http://前缀的重定向,因此服务器会尝试在 localhost/img 目录而不是 localhost:8080/img 目录中定位图像.
Now, the problem here is that the wsgi application generates an html where the image and the javascript location paths are absolute paths like /img/image.png and /js/javascript.js. Now, they are part and parcel of the HTML and do not send any redirect with a complete http:// prefix as such therefore, the server tries to locate the images in the localhost/img directory instead of the localhost:8080/img directory.
一个肮脏的解决方法可能是在服务器配置中将/img 和/js 目录定义为单独的位置",并为它们指定一个 proxy_pass.但是这些目录的数量可能会更多,保持相同的目录可能会让人头疼.
A dirty workaround for the same could be to have the /img and /js directories also defined as separate "location" in the server config and a proxy_pass specified to them. But then these directories could be more in number and maintaining the same could potentially become a headache.
有更简洁的方法吗?
这是为了解决石墨中的问题 Apache 无法从其他网址提供石墨网比/
This is in reference to fixing the issue in graphite Apache cannot serve graphite-web from URLs other than /
推荐答案
如果代理应用程序(在本例中为 Graphite 应用程序)无法配置为从属"应用程序,则最好保留应用程序的整个子域.
In case where the proxied application (the Graphite app in this case) isn't able to be configured as a "slave" app, it is better to spare a whole subdomain to the application.
或者您可以尝试使用 ngx_http_sub_module :
Or you may try the ngx_http_sub_module which:
...通过将一个指定的字符串替换为另一个来修改响应.
…modifies a response by replacing one specified string by another.
作为示例,此代码会将所有 ':/localhost:8080/' 更改为 ':/localhost/app_name/':
as an example this code will change all the ':/localhost:8080/' to ':/localhost/app_name/':
location <app_name> {
# ...
sub_filter ':/localhost:8080/' ':/localhost/app_name/';
}
<小时>
注意:
默认情况下不构建此模块,应使用 --with-http_sub_module 配置参数启用它.
This module is not built by default, it should be enabled with the --with-http_sub_module configuration parameter.
但在大多数包系统中,nginx 已经构建了所有可选模块.只需检查您的版本是否内置了 sub_module:
But in most package systems nginx is built with all it's optional modules already. Just check if you version has the sub_module built in:
nginx -V | grep sub_module
就我的自制软件而言,它提供了这样的输出.
In my case for homebrew it gives output like this.
这篇关于在nginx中通过proxy_redirect替换图片和javascript绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!