6

I have a question related with Nginx redirects Bellow you can see configurations. My goal is to redirect from https://example.com to https://www.example.com

I looked through almost all in stackoverflow and I didn't find any help. Please help me with this issue. I will provide all necessary information about my Nginx Web Server. I hope you will help me, with this difficult question.

My file nginx.conf looks like there:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections 768;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  server_names_hash_bucket_size 64;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;
  gzip_static on;
  gzip_disable "msie6";

  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 9;
  # gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xm$

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

My file /etc/nginx/sites-enabled/example:

server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name www.example.com;
  ssl_stapling on;
  ssl on;
  ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot

  root /var/www/example/public;
  index ../views/index.html;

  location /img/ {
    proxy_pass http://127.0.0.1:3010;
    proxy_cache off;
    proxy_cache_key "$proxy_host$uri$is_args$args";
  }

  location / {
    proxy_pass http://127.0.0.1:3010;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

  location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|css|js|html)$ {
    root /var/www/example/public;
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
  }
}
1
  • Do you have registered subdomain www in your DNS config?
    – Igor Cova
    Jul 23, 2018 at 13:55

2 Answers 2

13

Just create a server for non-www requests, for example:

# redirect http to https
server {
    listen 80;
    server_name www.example.com example.com;
    return 301 https://www.example.com$request_uri;
} 

# redirect http://example.com to https://www.example.com
server {
    listen 443 ssl;
    server_name example.com;

    # ssl ...

    return 301 https://www.example.com$request_uri;
}

# https://www.example.com
server {
    listen 443 ssl;
    server_name www.example.com;

    # ssl ...
}

The DNS records for example.com and www.example.com should be pointing to your Nginx server

10
  • Output logs: This site can’t be reached example.com is unreachable. ERR_ADDRESS_UNREACHABLE
    – Min MIna
    Jul 23, 2018 at 13:50
  • @MinMIna can you resolve (DNS) the domains within your instance/ server?
    – nbari
    Jul 23, 2018 at 13:56
  • I am sorry but I don't know how it is done. Can you help me how to resolve DNS?
    – Min MIna
    Jul 23, 2018 at 13:58
  • Go to the site when your site domain name registered and see instruction how to see and change subdomains yours site
    – Igor Cova
    Jul 23, 2018 at 14:00
  • 1
    @MinMIna just ensure that your domain records @ and www at least are pointing to your Nginx server IP
    – nbari
    Jul 23, 2018 at 14:10
2

Quick instruction for redirect and also for ssl

Don't write all conf all your sites in one file nginx.conf. Separate these. You have two folders for it /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/

Add file for your site for example /etc/nginx/sites-available/example Make link ln -s /etc/nginx/sites-enabled/example

To this conf file paste text below:

server {
  listen 80;
  server_name example.com www.cova.company;
  return 301 https://www.example.company$request_uri;
}

server {
  listen 443 ssl;
  server_name www.example.com;
  ssl_stapling on;
  ssl on;
  ssl_certificate /etc/letsencrypt/live/www.site.com/fullchain.pem;     
  ssl_certificate_key /etc/letsencrypt/live/www.site.com/privkey.pem;
  # your location there a
}

In you nginx.conf ypu already have row include /etc/nginx/sites-enabled/*; it means automatically take all of their sites configs from folder sites-enabled

After it check syntax with command nginx -t and reload your nginx with command systemctl reload nginx

And after all off this who call your site via http://example.com or https://example.com will be redirected to https://www.example.com

10
  • Unfortunately, I have done as you wrote here and anything happened. I enter command in terminal: curl -I example.com and get logs: curl: (7) Failed to connect to example.com port 443: No route to host But I used service nginx restart instead of systemctl reload nginx command
    – Min MIna
    Jul 23, 2018 at 13:01
  • Do you have an ssl certificate?
    – Igor Cova
    Jul 23, 2018 at 13:03
  • Sure, here path: ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
    – Min MIna
    Jul 23, 2018 at 13:06
  • Did you called nginx -t with no warnings or errors?
    – Igor Cova
    Jul 23, 2018 at 13:07
  • username@name:/etc/nginx/sites-enabled# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
    – Min MIna
    Jul 23, 2018 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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