1

I'm trying to investigate the issue redirect when using www and HTTP link. Currently, if I use HTTP link with www.domain then it will redirect 2 times 301 > 302 > 200. I check via site: https://wheregoes.com/trace/20222001706/ enter image description here The remain case it just redirect 1 time from HTTP to https

This is my nginx config:

server {
  listen 443 ssl http2;
  listen [::]:443 ssl;
  ssl_certificate /root/ssl/ins_multi_domain_new_2021_to_2022_cert_chain.crt;
  ssl_certificate_key /root/ssl/ins_multi_domain_new_2021_to_2022.com.key;

  server_name ins.com www.ins.com *.ins.com;
  root /var/www/html/ins;
  index index.php index.html index.htm index.nginx-debian.html;
  try_files $uri $uri/ /index.php?$args;

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include snippets/fastcgi-php.conf;
    fastcgi_connect_timeout 300s;
    fastcgi_read_timeout 300s;
  }

  # disable access to hidden files
  location ~ /\.ht {
     # access_log off;
    #  log_not_found off;
      deny all;
  }

 #browser caching of static assets
    location ~*  \.(jpg|jpeg|png|gif|ico)$ {
        expires 7d;
    }

    #browser caching of css and js
    location ~*  \.(css|js)$ {
        expires 14d;
    }

    gzip on;
    gzip_disable "msie6";

    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 4 32k;
    gzip_proxied any;
    gzip_vary on;
    gzip_static on;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/x-component
        text/javascript
        application/javascript
        application/json
        application/xml
        application/rss+xml
        font/truetype
        font/opentype
        application/vnd.ms-fontobject
        image/svg+xml;

  #for varnish
    #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 24h;
    keepalive_timeout 300s;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;
}


server {
  listen 80;
  server_name ins.com www.ins.com;
  root /var/www/html/ins;
  index index.php index.html index.htm index.nginx-debian.html;
  try_files $uri $uri/ /index.php?$args;
  client_max_body_size 100M;
  return 301 https://$host$request_uri;

location / {
          proxy_pass http://127.0.0.1:6081/;
          proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Ssl-Offloaded "1";
                proxy_set_header      X-Forwarded-Port 443;
                proxy_set_header X-Forwarded-Proto https;
                proxy_set_header X-Forwarded-Ssl on;
}
}

upstream fastcgi_backend {
  server   unix:/run/php/php7.2-fpm.sock;
}

Does anyone have experience with these issues? pls help

1 Answer 1

0
  • There is a http to https redirect (First one)
  • There is a www to non-www redirect (Second one)

Make sure you have 1 redirect which point instantly to the right variant.

Typically, there are 4 entry points:

  1. http www
  2. http non-www
  3. https www
  4. https non-www

In your case you want to use a https non-www one I guess? You need to pick the one you want to use, and redirect all other (with one rule) to it.

So in your case, redirect variants 1, 2 and 3 to variant 4.

2
  • Yes, I want to redirect every url to point #4 ins.com So, May I know the rule or edit code nginx for that?
    – the light
    Apr 13, 2022 at 10:05
  • I am not an Nginx expert. Did you search for nginx redirect all to https non-www? Take a look here, first hit is what you are looking for I guess? serverfault.com/questions/258378/…
    – Akif
    Apr 13, 2022 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.