我想将所有请求重定向到 example.org。我创建了一个如下所示的配置文件。我可以将请求重定向到:
http anything.example.org 到 https example.org
https anything.example.org 至 https example.org
但有一个问题我无法解决
http example.org 到 https example.org
你能帮我找出棘手的部分吗(我认为是 server_name 部分)
server {
error_log /opt/domain/web/logs/error.log;
access_log /opt/domain/web/logs/access.log;
root /opt/domain/web/html;
listen 80;
listen 443 ssl;
server_name *.example.org example.org;
return 301 https://example.org$request_uri;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/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 {
error_log /opt/domain/web/logs/error.log;
access_log /opt/domain/web/logs/access.log;
root /opt/domain/web/html;
autoindex on;
autoindex_exact_size off;
listen 443 ssl;
server_name example.org;
auth_basic “Administrator Area”;
auth_basic_user_file .htpasswd;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/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
}
答案1
尝试将第一个server
块拆分成两部分:
server {
error_log /opt/domain/web/logs/error.log;
access_log /opt/domain/web/logs/access.log;
listen 80;
server_name *.example.org example.org;
return 301 https://example.org$request_uri;
}
server {
error_log /opt/domain/web/logs/error.log;
access_log /opt/domain/web/logs/access.log;
listen 443 ssl;
server_name *.example.org;
return 301 https://example.org$request_uri;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/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
}