Nginx

如何使用 Nginx 在同一個 IP 地址上配置多個伺服器?

  • March 5, 2019

我從 Nginx 開始。

我安裝在我的伺服器上:

  • 1 www.example.com 上的 Drupal 站點
  • 1 Analytics.example.com 上的 GoAccess 站點
  • 1 monitoring.example.com 上的網路數據站點

IP 地址和 example.com 必須重定向到 www.example.com

所有域和子域 dovent 重定向到 https

在我的 OVH 網路主機上,我創建了域 example.com www.example.com analytics.example.com monitoring.example.com 指向我的伺服器的 IP 地址。

當我在瀏覽器的地址欄中輸入伺服器的 IP 地址時,它會重定向到 analytics.example.com 為什麼?

如何重定向到 www.example.com

在為 analytics.example.com 和 monitoring.example.com 創建配置之前,它起作用了。

當我測試我的配置時:

須藤 nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

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

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

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

   add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;
   add_header X-Content-Type-Options "nosniff" always;
   add_header X-XSS-Protection "1; mode=block" always;
   add_header X-Frame-Options "SAMEORIGIN" always;
   add_header X-Download-Options "noopen" always;
   add_header X-Permitted-Cross-Domain-Policies "none" always;
   add_header Content-Security-Policy "default-src https: data: wss: 'unsafe-inline' 'unsafe-eval'; base-uri 'self';" always;

   gzip on;
   gzip_vary on;
   gzip_proxied any;
   gzip_comp_level 6;
   gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject 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 80;
   server_name www.example.com example.com;
   return 301 https://$server_name$request_uri;
}

server {
   listen 443;
   server_name example.com;
   return 301 https://www.$server_name$request_uri;
}

https://goaccess.io/faq

sudo nano /etc/nginx/sites-available/analytics-example-com

server {
   listen 443 ssl http2;
   server_name analytics.example.com;
   root /var/www/analytics-example-com/web;
   report.html;

   auth_basic "Protected";
   auth_basic_user_file /var/www/analytics-example-com/web/.htpasswd;

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

server {
   listen 80;
   server_name analytics.example.com;
   return 301 https://$server_name$request_uri;
}

https://jesuisadmin.fr/netdata-solution-monitoring-legere-serveur-linux/

sudo nano /etc/nginx/sites-available/monitoring-example-com

upstream backend {
   server 127.0.0.1:19999;
   keepalive 64;
}

server {
   listen 443 ssl http2;
   server_name monitoring.example.com;
   root /var/www/monitoring-example-com/web;

   auth_basic "Protected";
   auth_basic_user_file /var/www/monitoring-example-com/web/.htpasswd;

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

   location / {
       proxy_set_header X-Forwarded-Host $host;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_pass http://backend;
       proxy_http_version 1.1;
       proxy_pass_request_headers on;
       proxy_set_header Connection "keep-alive";
       proxy_store off;
   }
}

server {
   listen 80;
   server_name monitoring.example.com;
   return 301 https://$server_name$request_uri;
}

您需要使用 default_server 指令。我認為您需要在一個 http 和一個 https 塊上使用它。

server {
 listen 443 ssl http2;
 listen [::]:443 ssl ipv6only=on http2;
 server_name www.domaine.com default_server;
 root /var/www/www-domaine-com/web;
 // etc
}

server {
 listen 80;
 server_name www.domaine.com default_server;
 root /var/www/www-domaine-com/web;
 // etc
}

在我的伺服器上,我拒絕所有對沒有域的 IP 的請求,我看不到為這些請求提供服務有任何價值。

server {
 listen      80 default_server;
 server_name _; # Wildcard, any domain is served
 return      444; # This means "go away", effectively
 access_log off; log_not_found off;
}

由於 SSL/TLS 需要有效的域名證書,我認為使用 https 不可能做到這一點。

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