0

I want to redirect domain.com to www.domain.com on my Nginx server. But me configuration does not work and displays the error message in the internet browser :

The page is not redirected correctly

I added this line to do the redirection :

return 301 $scheme://www.s1biose.com$request_uri;

How to correct this error ? Here is my configuration :

server {
    #listen 80;
    #listen [::]:80 ipv6only=on;
    server_name s1biose.com www.s1biose.com;
    return 301 $scheme://www.s1biose.com$request_uri;
    root /var/www/www-s1biose-com/web;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }

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

    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri /index.php?$query_string;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    location ~ ^/sites/.*/files/styles/ {
        try_files $uri @rewrite;
    }

    location ~ ^(/[a-z\-]+)?/system/files/ {
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }

    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    if ($host = www.s1biose.com) {
        return 301 https://$host$request_uri;
    }


    if ($host = s1biose.com) {
        return 301 https://$host$request_uri;
    }

    listen 80;
    listen [::]:80;
    server_name s1biose.com www.s1biose.com;
    return 404;
}
ML19
  • 91
  • 1
  • 2
  • 8

1 Answers1

2

Your "return 301" line is being run whether the site is accessed using www or not, causing the browser to be redirected endlessly. To only return a 301 for site.com, remove the "return 301" from your main server block, only have it listen for www.site.com, and create a separate server block for site.com.

server {
    #listen 80;
    #listen [::]:80 ipv6only=on;
    server_name www.s1biose.com;
    root /var/www/www-s1biose-com/web;

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~* \.(txt|log)$ {
        allow 192.168.0.0/16;
        deny all;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }

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

    location ~ (^|/)\. {
        return 403;
    }

    location / {
        try_files $uri /index.php?$query_string;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    }

    location ~ ^/sites/.*/files/styles/ {
        try_files $uri @rewrite;
    }

    location ~ ^(/[a-z\-]+)?/system/files/ {
        try_files $uri /index.php?$query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        try_files $uri @rewrite;
        expires max;
        log_not_found off;
    }

    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
    # redirect to www
    listen [::]:443;
    listen 443;
    ssl_certificate /etc/letsencrypt/live/s1biose.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/s1biose.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    server_name s1biose.com;
    return 301 https://www.s1biose.com$request_uri;
}

server {
    # force https
    listen [::]:80 ipv6only=on;
    listen 80;
    server_name s1biose.com www.s1biose.com;
    return 301 https://www.s1biose.com$request_uri;
}
Jesse C
  • 63
  • 3
  • It does not work `nginx: [emerg] duplicate listen options for [::]:443 in /etc/nginx/sites-enabled/www-s1biose-com:91` – ML19 Feb 28 '19 at 16:45
  • Forgot to remove the port options on listen directive. Port options should only be specified once per port. – Jesse C Mar 02 '19 at 05:07
  • I used `include snippets/ssl-params.conf;` which also has a `ssl_dhparam` in it. – malhal May 11 '23 at 10:05
  • @Jesse C Could you please update your answer following your last comment. I am also getting the same error as ML19, and I am not sure by what you mean by removing the port options. – Jean-François Beauchamp Jun 15 '23 at 17:27
  • @JesseC In fact, in your second server block, I believe you should have ` listen [::]:443 ssl; listen 443 ssl;` Otherwise, there will be an error saying that the port options have been redefined. – Jean-François Beauchamp Jun 15 '23 at 17:48