Redirecting with Nginx

1

I am learning to use Nginx for a new client. I am using DigitalOcean as a VM provider, I have Nginx installed and correctly configured with PHP. The problem comes when I want to add the redirection 301 in the Nginx server, because when I add return 301 $scheme://dominio.com$request_uri; when I go to the page in the internet browser, it returns this:

However, when removing this line from the server configuration, I can correctly enter the page without any problem.

What I want to do is redirect the domain www to one without the www .

On the other hand, in the digitalocean networking configuration I tried both having the www and a CNAME field and having deleted it, and in neither of the two ways it works, could someone tell me how to perform the configuration correctly?

I enclose the configuration block of my server:

server {
    listen 80;
    listen 443 ssl default_server;
    server_name www.domain.com;
    return 301 $scheme://domain.com$request_uri;

    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    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:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-$
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;

    root /var/www/domain.com/html/domain;
    index index.php index.html index.htm;

    location / {
            try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
            root /usr/share/nginx/html;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~ /.well-known {
            allow all;
    }
}

For security reasons, change the name of my domain to "domain.com"

    
asked by Ata Sanchez 21.11.2016 в 18:40
source

2 answers

3

You have to create two separate server servers.

Read the documentation Converting Rewrite Rules

server {
    listen 80;
    listen 443 ssl default_server;
    server_name www.domain.com;
    return 301 $scheme://domain.com$request_uri;
}

server {
    listen 80;
    listen 443 ssl default_server;
    server_name domain.com; # Aquí va sin www

    # Resto de configuración
}
    
answered by 21.11.2016 / 18:51
source
0

Thanks for the response, implement these changes and it works correctly, however in the moment of passing the nginx test it tells me that port 443 is duplicated, therefore in the second server block remove the "default_server" clause "and everything was solved.

Thanks guys!

    
answered by 23.11.2016 в 20:12