nginx proxy to tomcat

Viewed 357

I've realized it belongs more to serverfault so I moved it there: link

I went through dozens of tutorials and I can't figure out following:

I have my compiled vue application in /var/www/mydomain.com and I want it to be shared as static content.

My backend running on 8080 by tomcat with public APIs on /api/something... URLs. The URLs are hardcoded including the "api" part.

I'd like to configure nginx to proxy mydomain.com/api/something... requests to tomcat and rest be served statically from /var/www/mydomain.com. Everything served through SSL.

I litterally don't need anything else.

Can you help me configure the nginx and tomcat to achieve that? Thank you!

nginx config /etc/nginx/sites-available/mydomain.com

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
        listen 443 ssl default_server;
        #listen [::]:443 ssl default_server;

        root /var/www/mydomain.com;
        index index.html index.htm index.nginx-debian.html;

       server_name _ mydomain.com www.mydomain.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        location /api/ {
                include proxy_params;
                proxy_pass http://tomcat;
        }

        ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.mydomain.com/privkey.pem; # managed by Certbot
}
server {
    if ($host = www.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


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


       listen 80 default_server;
        listen [::]:80 default_server;

       server_name _ mydomain.com www.mydomain.com;
   return 404; # managed by Certbot
}

Tomcat config server.xml

        <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               address="127.0.0.1"
               redirectPort="8443" />

Current situation is that when I go to mydomain.com/api/docs where swagger should be running, I get redirected back to mydomain.com or get 500 or 502 error.

0 Answers
Related