我从 Nginx 开始。
我在我的服务器上安装了:
- www.example.com 上的 1 个 Drupal 站点
- analytics.example.com 上的 1 个 GoAccess 网站
- 1 个 Netdata 站点,位于monitoring.example.com
IP 地址和 example.com 必须重定向到 www.example.com
所有域名和子域名均重定向至 https
在我的 OVH 网络主机上,我创建了指向我服务器 IP 地址的域 example.com www.example.com analytics.example.com surveillance.example.com 。
当我在浏览器的地址栏中输入我的服务器的 IP 地址时,它会重定向到 analytics.example.com,为什么?
如何重定向到 www.example.com?
在为 analytics.example.com 和 surveillance.example.com 创建配置之前,它是有效的。
当我测试我的配置时:
sudo 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;
}
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;
}
答案1
您需要使用 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 来执行此操作。