Reverse-proxying multiple ports to same domain with Nginx

I'm using Nginx as a reverse proxy for several HTTP services that I have in one server in different ports such as 8001, 8002 and 8003. In the past, each one had its own subdomain but now, I have one HTTPS certificate that I need to share between all the services.

Previously, a URL for a service was, but with my new cert, I need it to be, how do I do this? Well, with Nginx we can easily accomplish it, just configure your server block where you need this URL scheme as follows:

server {
    listen 443 ssl;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location ^~ /service1/ {
            rewrite ^/service1(/.*)$ $1 break;
            proxy_pass http://localhost:8001;

    location ^~ /service2/ {
            rewrite ^/service2(/.*)$ $1 break;
            proxy_pass http://localhost:8002;

    location ^~ /service3/ {
            rewrite ^/service3(/.*)$ $1 break;
            proxy_pass http://localhost:8003;

    location / {
            return 444;

Obviously, you need to change some parameters in the file to match your actual configurations and paths but there's the idea. And it won't need modifications to the services urls because Nginx is not passing the initial /service[1-3] string to the internal service.