geekscrap
geekscrap

Reputation: 1045

Nginx redirect proxy_pass

Trying to utilise proxy_pass in my project but one of my redirected apps isn't playing properly.

server {

listen       80;

location / {
    root /usr/share/nginx/html/;
}

# RabbitMQ
location /rabbitmq/ {
    proxy_pass          http://rabbitmq:15672/;

    proxy_no_cache      1;
    proxy_cache_bypass  1;

    proxy_set_header    Host $host;
    proxy_set_header    X-Forwarded-For $remote_addr;
}

# mongo-express:0.44.0
location /mongoui/ {
    proxy_pass          http://mongoui:8081/;

    proxy_no_cache      1;
    proxy_cache_bypass  1;

    proxy_set_header    Host $host;
    proxy_set_header    X-Forwarded-Host $host;

}
}

RabbitMQ works fine, mongo-express doesn't.

It tries to load resources from the root directory /public/ which isn't set to proxy. A couple of the logs are below:

2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/vendor-cd59715f645362da9f54.min.js" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/vendor-cd59715f645362da9f54.min.js HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"
172.16.229.1 - - [06/Mar/2018:00:10:04 +0000] "GET /public/vendor-cd59715f645362da9f54.min.js HTTP/1.1" 404 571 "http://172.16.229.174:8080/mongoui/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/index-d5bd917c50cff4d254ba.min.js" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/index-d5bd917c50cff4d254ba.min.js HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"
172.16.229.1 - - [06/Mar/2018:00:10:04 +0000] "GET /public/index-d5bd917c50cff4d254ba.min.js HTTP/1.1" 404 571 "http://172.16.229.174:8080/mongoui/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/img/mongo-express-logo.png" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/img/mongo-express-logo.png HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"
172.16.229.1 - - [06/Mar/2018:00:10:04 +0000] "GET /public/img/mongo-express-logo.png HTTP/1.1" 404 571 "http://172.16.229.174:8080/mongoui/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/img/gears.gif" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/img/gears.gif HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"
172.16.229.1 - - [06/Mar/2018:00:10:04 +0000] "GET /public/img/gears.gif HTTP/1.1" 404 571 "http://172.16.229.174:8080/mongoui/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/vendor-cd59715f645362da9f54.min.js" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/vendor-cd59715f645362da9f54.min.js HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"
172.16.229.1 - - [06/Mar/2018:00:10:04 +0000] "GET /public/vendor-cd59715f645362da9f54.min.js HTTP/1.1" 404 571 "http://172.16.229.174:8080/mongoui/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36" "-"
2018/03/06 00:10:04 [error] 5#5: *13 open() "/usr/share/nginx/html/public/index-d5bd917c50cff4d254ba.min.js" failed (2: No such file or directory), client: 172.16.229.1, server: , request: "GET /public/index-d5bd917c50cff4d254ba.min.js HTTP/1.1", host: "172.16.229.174:8080", referrer: "http://172.16.229.174:8080/mongoui/"

Upvotes: 1

Views: 1059

Answers (1)

geekscrap
geekscrap

Reputation: 1045

The app had an environment variable that was required to be set in docker:

 - ME_CONFIG_SITE_BASEURL=/mongoui

So the following works (without the trailing slashes):

server {
listen       80;

location / {
    root /usr/share/nginx/html/;
}

# RabbitMQ
location /rabbitmq/ {
    proxy_pass          http://rabbitmq:15672/;

    proxy_no_cache      1;
    proxy_cache_bypass  1;

    proxy_set_header    Host $host;
    proxy_set_header    X-Forwarded-For $remote_addr;
}

# MongoUI
location /mongoui {
    proxy_pass          http://mongoui:8081;

    proxy_no_cache      1;
    proxy_cache_bypass  1;

    proxy_set_header    Host $host;
    proxy_set_header    X-Forwarded-For $remote_addr;
}
}

Upvotes: 2

Related Questions