Ubuntu

我伺服器的所有 IP 都重定向到我的站點

  • March 16, 2019

我有一個帶有主 IP(伺服器的預設 IP)和兩個附加 IP 的伺服器。

問題是我的伺服器的所有 IP 都重定向到我的站點。

我只希望主 IP 以 IPv6 和 IPv4 重定向到我的站點。

這是我的配置:

$ sudo nano /etc/netplan/50-cloud-init.yaml

network:
   version: 2
   ethernets:
       ens3:
           dhcp4: true
           dhcp6: no
           addresses:
               - 2001:41d0:666:1000::a5e/64
               - 164.532.101.48/32
               - 164.532.101.49/32
           gateway6: 2001:41d0:666:1000::1
           routes:
               - to: 2001:41d0:666:1000::a5e/64
                 via: 2001:41d0:666:1000::1
           match:
               macaddress: fa:96:9e:f0:27:b2
           set-name: ens1

$ sudo nano /etc/nginx/sites-available/www-example-com

server {
   listen 80 default_server;
   listen [::]:80 default_server;
   server_name example.com www.example.com;

   location ^~ /.well-known/acme-challenge/ {
       default_type "text/plain";
       root /var/www/letsencrypt;
   }

   location / {
       return 301 https://www.example.com$request_uri;
   }
}

server {
   listen 443 ssl http2;
   listen [::]:443 ssl http2;
   server_name www.example.com;
   root /var/www/www-example-com/web;
   index index.php;

   ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
   include /etc/letsencrypt/options-ssl-nginx.conf;
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

   add_header X-XSS-Protection "1; mode=block" always;
   add_header Referrer-Policy "no-referrer-when-downgrade" always;
   add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

   gzip on;
   gzip_vary on;
   gzip_proxied any;
   gzip_comp_level 6;
   gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;

   brotli on;
   brotli_comp_level 6;
   brotli_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;

   expires 1209600s;

   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)$ {
       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' {
       expires off;
       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;
   }
}

server {
   listen 443 ssl http2;
   listen [::]:443 ssl http2;
   server_name example.com;

   ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
   include /etc/letsencrypt/options-ssl-nginx.conf;
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

   add_header X-Frame-Options "SAMEORIGIN" always;
   add_header X-XSS-Protection "1; mode=block" always;
   add_header X-Content-Type-Options "nosniff" always;
   add_header Referrer-Policy "no-referrer-when-downgrade" always;
   add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
   add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

   location / {
       return 301 https://www.example.com$request_uri;
   }
}

您應該重新啟用 Debian 建構的 nginx 附帶的預設伺服器,並default_serverserver主機塊中刪除您實際上不想成為預設伺服器的伺服器。在此之後,對您的 IP 地址的請求只會獲得“它可以工作”頁面(如果需要,您可以稍後更改)。

似乎 Nginx 正在偵聽您的所有介面:

了解 nginx ’listen’ 指令的不同值

如果您要求您的網路伺服器僅監聽某個地址,請通過此處的文件中的 listen 指令指定它們:

http://nginx.org/en/docs/http/ngx_http_core_module.html#listen

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