kutt icon indicating copy to clipboard operation
kutt copied to clipboard

Docker + Nginx Reverse Proxy

Open samboman opened this issue 3 years ago • 9 comments

I have one machine running the container (internally accessible): general.example.com Kutt.it are running on PORT=3000 (set in .env and the docker-compose). DEFAULT_DOMAIN=general.example.com

Its working fine, I can access it on http://general.example.com:3000

But when I want to run it behind my Nginx reverse proxy, configured to respond to https://example.com I get redirected to the internal URL.

server {
    listen 443 ssl;
    server_name example.com;
    proxy_request_buffering off;
    proxy_buffering off;
    
    ssl_certificate fullchain.pem;
    ssl_certificate_key privkey.pem;
    include options-ssl-nginx.conf;
    ssl_dhparam ssl-dhparams.pem;

    if ($host != "example.com") {
        return 403;
    }

    server_tokens off;

    location / {
        proxy_pass http://general.example.com:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

Can you advice me? Thanks!

samboman avatar Apr 28 '21 19:04 samboman

I'm no NGINX expert, but I think you should set server_name to your full domain and set proxy_pass to localhost like: proxy_pass http://localhost:3000;

poeti8 avatar May 09 '21 09:05 poeti8

Ok, maybe I was not so detailed. It is two different hosts (machines). Reverse-proxy (example.com) and KuttIt-server (general.example.com). It is some redirection within the KuttIt application that cause the browser to redirect to the internal URL.

samboman avatar May 09 '21 10:05 samboman

Try adding this: proxy_redirect off;

RyderForReal avatar May 14 '21 11:05 RyderForReal

Tried: proxy_redirect off; But it have no effect.

I think the problem is that Kutt are checking the hostname (example.com) typed into the browser against the DEFAULT_DOMAIN=general.example.com. And if the hostname does not match, Kutt sends a permanent redirect response to the browser to the configured DEFAULT_DOMAIN. Is there a way to avoid this?

samboman avatar May 17 '21 08:05 samboman

I removed: proxy_set_header Host $host;

Then the redirect to the internal domain stopped. Also I set: DEFAULT_DOMAIN=general.example.com:3000

The problem is now in the webbapp: image

The internal URL are used instead of the public: https://example.com/bbg84

A work around can be to add example.com as a custom domain. But then I have to remember to use the custom domain every time I create a new one. - But it seems to give a 404-error...

EDIT: I can fix it by rewriting the response. But it's not ideal.

samboman avatar May 17 '21 10:05 samboman

My "final" solution is to rewrite the response at the reverse-proxy (Nginx), not ideal but it works as expected:

sub_filter_types application/json;
sub_filter 'general.example.com:3000' $host; # $host = example.com
sub_filter_once off;

samboman avatar May 18 '21 12:05 samboman

Hi @samboman,

To solve this problem I created two instances of the app, changing the DEFAULT_DOMAIN accordingly.

So, to access it internally one instance has DEFAULT_DOMAIN=internal.mynetwork.com and the wide used one has DEFAULT_DOMAIN=public.mynetwork.com. Both point to the same BD and Redis and both redirect correctly (public.mynetwork.com/zzxx12 and internal.mynetwork.com/zzxx12).

thyarles avatar May 28 '21 20:05 thyarles

Hi, I've stumbled on the same issue and managed to make it work, maybe it can help you out.

In .env file set

DEFAULT_DOMAIN=example.com

On your nginx conf, set

proxy_pass http://general.example.com:3000

If you access http://general.example.com:3000 you will be redirected to https://example.com.

When you access https://example.com it will correctly forward the request to general.example.com:3000 an everything should work as expected.

If you still need to access it with multiple domains take a look at @thyarles comment above.

LPBR avatar Aug 19 '21 13:08 LPBR

Sorry... I don't understand... I have domain example.com. What I need write in .env (docker installation), and what I must write in nginx? For now when I curl https://example.com I recieve 301 Moved Permanently to https://example.com DEFAUL_DOMAIN=example.com CUSTOM_DOMAIN_USE_HTTPS=true

In nginx proxy_pass http://localhost:3000;

Kirill avatar Oct 03 '21 21:10 Kirill