【问题标题】:routing subdomain to subdirectory on nginx将子域路由到 nginx 上的子目录
【发布时间】:2016-01-24 22:14:30
【问题描述】:

请尽可能明确地提供帮助。我已经在 Ubuntu 15 上运行 nginx 的家庭服务器上设置了一个域,并且我有 dns 指向它。我可以使用该域来访问该站点,如果我将 /subdirectory 附加到它,我就可以在子目录中启动页面。我想要做的是让子域直接进入正确的根目录。即:mysite.com = /index.htm, subdomain.mysite.com = ./subdirectory 文件所在的位置。 我已经尝试了每一个建议,包括那些流行的和那些受到批评的,要么我得到一个错误重新启动 Nginx,要么我得到一个“找不到服务器”错误。我试过用我的 DNS 服务器设置 cname 别名,但这也不起作用。

工作配置文件如下:

    ##

server {


      server_name "~^(?<sub>.+)\.domain\.tld$";

        index index.php index.html index.htm;
    root //media/user/ednet/$sub;
    ssl_certificate REMOVED FOR SECURITY
    ssl_certificate_key REMOVED FOR SECURITYY
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!Anull:!md5;

    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;



#   root //media/user/ednet;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

#=========================Locations=============================

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
#       root /usr/share/nginx/html;
    }

#============================================================PHP=========================================================
    location ~ \.php$ {
        try_files $uri =404;
#       fastcgi_split_path_info ^(.+\.php) (/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
 }



}

===================== 我已经尝试过代理、重定向以及我能想到的所有其他方法——包括阅读 Nginx 手册和 wiki 中的说明。 谢谢

【问题讨论】:

    标签: nginx subdomain subdirectory multisite


    【解决方案1】:

    浏览器快速显示“未找到服务器”表明未设置 DNS。您的子域需要使用以下任一方式解析到您服务器的公共 IP 地址:

    • 指向主域特定 A 记录的特定 CNAME 记录
    • 指向服务器的IP地址wild(catch-all)A记录
    • 指向服务器的 IP 地址

    对 DNS 的更改可能需要数小时才能传播。

    重新启动nginx 时出错表明配置文件中存在语法错误。

    您的配置文件不包含明显的语法错误,但有一些奇怪的元素:

    • root 指令出现两次
    • index 指令出现两次
    • root 指令有两个前导 //,通常一个会这样做

    您已经配置了 IPv4 和 IPv6,如果您有 IPv6 连接就可以了。

    您将此服务器标记为default_server,这意味着即使server_name 正则表达式不匹配,也会使用它。因此,如果您向服务器提供从根目录返回文件的子域,这意味着正则表达式无法匹配,但默认使用 server 块。在这种情况下,请检查正则表达式。

    总之,首要任务是修复 DNS。除非子域解析为服务器的 IP 地址,否则什么都不会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2017-10-09
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 2019-04-29
      相关资源
      最近更新 更多