Question

How to redirect domain.com to https://domain.com NGINX

Hello everyone,

I’ve been struggling in the last 2 weeks to make my domain accessible, but didn’t find a solution yet. I am running an Ubuntu VPS with installed Nginx, Varnish and Redis for Magento 2.

The website can be reached only if I hit - https://domain.com/ but if I try domain.com or http://domain.com it returns “Hmmm… can’t reach this page” I am using multidomain option as I have set up 2 different domains - domain.com and domain.eu

I am sharing you the configuration of my nginx at /etc/nginx/sites-enabled/mywebsite.com:

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

map $http_host $MAGE_RUN_CODE {
    default '';
    domain.eu viewinternational;
    domain.com en;
}

server {
# this is the main server conf for port 443
  server_name domain.com domain.eu;
  listen 443 ssl http2;
  client_max_body_size 20M;
  # OCSP stapling
  ssl_stapling on;
  ssl_stapling_verify on;

    set $cors_credentials '';
    set $cors_content_type '';
    set $cors_content_length '';

    if ($http_origin ~ '.+') {
        set $cors_credentials 'true';
    }

    if ($request_method = OPTIONS) {
        set $cors_content_type 'text/plain';
        set $cors_content_length '0';
    }

    # empty header will not be added
    add_header Access-Control-Allow-Origin $http_origin always;
    add_header Access-Control-Allow-Credentials $cors_credentials always;
    add_header Access-Control-Allow-Methods 
$http_access_control_request_method always;
    add_header Access-Control-Allow-Headers $http_access_control_request_headers always;
    add_header Content-Type $cors_content_type;
    add_header Content-Length $cors_content_length;

    if ($request_method = OPTIONS) {
        return 204;
   }

  # this is for Varnish with listers to 6081 by default you must make sure tha varnish looks for backend at port 8080. Check config at the end of the file
  location / {
     proxy_pass http://127.0.0.1:6081;
     proxy_set_header Host $http_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-Proto https;
     proxy_set_header X-Forwarded-Port 443;
     proxy_http_version 1.1;
     proxy_buffer_size 128k;
     proxy_buffers 4 256k;
     proxy_busy_buffers_size 256k;
     fastcgi_buffer_size 32k;
     fastcgi_buffers 4 32k;
     proxy_connect_timeout 600;
     proxy_send_timeout 600;
     proxy_read_timeout 600;
     send_timeout 600;
  }
  
# this is with let's encrypt, here you can set your own paths for SSL certs
    ssl_certificate /etc/letsencrypt/live/domain.eu-0001/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/domain.eu-0001/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
# this makes nginx to server directly media files it's faster than to go through Varnish

  location /media/ {
    auth_basic off;
    proxy_pass http://127.0.0.1:6081;
    proxy_set_header Host $http_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-Proto https;
    proxy_set_header X-Forwarded-Port 443;
    proxy_http_version 1.1;
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 4 32k;
 }

}

# this is the main server conf for serving magento. This should be set as the varnish backend

server {
  server_name domain.com domain.eu;
  listen 127.0.0.1:8080;
  set $MAGE_ROOT /var/www/html;
  set $MAGE_MODE developer;
  set $MAGE_RUN_TYPE store;
  include /var/www/html/nginx.conf;
  client_max_body_size 20M;
}

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 2, 2021
Accepted Answer

Hello,

As far as I can see you only have a server block for port 443 (HTTPS) and there is no server block for port 80 (HTTP), this is why when you access your website over HTTP you get the site can not be reached message.

To fix that, you could add a server block in the beginning of the configuration file as follows:

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

All that this would do is to listen on port 80 and redirect the traffic to port 443.

Hope that this helps. Best, Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more