'Nginx reverse proxy for Thingsboard

I need your help in nginx configuration to reverse proxy a thingsboard Community Edition instance. I already red a lot of tutorials, examples and posts on this topic but by my side it doesn't work and I don't understand why...

Here is my domain file in /etc/nginx/sites-enabled/my.domain.com:

server {
        server_name my.domain.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                proxy_pass http://58.34.135.17:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }

        listen [::]:443 ssl ipv6only=on; # managed by Certbot
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/my.domain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/my.domain.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = my.domain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name my.domain.com;
    return 404; # managed by Certbot
}

server {
        listen 8080;
        listen [::]:8080;

        server_name my.domain.com;

        return 301 https://my.domain.com$request_uri;

}

When nginx service is stopped, thingsboard is correctly running on port 8080 and reachable at the adresse http://my.domain.com:8080

When I start nginx (and thingsboard is still running) I have the following errors:

022/01/24 15:40:42 [emerg] 25727#25727: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2022/01/24 15:40:42 [emerg] 25727#25727: bind() to [::]:8080 failed (98: Address already in use)
2022/01/24 15:40:42 [emerg] 25727#25727: still could not bind()

So I stop thingsboard, run nginx and relaunch the thingsboard service with the following commands:

sudo systemctl stop thingsboard.service
sudo systemctl restart nginx.service
sudo systemctl restart thingsboard.service

At this step no error message, in my chrome browser the adresse changed to https://my.domain.com/ but thingsboard is unreachable and in my google chrome window I have the error "ERR_TOO_MANY_REDIRECTS":

chrome browser error_message

I have the last version of my browser, Thingsboard and Nginx are running under a Ubuntu server 18.04 which is up to date.

I would to see the open ports on my server to be sure thingsboard is correctly running (in addition of a systemctl status command):

netstat -lntup
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:587           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8883            0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::8080                 :::*                    LISTEN      -                   
tcp6       0      0 :::80                   :::*                    LISTEN      -                   
tcp6       0      0 :::8883                 :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 :::443                  :::*                    LISTEN      -                   
tcp6       0      0 :::1883                 :::*                    LISTEN      -                   
udp        0      0 127.0.0.53:53           0.0.0.0:*                           -                   
udp        0      0 58.34.135.17:68         0.0.0.0:*                           -                   

The 8080 port is actually used.

I tryed to change few lines in the nginx configuration but no change.

The point that hurt me is I already did it on another machine which is exactly the same that the one i'm using now and I haven't got problems. I of course used the configuration file of the "old" machine to configure this instance. And it's running under the other machin but not on this one.

EDIT

In the log of thingsboard we can see that he can't use the 8080 port if nginx is running first:

2022-01-24 15:48:08,829 [main] INFO  o.s.b.a.l.ConditionEvaluationReportLoggingListener - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-01-24 15:48:08,886 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Web server failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

So I don't understand because nginx is suposed to watch the port of the web UI of Thingsboard (which is on the port 8080) to redirect the http and 8080 port requests in https requests.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source