NGINX

Redirect www and non-www http to non-www https on NGINX

How to set up redirect www and non-www http to non-www https

o nginx config file

vim /etc/nginx/sites-enabled/blackonsole.conf

# http
server {
  listen      80;
  server_name blackonsole.org www.blackonsole.org;
  return      301 https://blackonsole.org$request_uri;
}

# https
server {
  listen 443 ssl;
  server_name blackonsole.org;

  # redirect to non-www
  if ($host = 'www.blackonsole.org') {
    rewrite ^/(.*)$ https://blackonsole.org/$1 permanent;
  }
 
  # others
}

o testing config file and restart nginx

nginx -t
nginx -s reload
Hi, I’m Sysadmin.ID

Leave a Reply

Your email address will not be published. Required fields are marked *