首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自动虚拟主机-单个Nginx配置

自动虚拟主机-单个Nginx配置
EN

Server Fault用户
提问于 2021-06-27 20:24:53
回答 1查看 218关注 0票数 0

我正试图根据一个目录为多个主机创建一个单一的Nginx配置。我遵循了一个指南,它似乎很适合标准HTTP设置,但是当我添加HTTPS 301重定向时,可能会出现“无效重定向”错误。对此有什么想法吗?下面是我的配置。发送

代码语言:javascript
运行
AI代码解释
复制
server {
  listen x.x.x.x:80;

  server_name ~^(?.+?).domain.com$;

  return 301 https://$server_name$request_uri;

}

server {
    listen x.x.x.x:443 ssl default_server;
    server_name ~^(?.+?).domain.com$;


root /var/web/$sname;

index index.html index.htm index.php;


charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }


ssl_certificate /etc/letsencrypt/live/wildcard.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/wildcard.domain.com/privkey.pem;

access_log /var/log/nginx/$sname-access.log;
error_log  /var/log/nginx/wildcard-error.log debug;

error_page 404 /index.php;

    sendfile off;

        location ~ \.php {
                include fastcgi.conf;
                #fastcgi_index index.php;
                include cors_support;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
        }
        location ~ /\.ht {
                deny all;
        }
  location /.well-known {
    root /var/www/html;
  }
}
EN

回答 1

Server Fault用户

发布于 2021-06-28 03:19:48

您的重定向是“无效的”,因为您试图重定向到https://~^(?.+?).domain.com$,这显然是无效的。

为何会这样呢?

您选择按以下方式编写重定向:

代码语言:javascript
运行
AI代码解释
复制
  return 301 https://$server_name$request_uri;

这没道理。server_name不是有效的主机名。

相反,您应该重定向到用户代理使用的同一台主机。在那里使用的正确变量$host,而不是$server_name

代码语言:javascript
运行
AI代码解释
复制
  return 301 https://$host$request_uri;
票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1068027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档