My nginx root is /usr/share/nginx/html/ and my Node.js App is at /root/app/. But the reverse proxy searches for the app at nginx root.
I used the following location block.
location /t {
rewrite ^/t/(.*) /$1 break;
proxy_pass http://127.0.0.1:3000;
}
And changed the base uri in the app's config to
baseURI: '/t'
But I get a blank page, but the title bar says "AppName"
Here is the nginx error log
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/"
How can I successfully reverse proxy without moving my app from /root/app to the nginx root?
UPDATE :
On viewing the source of the page, I can see that nginx is trying to load resources from /static/js and /static/css at http://139.59.46.93 when it should be loading them from http://139.59.46.93:3000
So it is basically proxying the port, but not the resources.
How do I pass the resources too?
Best Answer
You are using
/t
inlocation
, but rewrites^/t/(.*)
, so nginx doesn't rewrite request to/t
and it is passed to your app as is.In general, you should always use trailing slash in
location
directive that should match directory (unless you're absolutely sure, you don't need it).Also there is a shortcut for this common case to replace location prefix in nginx. Try this one:
Documentation on
proxy_pass
says:Nginx will replace
/t/
in location directive with/
from proxy_pass directive.Probably you have to change app's
baseURI
to/t/
too.