Score:1

nginx one site redirects to another

tr flag

I have couple domains on my server. There is wordpress multisite and vanilla php site. All WP sites are working correctly but if vanilla php site redirects to main WP site. Here is config of main WP site:

map $http_host $blogid {
 1survey.cc 0;
 b-shield.icu 1;
 airlinetravel.life 2;
}




server {
    server_name 1survey.cc *.1survey.cc;
    return 301 https://$host$request_uri;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/1survey.cc/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/1survey.cc/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
}

server {
    listen 5.187.1.93:443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/1survey.cc/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/1survey.cc/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
    if ($host = www.1survey.cc) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

   root  /home/fornex/wordpress;
   index index.php;

   client_max_body_size 7m;

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

    location ~* /\. {
        deny all;
    }

   location ~*\.(php)$ {
     fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
     fastcgi_index index.php;
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     fastcgi_param PATH_INFO $fastcgi_path_info;
    }


}

server {
    if ($host = www.1survey.cc) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = b-shield.icu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 5.187.1.93:80;
    server_name 1survey.cc *.1survey.cc;
    return 404; # managed by Certbot




}

server {
    if ($host = 1survey.cc) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name 1survey.cc *.1survey.cc;
    listen 80;
    return 404; # managed by Certbot
}

Here is config of vanilla php site:

map $http_host $blogid {
 1survey.cc 1;
 b-shield.icu 0;
 airlinetravel.life 2;
 apparel.rest 3;
}


server {
    if ($host = www.b-shield.icu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = b-shield.icu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    server_name b-shield.icu *.b-shield.icu;
    return 301 https://$host$request_uri;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/1survey.cc/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/1survey.cc/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
}

server {
    if ($host = www.b-shield.icu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


listen 80;

   server_name b-shield.icu *.b-shield.icu;
   root  /home/fornex/b-shield.icu;
   index index.php;

 include /home/fornex/b-shield.icu/nginx.conf;

   client_max_body_size 7m;

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

    location ~* /\. {
        deny all;
    }

   location ~*\.(php)$ {
     fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
     fastcgi_index index.php;
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}



server {
    if ($host = b-shield.icu) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 5.187.1.93:80;
    server_name b-shield.icu *.b-shield.icu;
    return 404; # managed by Certbot


}

If I try to access to b-shield.icu it redirects to https://1survey.cc/wp-signup.php?new=b-shield.icu. What is wrong?

Score:0
jp flag

You have listen 5.187.1.93:443 ssl; in one server block and listen 443 ssl; in another.

Nginx uses the IP address part to select server blocks on servers with more than one IP address, where different IP addresses need to be handled by different server blocks.

In most cases, the IP address is not required in the listen statement.

If you have some listen statements with an IP address and some without, the more specific listen statement will be chosen, which is probably why one of your server blocks is not accepting connections.

For consistency, in all of your server blocks, use:

listen 80;

and/or:

listen 443 ssl;

See this document for details.

stack avatar
tr flag
thank you but I've completly rewrote config files 3 times and solve this problem already. Perhaps your answer will be useful for someone. Thank you!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.