<p>I got a problem with infinite redirects. I've tried looking &quot;everywhere&quot; but I couldnt find anything that would solve my problem.</p>
<p>I've got a K8s deployment with Docker image that's running an Nginx image.</p>
<p>The problem is that when I make a request to <code>http://[company]</code> I correctly get a <code>301</code> and get redirected to <code>https</code>, but the same happens when I make a request to a <code>https://[company]</code> - and it causes an infinite redirects.</p>
<p>Also, when I make a request to <code>http://[company]/healthz</code> I get <code>200</code>, which is correct, but when I make the request to <code>https://[company]/healthz</code> I also get the <code>200</code> response - which is incorrect!</p>
<p>From this behaviour I assume that the server on port <code>80</code> somehow catches all the traffic, but I cannot figure out why.</p>
<p>Please take a look on my config files - maybe it's a small mistake (<em>please bear in mind, that I'm kind of still new to Nginx and K8s and I want to understand how it works</em>).</p>
<p>Here's my <code>nginx.conf</code>:</p>
<pre><code>worker_processes auto;

events {
  worker_connections 1024;
  multi_accept on;
}

http {
  include /etc/nginx/mime.types;

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    error_log /var/www/logs/http_error.log error;

    location / {
      return 301 https://[company]$request_uri;
    }
    location /healthz {
      access_log off;
      return 200 &quot;healthy&quot;;
    }
  }

  server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;
    server_name [company];
    root /var/www;

    error_log /var/www/logs/ssl_error.log error;

    location / {
      index index.html;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $host;
      proxy_set_header X-Url-Scheme $scheme;
      proxy_redirect off;
      proxy_max_temp_file_size 0;
    }

    ssl_certificate /etc/cert/[company].crt;
    ssl_certificate_key /etc/cert/[company].key;
  }
}
</code></pre>
<p>Here's my <code>Dockerfile</code>:</p>
<pre><code>FROM nginx:alpine
...
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>Here's my <code>deployment.yaml</code>:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
    spec:
      containers:
        ...
          ports:
            - containerPort: 80
              name: http
              protocol: TCP
            - containerPort: 443
              name: https
              protocol: TCP
</code></pre>
<p>Here's my <code>ingress.yaml</code>:</p>
<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
...
spec:
  backend:
    serviceName: [service-name]
    servicePort: 80
</code></pre>
<p>At this point I really don't know why it doesn't work, so please, even if it's some dumb mistake, please feel free to point it out!
What am I doing wrong here?</p>

Esskimo
<p>What you are doing wrong here is that you are not using Ingress resource correctly. K8s Ingress is used to expose services, and also act as DNS records for your application. The nginx setup you have isn't made for running inside docker.</p>
<p>What you should do in order to have everything working is as follows:</p>
<p>1.rewrite your <code>nginx.conf</code> as follows:</p>
<pre><code>...
http {
  include /etc/nginx/mime.types;

  server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    error_log /var/www/logs/http_error.log error;

    location / {
      index index.html;
      ...
    }

    location /healthz {
      access_log off;
      return 200 &quot;healthy&quot;;
    }
  }
}
</code></pre>
<p>2.rewrite your <code>Dockerfile</code> as follows:</p>
<pre><code>...
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD [&quot;nginx&quot;, &quot;-g&quot;, &quot;daemon off;&quot;]
</code></pre>
<p>3.rewrite your <code>deployment.yaml</code> as follows:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
    spec:
      containers:
        ...
          ports:
            - containerPort: 80
              name: http
              protocol: TCP
</code></pre>
<p>4.rewrite your <code>ingress.yaml</code> as follows:</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
...
spec:
  tls:
  - hosts:
      - [company]
    secretName: [company]-tls
  rules:
  - host: [company]
    http:
      paths:
      - path: /
        backend:
          serviceName: my-service
          servicePort: 80
</code></pre>
<p>With this setup you are allowing Ingress to do it's work properly and not bear extra load inside the container as this is not necessary nor functional. After this setup you also need to add the certification files into a k8s secret resource.</p>
<p>EDIT: If you want to include the headers you had the SSL server block, I suggest into looking into way of doing this inside Ingress.</p>
