Error running on subdirectory with Nginx


So I’m using Ubuntu 16 and I’ve installed 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 Who can help me out?

# Upstreams
upstream backend {

#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;


        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/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot