1

My intention is to have several webapps run on different VMs, all put through a nginx proxy.

The main Problem is, that the URL gets changed, e.g. http://example.com/app1/ Gets to the webapp (but CSS doesn't get displayed) and on click on e.g. "login" there is this: http://example.com/login/

So, What I get:

example.com/app1/ -> "click" -> example.com/login/

What I want is:

example.com/app1/ -> "click" -> example.com/app1/login/

The Setup

Virtual Machine redirecting all incoming port 80 traffic to Nginx-VM, Webapp Runs on different VM, hosting own webserver on e.g. port 8000.

File /etc/nginx/sites-enabled/default (abbrev.):

server {
    listen 0.0.0.0:80;
    server_name example.com;
    location /app1/ {
        proxy_pass http://10.11.100.204:8080/;
    }
}

I tried different things, e.g. leaving the trailing slash at the end of the proxy_pass, but that gives me a 404 when calling example.com/app1. proxy_set_header Host $host didn't do the trick either.

Curl output

From Hypervisor Machine (as in comments):

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 3587
Set-Cookie: session=.eJyrVorPTFGyqlZSSFKyUooM98uOCnE0jHIPLfdzcTWMcnGs8s1KrvTNysmOzEo2ijQKrfINdzXwd0m2Va rVUcrJT07MSYVrB2oDCxckpqfGZ2QWl-QXVSpZRStllJQUWOnrF5fmJCbn6-Wm5ZTmF-klZ-grxdYCABkQKVw.B4U2hw.Rbl3k3yxoLQx1UgKpP68uB_X5cI; HttpOnly; Path=/
Server: Werkzeug/0.9.6 Python/2.7.3
Date: Wed, 31 Dec 2014 07:25:59 GMT

From Outside (with external URL example.com/app1/):

HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Wed, 31 Dec 2014 07:35:06 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 3587
Connection: keep-alive
Set-Cookie: session=.eJyrVorPTFGyqlZSSFKyUorKDTX1C3HL8nPxNPU18suKzI008ctyyojKdTWJDEk38s31LffN8iz3C3G0VarVUcrJT07MSUVodw8FCxckpqfGZ2QWl-QXVSpZRStllJQUWOnrF5fmJCbn6-Wm5ZTmF-klZ1hZGOgrxdYCAHnCKcI.B4U4qg.IWeiafAy-VOV0vhuyUP-dlDFt5w; HttpOnly; Path=/

Without trailing slash "example.com/app1"

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.1
Date: Wed, 31 Dec 2014 07:29:52 GMT
Content-Type: text/html
Content-Length: 184
Location: http://example.com/app1/
Connection: keep-alive

Thank you in advance!

Mat Fluor
  • 11
  • 4
  • Are you sure you are not getting the `Location` header from browser cache ? If not, what's the result of `curl -sI "http://10.11.100.204:8080/" -H "Host: example.com"` ? – Xavier Lucas Dec 31 '14 at 00:49
  • You should configure your apps, so they know that they runs in aubdirectory. But usually it's much easier to use subdomains like app1.example.com, app2.exapmle.com... – Alexey Ten Dec 31 '14 at 07:13
  • Curl output added to question. – Mat Fluor Dec 31 '14 at 07:25
  • I would suggest trying removing the trailing slash from both the ```/app1``` location setting and the ```proxy_pass``` URL. As per the accepted answer to this question; http://serverfault.com/questions/598003/nginx-as-a-proxy-for-tomcat-with-subdirectory – Chris Davidson Dec 31 '14 at 07:53
  • Unfortunately removing the trailing slash from the proxy_pass line returns a 404 by the webapp, because it tries to access "/app1" on the VM, but on the VM it's "/". – Mat Fluor Dec 31 '14 at 07:59
  • Ok, I've just read http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass and you should be able to add the /app1 on to the proxy_pass decleration. Could you try that? – Chris Davidson Dec 31 '14 at 08:11
  • @Dayvo: Then it tries to access /app1 on the VM, it has to access the root on the VM, but prefix the "subdirectory" always, so hat example.com/app1/.* always proxies to 10.11.100.204:8080/.* – Mat Fluor Dec 31 '14 at 08:16
  • You probably need to rewrite the incoming URL to remove the `app1/` part before proxing it. – wurtel Dec 31 '14 at 11:27
  • @AlexeyTen It works with subdomains, thank you very much! My intention was to test on subdirectories, and when testing is done, put it on a subdomain (before your answer) good to know that works flawlessly. – Mat Fluor Jan 01 '15 at 08:05

0 Answers0