0

I have, what I hope is, a simple question. I am running Nginx and some applications in Docker containers. Some of the applications run on the same host as Nginx. I can access an application using, for example, app.example.com, but I want to access the same application using example.com/app. I cannot figure out how to define the server block with location /app. I would like to achieve something like:

server {
  listen       80;
  server_name  app.example.com;
  location / {
    proxy_pass app-srv:port;
  }
}
server {
  listen       80;
  server_name  example.com;
  location /app {
    What do I place here?
  }
}

Edit: with additional information.

My server configuration is:

server {
  listen 80;
  server_name openhab.aronica-sys;
  location / {
    proxy_pass http://openhab:8081;
  }
}
server {
  listen 80;
  server_name aronica-sys;
  location /openhab/ {
    proxy_pass http://openhab:8081/;
  }
}

openhab in the proxy_pass statements is the Docker virtual address for the openHAB server. 'openhab.aronica-sys' gets:

VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/tiles".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/habot/greet".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/components/ui:widget".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/ui/components/ui:page".
VM6:1 XHR finished loading: GET "http://openhab.aronica-sys/rest/items?metadata=semantics,listWidget,widgetOrder".
VM6:1 XHR finished loading: POST "http://openhab.aronica-sys/rest/events/states/2e0eee99-770f-498b-bd9f-736777096c30".
VM6:1 XHR finished loading: POST "http://openhab.aronica-sys/rest/events/states/2e0eee99-770f-498b-bd9f-736777096c30".

aronica-sys/openhab gets:

VM6:1 GET http://aronica-sys/rest/ui/tiles 404 (Not Found)
VM6:1 GET http://aronica-sys/rest/ 404 (Not Found)
VM6:1 XHR failed loading: GET "http://aronica-sys/rest/ui/tiles".
aronica-sys/:1 Uncaught (in promise) Not Found
aronica-sys/:1 Uncaught (in promise) Not Found
VM6:1 XHR failed loading: GET "http://aronica-sys/rest/".

I do not know how to interpret the above information nor how to proceed.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41

1 Answers1

0

You will need something like:

server {
  listen       8080;
  server_name  app.example.com;
  location / {
    proxy_pass http://localhost:9090;
  }
}
server {
  listen       8080;
  server_name  example.com;
  location /app/ {
    proxy_pass http://localhost:9090/;
  }
}

Please be careful with the slashes used in location and in proxy pass, they are quite important! For an explanation on the slashes, you can check: https://stackoverflow.com/a/51225241/83037

Edit: with the above configuration your actual sever process will receive the request without any modification to the host part. If you use in a browser http://app.example.com/file.html the server will receive a request for http://app.example.com/file.html (same host app.example.com). If you use in a browser http://example.com/app/file.html the server will receive a request for http://example.com/file.html (same host example.com, different path).

Generally, a server could "care" about the hostname, and/or could care "care" about the path, in the example above if it does not "care" about hostname will "interpret" the request as "give me /file.html", but if he "cares" about hostname, it will be configured to work only for a set of hostnames - server might answer or example.com but not for app.example.com.

Based on your addition, your server is configured to answer only to one specific hostname (he "cares" about hostname). In that case you can make nginx "rewrite" the incoming request such that your server "sees" only one hostname. Like if you use in a browser http://example.com/app/file.html the server will receive a request for http://app.example.com/file.html (notice example.com changed to app.example.com).

The configuration for that is:

server {
  listen       8080;
  server_name  app.example.com;
  location / {
    proxy_pass http://localhost:9090;
    proxy_set_header Host app.example.com;
  }
}
server {
  listen       8080;
  server_name  example.com;
  location /app/ {
    proxy_pass http://localhost:9090/;
    proxy_set_header Host app.example.com;
  }
}

WARNING! The server can still show you links to the domain it is configured to. In your case, the server might show (or build with javascript) a link to "http://openhab.aronica-sys/my_page.html". If that is an issue for you, I would suggest to open another question - this question is about the virtual directory.

vladmihaisima
  • 2,119
  • 16
  • 20