<p>My asp.net core web application has redirection applied on statup.cs as:</p>
<pre><code>app.UseHttpsRedirection();
</code></pre>
<p>The problem arises when I host it on kubernetes where this redirection does not happens. The app opens on both http and https url.</p>
<p><strong>I am hosting the app on local kubernetes cluster on my pc having kali linus.</strong></p>
<p>This is the ingress which i am using:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: first-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - secretName: ssl
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: first-service
            port:
              number: 8080
</code></pre>
<p>The ingress on opens 2 ports which are - 80, 443.</p>
<p>How should the redirection be done in such a case?</p>
<p>The deployment is:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: first-dep
  labels:
    app: aspnet-core-app
spec:
  replicas: 1
  selector: 
    matchLabels:
      component: web
  template:
    metadata: 
      labels:
        component: web
    spec:
      containers:
        - name: csimpleweb
          image: simpleweb
          imagePullPolicy: Never
          ports:
            - containerPort: 80
</code></pre>
<p>The Service is:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: first-service
spec:
  type: NodePort
  selector:
    component: web
  ports:
    - port: 8080
      targetPort: 80
</code></pre>
<p>So in summary I want to ask the following:</p>
<ol>
<li>How redirection works in k8s + ASP.NET Core. The redirection does not work in my case where I have applied the redirection on the app.</li>
<li>Should the redirection be done on ingress if so how. In that case I think we have remove <strong>app.UseHttpsRedirection()</strong> from the app.</li>
<li>What docker container ports, which the pods will host, we have to open and how?</li>
</ol>
<p>Thank you</p>

<p>According to my research I found the following way.</p>
<ol>
<li>Remove the redirection from an ASP.NET Core app when we host it to kubernetes.</li>
<li>Apply the redirect to Ingress itself. By adding the following 2 lines to it:</li>
</ol>
<blockquote>
<p>nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;<br />
nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;</p>
</blockquote>
<p>So my ingress code becomes:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: first-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/force-ssl-redirect: &quot;true&quot;
    nginx.ingress.kubernetes.io/ssl-redirect: &quot;true&quot;
spec:
  tls:
  - secretName: ssl
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: first-service
            port:
              number: 8080
</code></pre>
<p>Let me know your thoughts. If you have a better answer the kindly add a new answer on the question.</p>
