通过Nginx中的proxy_redirect替换图像和JavaScript绝对路径 [英] Replace image and javascript absolute paths through proxy_redirect in nginx

查看:727
本文介绍了通过Nginx中的proxy_redirect替换图像和JavaScript绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况.

Nginx用作监听8080端口的apache服务器的反向代理.nginx运行在80端口.apache服务器正在运行一个wsgi应用程序.

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,这样,无论对本地主机/(nginx端口是默认端口80)的请求如何,它们都将被重定向到本地主机: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/<something>的任何请求都重定向到http://localhost:8080/<something>,因为应用程序的资源将在端口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无法通过其他URL为石墨网提供服务比/

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 :

…通过将一个指定的字符串替换为另一个指定的字符串来修改响应.

…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/';
}


请注意:


Note that:

默认情况下未构建此模块,应使用--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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆