<p><code>kubectl port-forward service/nginx 8080:443</code></p>
<pre><code>Forwarding from 127.0.0.1:8080 -&gt; 443
Forwarding from [::1]:8080 -&gt; 443
Handling connection for 8080
E0314 16:38:47.834517   25527 portforward.go:406] an error occurred forwarding 8080 -&gt; 443: error forwarding port 443 to pod bb5e0e3b270881ce659aa820d29dd47170e229abb90fb128e255467a8dba606a, uid : failed to execute portforward in network namespace &quot;/var/run/netns/cni-5ef7f945-3c15-25c0-8540-39513d9d3285&quot;: failed to connect to localhost:443 inside namespace &quot;bb5e0e3b270881ce659aa820d29dd47170e229abb90fb128e255467a8dba606a&quot;, IPv4: dial tcp4 **127.0.0.1:443: connect: connection refused IPv6 dial tcp6 [::1]:443: connect: connection refused
E0314 16:38:47.834846   25527 portforward.go:234] lost connection to pod**
</code></pre>
<p><code>the same working with port 80</code></p>

newbie
<p>The command used is as follows:</p>
<pre><code>kubectl port-forward pod/&lt;pod-name&gt; &lt;local-port&gt;:&lt;exposed-port&gt;
</code></pre>
<p>where <strong>local-port</strong> is the port from which the container will be accessed from the browser while the <strong>exposed-port</strong> is the port on which the container listens to which is defined with the <strong>EXPOSE</strong> command  in the Dockerfile.</p>
<p>The error <strong>failed to connect to localhost:443</strong> resembles that there is no process listening on <strong>port 443</strong>. The dockerfile used to create the nginx image exposes port 80 and nginx is configured to listen on <strong>port 80</strong> by <strong>default</strong>.</p>
<p>In order to establish a connection using port 443,please change the ngnix config file to listen on port 443 instead of port 80. Also, please update the docker image with the corresponding changes.</p>
<p>As you have not shared the nginx configuration file, the default <strong>nginx.conf</strong> file will be as follows:</p>
<pre><code>server { 
    listen  ${PORT}; 
    server_name localhost;
 
    location / { 
        root   /usr/share/nginx/html; 
        index index.html index.htm; 
    } 


    include /etc/nginx/extra-conf.d/*.conf; 
}
</code></pre>
<p>Please update the <strong>{PORT}</strong> to <strong>443</strong> in the above template and in DockerFile please add the below line:</p>
<pre><code>COPY ./nginx.conf /etc/nginx/conf.d/default.conf
</code></pre>
<p>For more details please look into the below documentations:</p>
<p><a href="https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/" rel="nofollow noreferrer">Link_1</a> <a href="https://www.golinuxcloud.com/kubectl-port-forward/" rel="nofollow noreferrer">Link_2</a></p>
