Error running on subdirectory with Nginx

So I’m using Ubuntu 16 and I’ve installed rocket.chat with snap. I’ve setup systemd and SSL with Let’s Encrypt. Following is my Nginx config file and I keep getting 502 Bad Gateway when I go to app.andrew.at.tw/chat. Who can help me out?

# Upstreams
upstream backend {
    server 127.0.0.1:3000;
}

#HTTPS Server
server {

        root /var/www/app;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name app.andrew.at.tw;

        location ~/chat(.*)$ {
                proxy_pass http://backend/$1;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_set_header Host $http_host;

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forward-Proto http;
                proxy_set_header X-Nginx-Proxy true;

                proxy_redirect off;
        }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/app.andrew.at.tw/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/app.andrew.at.tw/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

}