虽然不是 NGINX 专家,但我已做出我认为有效的更改。这是 PROD 服务器,因此不要只是将其推送到它并希望获得最佳效果。
我想要做的是让子域名和域名都重定向到各自的页面。例如,当有人输入https://example.com我需要它来做https://example.com当有人进入时https://learning.example.com我需要它去https://learning.example.com最后当有人进入https://www.learning.example.com我需要服务器重定向到https://learning.example.com.如果这一切都有道理的话。
我相信下面的代码(但可能是错的)应该可以工作,但如果有人能审查它并修复可能导致的潜在问题,我将不胜感激。
server {
listen 80;
listen [::]:80;
server_name *.example.com;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
return ^ https://$host$request_uri? permanent;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES8:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name *.example.com;
server_tokens off;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 500;
gzip_buffers 4 32k;
gzip_types text/plain text/css application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml image/svg;
gzip_vary on;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_buffer_size 8k;
ssl_dhparam /etc/ssl/certs/dhparam-2048.pem;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
ssl_ecdh_curve secp384r1;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8;
location ~* \.(jpg|jpeg|png|gif|ico)$ {
expires 365d;
}
location / {
try_files $uri @nodejs;
}
location @nodejs {
proxy_pass http://nodejs:8080;
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;
# enable strict transport security only if you understand the implications
}
location /graphql {
proxy_pass http://nodejs:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
}
答案1
你的第二个区块有错误server
。
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.example.com;
...
return 301 https://$host$request_uri;
}
$host
在这种情况下将解析为server_name
,这意味着这将导致重定向循环。
要从www.
前缀重定向到没有前缀的主机,您需要使用正则表达式server_name
:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^www\.(?<domain>.+)$;
...
return 301 https://$domain$request_uri;
}
正则表达式用于捕获裸域名,然后发出到实际域的重定向。