M
M
Mikhail Livach2018-08-13 12:04:20
Nginx
Mikhail Livach, 2018-08-13 12:04:20

How do you get an invalid redirect in nginx with an incorrect resolver?

Task: there is an example.com website with two subdomains: ru.example.com , en.example.com .
It is required to provide redirects from http ( with www and without ) to https without www , and from https://www.* to https without www , there is no
DNS server on the host.
it turned out that if you add a line to the config: then redirects start to work incorrectly: from https://example.com it redirects to https://en.example.com . Can anyone explain why? And why exactly on en.example.com - because it is the last one in the server_name directive? I give the config below
resolver 127.0.0.1;

server {
    listen   80; ## listen for ipv4
    server_name ~^(www\.)?(ru\.|en\.)?(example.com)$;

    location /.well-known/acme-challenge/ {
        alias /var/www/letsencrypt/;
    }

    location / {
        rewrite ^ https://$2$3$request_uri permanent;
    }
}

server {
    listen 250.250.250.250:443 ssl;
    server_name ~^(www\.)(ru\.|en\.)?(example.com)$;
    access_log off;

    include snippets/example.com.ssl;

    expires max;
    return 301 https://$2$3$request_uri;
}

server {

    server_name example.com ru.example.com en.example.com;
    listen 250.250.250.250:443 ssl;

    include snippets/example.com.ssl;

    root           /var/www/example.com/public;

    # Point index to the Laravel front controller.
    index           index.php;

    # Remove trailing slash to please routing system.
    if (!-d $request_filename) {
        rewrite     ^/(.+)/$ /$1 permanent;
    }
    
    location /.well-known/acme-challenge/ {
        alias /var/www/letsencrypt/;
    }

    location / {
        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$args;
    }

    # PHP FPM configuration.
    location ~* \.php$ {
            fastcgi_pass                    unix:/run/php/php7.2-fpm.sock;
            fastcgi_index                   index.php;
            fastcgi_split_path_info         ^(.+\.php)(.*)$;
            include                         /etc/nginx/fastcgi_params;
            fastcgi_param                   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question