skip to Main Content

I have a simple task with nginx, but I cannot work it out:

I have a domain example.com, which points to a local server 192.168.1.150, which runs nginx in docker.

But I have another container, "myapp" running on port 8080, and I want to pass this location – from "https://192.168.1.150/myapp" and "https://example.com/myapp" to "http://192.168.1.150:8080", which is http only, and I want to have a secure connection through nginx.

The Problem to solve

At the moment if I request https://192.168.1.150/myapp it navigates to https://192.168.1.150/login but the target should be https://192.168.1.150/myapp/login. Although URL "changes" successfully, nginx responds HTTP 404;

See my location settings in nginx.conf:

location /myapp {
        proxy_pass         http://192.168.1.150:8080;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
}

SSL

Just a side note: I already set SSL up with redirecting from HTTP, tested with a simple location "/test", which returns plain text "OK", looks fine.

If this is cannot be solved like that, it is also OK for me to use it with another server and listen to another port than 443. (tried, same result)

Any help will be appriciated! Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    So, answering my own question: I had to rewrite the uri to redirect correctly. I did not had the proper words to find the info on the net, but keywords like "preserve" helped to find examples.

    The solution was to use the rewrite module of nginx and write a replacement for the 'myapp' section: rewrite ^/myapp(.*) $1? break;.

    More info on ngx_http_rewrite_module.html and search for 'replacement'.

    So at the moment my working config with a simple Python http.server:

    location /myapp/ {
        rewrite ^/myapp(.*) $1? break;
        proxy_pass          http://myapp$is_args$args;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection keep-alive;
        proxy_set_header    Host $http_host$request_uri;
        proxy_cache_bypa    $http_upgrade;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_f>
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_set_header    X-Real-IP $remote_addr;
    }
    

    Other redirections to web applications and servers do not work, but my question is resolved.

    I also had problems without trailing slash like example.com/myapp, nginx dropped HTTP 500, which I overcame with this:

    location ~ ^/myapp$ {
        return 301 $request_uri/;
    }
    

    Thanks for the helps!


  2. Try this, maybe this is what you need:

    proxy_pass http://myapp;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search