Score:0

HTTPS Web Service and Backend Service on the same Server

ne flag

Is there a way for to setup a backend service to listen on HTTPS on an already existing Web Service that is already serving on HTTPS?

I originally had both being served on HTTP, but recently added a CA for the Web Service. Both work perfectly fine on HTTP, but when trying to access the backend (from port 5000) from HTTPS, I get no response (I was originally serving the backend with PM2). I needed to update the ajax calls from the front end to call the backend on HTTPS instead of HTTP.

I looked into applying NGINX for the backend, but cannot find resources for my specific situation and I have no clue how to configure NGINX to listen on port 5000 for HTTPS (which doesn't make sense to me since HTTPS is port 443).

Michael Hampton avatar
cz flag
Please post the output of `nginx -T`
Gerard H. Pille avatar
in flag
Https is not port 443, that protocol can be used on any port by client and server.
Score:0
za flag

That's a Basic request, I will answer it in generic as the User did not provide enough Information to be more precise. REMINDER: So, this will answer it in Generally.

Pre-Infromations

User tells that he wants to listen for or to port 5000 for https

Standard http to https Redirect

server {
        server_name *.domain.tld domain.tld;
        #listen for Port 80
        listen 80;
        #redirect to https
        return 301 https://$host$request_uri; 
}

Listen NGINX on port 5000

server {
        server_name *.domain.tld domain.tld;
        #listen for Port 5000, http
        listen 5000;
        #add stuff here
}

Standard https Server Directive, using Certbot for Certificate Generation

server {
        #listen on 443 for SSL (https) with http 1.0/1.1
        #listen 443 ssl;
        
        #Listen on 44 for SSL (https) with http 2
        listen 443 ssl http2;
        
        #What we listen for domain      
        server_name *.domain.tld domain.tld;
                location / {
                root /var/www/vhosts/domain.tld;
}
    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem; # managed by Certbot
}

Basic Reverse-Proxy Listen on Port 80 http AND port 443 SSL

server {
        #listen on 80 for https with http 1.0/1.1
        listen 80;
        
        #listen on 80 for https with http 2 (disabled, certbot does not understand it on port 80)
        #listen 80 http2;

        #listen on 443 for SSL (https) with http 1.0/1.1
        #listen 443 ssl;
        
        #Listen on 44 for SSL (https) with http 2
        listen 443 ssl http2;
        
        #What we listen for domain      
        server_name *.domain.tld domain.tld;
                location / {
                        proxy_pass              http://127.0.0.1:5000;

                        #https even works
                        #proxy_pass             https://127.0.0.1:5000;                            
                        proxy_set_header        Host $http_host;
}
    ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem; # managed by 
    Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.tld/privkey.pem; # managed by 
    Certbot
}

Conclusions

If you want to reach your Backend on Port 5000 you have to use proxy_pass, if you want that NGINX listen on port 5000, in case its not been used listen listen 5000 ssl;

And Also, as others already said, SSL is a Protocol and is not bound to a specific port, its commenly used 443 but it must not listen to it, i also could use 61337 for it.

I'll update the Answer in case the OP is going to update the question

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.