0

Is it possible to configure nginx reverse proxy where http://localhost/a/ leads to 1 site and http://localhost/b/ to another? I tried this config:

server {
    listen       80;
    server_name  localhost;

    location /a/ {
        proxy_pass http://apache.org/;
    }

    location /b/ {
        proxy_pass http://www.gnu.org/;
    }

it almost works, but all the links returned by web pages are missing /a/ or /b/ prefix so it can't load any pictures, styles and etc. For example link http://localhost/css/styles.css is not working, but http://localhost/a/css/styles.css is working.

Is there a directive that will append all links on page with suitable prefix? Or there is different approach to put a websites on separate locations?

lacerated
  • 375
  • 1
  • 4
  • 17
  • There's [ngx_http_sub_module](http://nginx.org/en/docs/http/ngx_http_sub_module.html) but I would refrain from using it. Instead you could just use different names (by adding additional names that resolve to 127.0.0.1 to your OS hosts file). – slauth Oct 21 '21 at 08:28
  • @lacerated See the last option of [this](https://stackoverflow.com/a/62840133/7121513) answer. There is also a thread on ServerFault: [How to handle relative urls correctly with a nginx reverse proxy](https://serverfault.com/questions/932628/how-to-handle-relative-urls-correctly-with-a-nginx-reverse-proxy) – Ivan Shatsky Oct 21 '21 at 08:31

1 Answers1

0

@ivan-shatsky, Thank you very much.

Just wanted to add working config if some1 else needs.

map $http_referer $prefix {
    ~https?://[^/]+/a/     a;
    default                   b;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        try_files /dev/null @$prefix;
    }

    location /a/ {
        proxy_pass http://apache.org/;
    }

    location @a {
        proxy_pass http://apache.org;
    }

    location /b/ {
        proxy_pass http://www.gnu.org/;
    }
    location @b {
        proxy_pass http://www.gnu.org;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}```
lacerated
  • 375
  • 1
  • 4
  • 17