K
K
kernUSR2016-12-04 15:39:21
Digital certificates
kernUSR, 2016-12-04 15:39:21

How to redirect https from www to non-www (Nginx)?

There is a server on Nginx (1.8.1). Access is carried out both by ip and by domain name.
We bought an ssl certificate https://www.reg.ru/ssl-certificate/GlobalSign/gs_a... (the one without a Wildcard).
I set up a redirect from host_ip , www.mydomain.ltd , mydomain.ltd to https://mydomain.ltd without problems.
Now there was a problem with the redirect from the addresses https://host_ip , https://www.mydomain.ltd to https://mydomain.ltd
In the nginx config, I wrote the following code before the main rules section:

server {
    listen 443 default_server ssl;
    server_name www.mydomain.ltd;
    return 301 https://mydomain.ltd$request_uri ;
}
server {
                listen 443 ssl;
                server_name mydomain.ltd;
                ....
}

But the redirect doesn't work. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TyzhSysAdmin, 2016-12-04
@POS_troi

In the first section just

server{
  server_name www.mydomain.ltd;
  return 301 $scheme://mydomain.ltd$request_uri;
}

And clear your browser cache.

K
kernUSR, 2016-12-04
@kernUSR

Full config of my server
s1.conf file

# Default website
        server {

                listen 80 default_server;
                server_name www.mydomain.ru  mydomain.ru;
                return 301 https://mydomain.ru$request_uri ;
                server_name_in_redirect off;

                proxy_set_header        X-Real-IP        $remote_addr;
                proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header        Host $host:80;

                set $proxyserver        "http://127.0.0.1:8888";
                set $docroot            "/home/bitrix/www";

                index index.php;
                root /home/bitrix/www;

                # Redirect to ssl if need
                #if (-f /home/bitrix/www/.htsecure) { rewrite ^(.*)$ https://$host$1 permanent; }

                # Include parameters common to all websites
                include bx/conf/bitrix.conf;

                # Include server monitoring locations
                include bx/server_monitor.conf;
        }

ssl.s1.conf file
server{
                listen 443;
                server_name www.mydomain.ru;
                return 301 $scheme://mydomain.ru$request_uri;
        }
        server {
                listen 443 default_server ssl;
                server_name mydomain.ru;

                # Enable SSL connection
                include bx/conf/ssl.conf;
                server_name_in_redirect off;

                proxy_set_header        X-Real-IP       $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header        Host            $host:443;
                proxy_set_header        HTTPS           YES;

                set $proxyserver        "http://127.0.0.1:8888";
                set $docroot            "/home/bitrix/www";

                index index.php;
                root /home/bitrix/www;

                # Include parameters common to all websites
                include bx/conf/bitrix.conf;

                # Include server monitoring API's
                include bx/server_monitor.conf;

        }

ssl.conf file
# If they come here using HTTP, bounce them to the correct scheme
# Nginx internal code used for the plain HTTP requests
# that are sent to HTTPS port to distinguish it from 4XX in a log and an error page redirection.
error_page 497 https://$host$request_uri;

# Increase keepalive connection lifetime
keepalive_timeout       70;
keepalive_requests      150;

# SSL encryption parameters
ssl                     on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:D$
ssl_prefer_server_ciphers on;

ssl_certificate         /etc/nginx/ssl/mydomain.ru.pem;
ssl_certificate_key     /etc/nginx/ssl/private.key;

# performance
ssl_session_cache       shared:SSL:10m;
ssl_session_timeout     10m;

bx/conf/bitrix.conf and bx/server_monitor.conf did not touch - they are default
The solution is on bitrixVM (based on CentOS)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question