title: Nginx redirect crash after testing a few times
body: <p>Currently i have 2 server-blocks, server block 1 and server block 2, my goal is to make it such that server block 1 only listen and serve over port 2206 and server block 2 only listen and serve over port 8206 , i have closed port 80 and open port 2206 and 8206.</p>
  <p>I inputted this code into my nginx block so that it will redirect anything that is not server-block-1 to port: 8206 and vice versa​</p>
  <pre><code>if($host != server-block-1.test.com) {
  return 301 http://$host:8206$request_uri; }
  </code></pre>
  <p>Below is the full serverBlock code for both blocks</p>
  <p>serverBlock1</p>
  <pre><code># Default server configuration
  #
  server {
  
          root /var/serverblock1/test.ml;
  
          # Add index.php to the list if you are using PHP
          index index.php index.html index.htm index.nginx-debian.html;
  
          server_name server-block-1.test.ml;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  try_files $uri $uri/ =404;
          }
  
          # pass PHP scripts to FastCGI server
          #
          location ~ \.php$ {
                  include snippets/fastcgi-php.conf;
  
                  # With php-fpm (or other unix sockets):
                  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          #       # With php-cgi (or other tcp sockets):
          #       fastcgi_pass 127.0.0.1:9000;
          }
  
          # deny access to .htaccess files, if Apache's document root
          # concurs with nginx's one
          #
          location ~ /\.ht {
                  deny all;
          }
  
      listen [::]:2206 ssl ipv6only=on; # managed by Certbot
      listen 2206 ssl; # managed by Certbot
      ssl_protocols TLSv1.2 TLSv1.3;
      ssl_certificate /etc/letsencrypt/live/server-block-1.test.ml/fullchain.pem; # managed by Certbot
      ssl_certificate_key /etc/letsencrypt/live/server-block-1.test.ml/privkey.pem; # managed by Certbot
      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
      error_page 497 =301 https://$host:$server_port$request_uri; # redirect http to https
      if ($host != server-block-1.test.ml) {
          return 301 http://$host:8206$request_uri;
      }
  
  }
  
  
  # Virtual Host configuration for example.com
  #
  # You can move that to a different file under sites-available/ and symlink that
  # to sites-enabled/ to enable it.
  #
  #server {
  #       listen 80;
  #       listen [::]:80;
  #
  #       server_name example.com;
  #
  #       root /var/www/example.com;
  #       index index.html;
  #
  #       location / {
  #               try_files $uri $uri/ =404;
  #       }
  #}
  
  server {
      if ($host = server-block-1.test.ml) {
          return 301 https://$host$request_uri;
      } # managed by Certbot
  
  
          listen 80;
          listen [::]:80;
  
          server_name server-block-1.test.ml;
      return 404; # managed by Certbot
  
  
  }
  </code></pre>
  <p>serverBlock2</p>
  <pre><code># Default server configuration
  #
  server {
  
          listen 8206 http2;
          listen [::]:8206 http2;
  
          root /var/serverblock2/test.ml;
  
          # Add index.php to the list if you are using PHP
          index index.php  index.html index.htm index.nginx-debian.html;
  
          server_name server-block-2.test.ml;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  try_files $uri $uri/ =404;
          }
  
          # pass PHP scripts to FastCGI server
          #
          location ~ \.php$ {
                  include snippets/fastcgi-php.conf;
          #
                  # With php-fpm (or other unix sockets):
                  fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          #       # With php-cgi (or other tcp sockets):
          #       fastcgi_pass 127.0.0.1:9000;
          }
  
          # deny access to .htaccess files, if Apache's document root
          # concurs with nginx's one
          #
          location ~ /\.ht {
                  deny all;
          }
          if ($host != server-block-2.test.ml) {
                  return 301 https://$host:2206$request_uri;
          }
  
  }
  
  
  # Virtual Host configuration for example.com
  #
  # You can move that to a different file under sites-available/ and symlink that
  # to sites-enabled/ to enable it.
  #
  #server {
  #       listen 80;
  #       listen [::]:80;
  #
  #       server_name example.com;
  #
  #       root /var/www/example.com;
  #       index index.html;
  #
  #       location / {
  #               try_files $uri $uri/ =404;
  #       }
  #}
  </code></pre>
  <p>It works to a certain extent where it directs but after testing a few times, the web page crashes and it will recover after a while.</p>
  <p>Nginx error log</p>
  <pre><code>2021/03/19 04:00:20 [info] 23230#23230: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:00:25 [notice] 23674#23674: signal process started
  2021/03/19 04:05:53 [info] 30543#30543: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:05:57 [notice] 30785#30785: signal process started
  2021/03/19 04:13:18 [info] 12175#12175: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:13:31 [notice] 13520#13520: signal process started
  2021/03/19 04:30:01 [info] 20276#20276: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:30:06 [notice] 20773#20773: signal process started
  2021/03/19 04:36:57 [info] 31584#31584: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:37:05 [notice] 32370#32370: signal process started
  2021/03/19 04:45:03 [info] 17694#17694: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:45:06 [notice] 18009#18009: signal process started
  2021/03/19 04:48:34 [info] 7388#7388: Using 32768KiB of shared memory for nchan in /etc/nginx/nginx.conf:63
  2021/03/19 04:48:39 [notice] 7884#7884: signal process started
  </code></pre>
  <p>Would like to ask why would it crash and is there any way to prevent it?</p>