Nginx 反向代理未从其他端口加载资源

Nginx 反向代理未从其他端口加载资源

我的 nginx 根目录是/usr/共享/nginx/html/我的 Node.js 应用程序位于/根/应用程序/.但是反向代理在 nginx 根目录搜索应用程序。

我使用了以下位置块。

location /t {
  rewrite ^/t/(.*) /$1 break;
  proxy_pass http://127.0.0.1:3000;
}

并将应用程序配置中的基本 uri 更改为

baseURI: '/t'

但我得到的是空白页,但标题栏显示“应用程序名称

这是 nginx 错误日志

2017/10/26 16:41:43 [error] 3585#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 49.206.123.228, server: _, request: "GET /t HTTP/1.1", upstream: "http://127.0.0.1:3000/t", host: "139.59.46.93"
2017/10/26 16:41:43 [error] 3585#0: *5 open() "/usr/share/nginx/html/50x.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /t HTTP/1.1", upstream: "http://127.0.0.1:3000/t", host: "139.59.46.93"
2017/10/26 16:41:43 [error] 3585#0: *5 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /favicon.ico HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t"
2017/10/26 16:41:43 [error] 3585#0: *5 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /favicon.ico HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t"
2017/10/26 16:41:45 [error] 3585#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 49.206.123.228, server: _, request: "GET /t/ HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "139.59.46.93"
2017/10/26 16:41:45 [error] 3585#0: *5 open() "/usr/share/nginx/html/50x.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /t/ HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "139.59.46.93"

##### Ignore above errors. Forgot to use public IP

2017/10/26 16:43:45 [error] 3620#0: *1 open() "/usr/share/nginx/html/static/css/main.749f8f8f.css" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:45 [error] 3620#0: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:45 [error] 3620#0: *1 open() "/usr/share/nginx/html/static/js/main.5957a012.js" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:45 [error] 3620#0: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:48 [error] 3620#0: *1 open() "/usr/share/nginx/html/static/css/main.749f8f8f.css" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:48 [error] 3620#0: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:48 [error] 3620#0: *3 open() "/usr/share/nginx/html/static/js/main.5957a012.js" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:48 [error] 3620#0: *3 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *3 open() "/usr/share/nginx/html/static/css/main.749f8f8f.css" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *3 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/css/main.749f8f8f.css HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *1 open() "/usr/share/nginx/html/static/js/main.5957a012.js" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /static/js/main.5957a012.js HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /favicon.ico HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"
2017/10/26 16:43:50 [error] 3620#0: *1 open() "/usr/share/nginx/html/404.html" failed (2: No such file or directory), client: 49.206.123.228, server: _, request: "GET /favicon.ico HTTP/1.1", host: "139.59.46.93", referrer: "http://139.59.46.93/t/"

如何在不将我的应用程序从 /root/app 移动到 nginx 根目录的情况下成功进行反向代理?

更新 :

查看页面源代码时,我可以看到 nginx 正在尝试从/静态/js/静态/csshttp://139.59.46.93什么时候应该加载它们http://139.59.46.93:3000

所以它基本上就是代理端口,但不是资源

我该如何传递资源?

答案1

您正在使用/tlocation但重写^/t/(.*),因此 nginx 不会重写请求/t并将其传递给您的应用程序照原样

一般来说,您应该始终在location与目录匹配的指令中使用尾随斜杠(除非您绝对确定,您不需要它)。

对于这种常见情况,还有一个快捷方式来替换 nginx 中的位置前缀。试试这个:

location /t/ {
    proxy_pass http://127.0.0.1:3000/;
    # note this slash --------------^
}

文档proxy_pass说:

请求 URI 传递到服务器如下:

  • 如果 proxy_pass 指令指定了 URI,那么当请求传递给服务器时,规范化请求 URI 中与位置匹配的部分将被指令中指定的 URI 替换。

Nginx 将/t/取代地点带有/from 的指令代理密码指示。

您或许也需要更改应用baseURI程序/t/

相关内容