0

I have 2 web applications that I want available under http://example.com/demo1 and http://example.com/demo2. My config below correctly reverse proxies to each site, and the site is mostly functional, but some API requests from my web application are 404'ing because they are omiting the /demo1 and /demo2 subdirectories from the Request URL. I suspect that I may need to update a header by appending the subdirectory, but I'm not sure. More details are below.

Here is my config:

worker_processes 1;
error_log stderr notice;
daemon off;

env DEMO1_PORT_8005_TCP_ADDR;
env DEMO2_PORT_8006_TCP_ADDR;

events {
    worker_connections 1024;
}

http {

    include /usr/local/openresty/nginx/conf/mime.types;
    charset utf-8;

    ##
    # Logging Settings
    ##
    error_log /var/log/nginx/error.log warn;
    access_log /var/log/nginx/access.log;                

    ##
    # Virtual Host Configs
    #
    server {
        listen 80;
        server_name example.com;
        # root /var/www;
        root /duwamish;

        location /demo1 {
            # load static files paths first (/static/(.)*), then reverse-proxy to demo
            try_files $uri $uri/ @demo1;
        }
        location @demo1 {
            set_by_lua $server_location 'return os.getenv("DEMO1_PORT_8005_TCP_ADDR")';
            proxy_pass http://$server_location:8005;
            proxy_set_header Host $host;
        }

        location /demo2 {
            # load static files paths first (/static/(.)*), then reverse-proxy to demo
            try_files $uri $uri/ @demo2;
        }
        location @demo2 {
            set_by_lua $server_location 'return os.getenv("DEMO1_PORT_8005_TCP_ADDR")';
            proxy_pass http://$server_location:8005;
            proxy_set_header Host $host;
        }

    }

}

My site is loading fine, but I am getting 404's on some requests made by my demo1 and demo2 web applications because the Request URL is not being served. For example, the Request URL is http://example.com/api/submitted_thing?parameter=true when I think it should be http://example.com/demo1/api/submitted_thing?parameter=true

Basically, I would like to use something like proxy_set_header Host $host/demo1, but I don't think I can append a path to the Host header. Any suggestions?

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
modulitos
  • 335
  • 1
  • 3
  • 16
  • How should nginx guess that `/api/...` should be `/demo1/api/...`? Just use subdomains. It would be much easier than adding/removing path prefix. – Alexey Ten Feb 12 '15 at 12:33
  • @AlexeyTen I believe my `demo1` and `demo2` web applications are making the `/api/...` requests, which is appended to my Host header `http://example.com`. If there was an appropriate header that nginx can set, where `/api/...` would be appended to `http://example.com/demo1` instead, then these requests should work. Yes, I can use subdomains instead, but I would prefer to access my apps by subdirectory. Am I missing something, or are there other options available? – modulitos Feb 12 '15 at 22:50
  • 1
    Host cannot contain path. So either you modify your application to access to `/demo1/api/...`(hard and errorprone) or use subdomains (easy and reliable). It's your choise – Alexey Ten Feb 13 '15 at 08:47
  • @AlexeyTen Thanks for the advice, and I agree, subdomains are much easier. It seems like I was trying something illogical. Just for clarity, if I have a client or a project manager that would prefer to access their web applications via subdirectory, I can respond by saying subdirectories are not a sane way to access different web applications? Perhaps the subdirectories that share a domain should be served, and are customarily served, by the same application? I think that an explanation for why my above attempt cannot, or should not, be implemented is a useful answer. – modulitos Feb 13 '15 at 09:21
  • 1
    If application is not written with this possibility in mind, there could be many problems from easy to practically impossible to fix. E.g. if there is some hand written content which contains `` you'll not be able to fix this link to point to `/demo1/link` – Alexey Ten Feb 13 '15 at 09:51

0 Answers0