Nginx

nginx 一個站點重定向到另一個站點

  • May 18, 2022

我的伺服器上有幾個域。有 wordpress 多站點和香草 php 站點。所有 WP 站點都正常工作,但如果 vanilla php 站點重定向到主 WP 站點。這是主要 WP 站點的配置:

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
}

這是香草 php 網站的配置:

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


}

如果我嘗試訪問b-shield.icu它會重定向到https://1survey.cc/wp-signup.php?new=b-shield.icu。怎麼了?

你有listen 5.187.1.93:443 ssl;一個server街區和listen 443 ssl;另一個街區。

Nginx 使用 IP 地址部分來選擇server具有多個 IP 地址的伺服器上的塊,其中不同的 IP 地址需要由不同的server塊處理。

在大多數情況下,聲明中不需要 IP 地址listen

如果您有一些listen帶有 IP 地址的語句和一些沒有 IP 地址的listen語句,則會選擇更具體的語句,這可能就是您的一個server塊不接受連接的原因。

為了保持一致性,在所有server塊中,使用:

listen 80;

和/或:

listen 443 ssl;

有關詳細資訊,請參閱此文件

引用自:https://serverfault.com/questions/1101242