大网数据广告活动
服务器运维问答

NGINX Reverse proxy not loading resources

· 52CE 我爱测

I'm trying to put a reverse proxy in front of a few internal web services. Nginx is setup and working properly to serve request. If I only serve one location / in my conf file, it loads that proxy_pass target successfully ie: loc…

问题原文

原帖链接 · PenguinCoder · CC BY-SA 3.0

I'm trying to put a reverse proxy in front of a few internal web services.

Nginx is setup and working properly to serve request. If I only serve one location / in my conf file, it loads that proxy_pass target successfully ie:

location / {
  proxy_pass https://internal.ip:port/;
 }

Browsing to the https://public_proxy_address/ loads that internal resource perfectly.

However, since I have multiple internal applications, I want to proxy all of them through nginx. So, in my testing I have changed the conf to as follows:

location /app1 {
  proxy_pass https://internal.ip:port/;
}

With this in place, I get the main/default internal app index page, but the view source shows me that no link stylesheets, js, or other resources are being re-written. Therefore the entire contents of the page, fails to load. In Apache, I would probably write a proxyhtmlurlmap ^/resource/ /app1/resource R. I cannot find any way to achieve this in nginx though.

I have also tried this, to no avail:

location /app1 {
  rewrite /resource(.*?) /app1/resource$1 break;
  proxy_pass https://internal.ip:port/;
 }

How do I get nginx to properly prepend the target location (app1) to the requested resource urls so that they load?

社区高分解答

原帖链接 · abdus_salam · CC BY-SA 3.0

As I don't have enough credit, I cannot comment on entries, so, here comes my response as a separate entry.

I don't know why the OP disagrees but I think Martin Fjordval identifies the problem correctly and suggests the right thing. I'd suggest a slight modification to his snippet as below though;

                location /app1/ {
                    proxy_set_header Host $http_host/app1;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Scheme $scheme;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header X-Forwarded-Proto $scheme;
                    proxy_redirect    off;
                    add_header Pragma "no-cache";
                    add_header Cache-Control "no-cache";

                    proxy_pass http://internal.ip:port/;
                    sub_filter 'action="/'  'action="/app1/';
                    sub_filter 'href="/'  'href="/app1/';
                    sub_filter 'src="/'  'src="/app1/';
                    sub_filter_once off;
            }

So, if you are sure that all your relative urls that are starting with '/' needs redirecting, the above would append '/app1/' to them all and get them caught by the 'location /app1/' block when referenced. You can then view the page source and confirm that the rewriting is happening.

You'd also probably need the whole or at least the part of the 'proxy_set_header' shabang, especially the 'proxy_set_header Host $http_host/app1' bit.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。