我终于开始设置 Nginx 反向代理来处理我在家里托管的多个网站,因为我只有一个外部 IP 地址,而且一切正常,但我想知道是否有办法简化我其中一个网站的当前配置,这个配置比我认为需要的要长。任何意见都非常感谢。
server {
listen 192.168.1.176:80;
server_name ighfdexplorers.com;
return 301 https://www.$server_name$request_uri;
}
server {
listen 192.168.1.176:443 ssl http2;
server_name ighfdexplorers.com;
return 301 https://www.$server_name$request_uri;
ssl_certificate /etc/nginx/ssl/ighfdexplorers/base/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/ighfdexplorers/base/privkey.pem;
}
server {
listen 192.168.1.176:80;
server_name www.ighfdexplorers.com;
return 301 https://$server_name$request_uri;
}
server {
listen 192.168.1.176:443 ssl http2;
server_name www.ighfdexplorers.com;
set $upstream 192.168.1.179;
ssl_certificate /etc/nginx/ssl/ighfdexplorers/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/ighfdexplorers/privkey.pem;
location / {
proxy_pass_header Authorization;
proxy_pass https://$upstream;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
client_max_body_size 0;
proxy_read_timeout 36000s;
proxy_redirect off;
proxy_ssl_session_reuse off;
}
}
答案1
您可以对所有没有服务器部分的域执行通用的 catchall 重定向,如下所示:
server
{
listen 80 default_server;
listen [::]:80 default_server;
server_name .ighfdexplorers.com;
location /
{
return 301 https://$host$request_uri;
}
}
server
{
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name ighfdexplorers.com;
if ($host != $server_name)
{
return 301 https://$server_name$request_uri;
}
... ssl directives, etc...
}
使用server_name
with 过程.
告诉 nginx 匹配任何子域前缀,或者不匹配。由于它是默认服务器,因此这也会匹配其他所有内容,您可能不希望这样。
第二台服务器再次是默认服务器,但用于 HTTPS,因此它将用于所有不匹配的内容。如果我们将请求$host
与配置进行比较并且它们不匹配,我们会重定向到正确的名称。除非有该特定名称的服务器,否则$server_name
这种方式*.ighfdexplorers.com
将始终重定向到。ighfdexplorers.com