【问题标题】:NGINX - Pointing a subdomain to a different directory that is not a subdirectory of the domain directoryNGINX - 将子域指向不是域目录子目录的不同目录
【发布时间】:2019-04-29 20:55:14
【问题描述】:

我使用 Nginx 作为 Apache 的反向代理。我有一个域,假设 example.com 指向 /var/www/html/example 目录。

我想将一个子域phpmyadmin.example.com 指向一个目录/var/www/html/phpmyadmin,该目录显然不是该域目录的子目录。

我的完整配置文件/etc/nginx/sites-enabled/example如下所示。我将##### BEGIN ..###### END .. 之间的部分添加到我的配置文件中以尝试使其工作。

然后我通过sudo service nginx restart成功重启了NGINX,但我仍然无法访问phpmyadmin.example.com

这是我的配置:

server {
    root /var/www/html/example;

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

    server_name example.com;

        access_log /var/www/html/example/access.log;
        error_log /var/www/html/example/error.log;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/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
}

##### BEGIN PHPMYADMIN CONFIG    #####
server {
    root /var/www/html/phpmyadmin;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name phpmyadmin.example.com;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
    }

    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    #   # With php-cgi (or other tcp sockets):
    #   fastcgi_pass 127.0.0.1:9000;
    }
}
###### END OF PHPMYADMIN CONFIG #####

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80 ;
    listen [::]:80 ;

    server_name example.com www.example.com
    return 404; # managed by Certbot
}

【问题讨论】:

  • 您的配置似乎正确。但是 sites-enabled 不是配置域的地方,所以 sites-available 是。
  • 访问http://phpmyadmin.example.com时会发生什么?
  • 我收到此错误消息:嗯。我们无法找到该网站。我们无法连接到服务器 phpmyadmin.example.com

标签: php nginx phpmyadmin reverse-proxy


【解决方案1】:

我解决了我的问题,我将列出我为解决该问题所采取的步骤,以供其他人将来参考:

NGINX 配置

我最初尝试的 nginx 配置是正确的(如问题中所述)。

CNAME 记录添加到 DNS 条目中

这是主要的缺失部分。我在我的 DNS 仪表板中添加了一个 CNAME 条目,如下所示:

类型:CNAME 名称:phpmyadmin 值:example.com TTL:3600(也可以设置为自动)

扩展 SSL 证书

我使用certbot 作为 SSL 证书,我必须重新生成它以将域包含在证书中。

sudo certbot --nginx -d example.com -d www.example.com -d phpmyadmin.example.com

在此步骤中,certbot 可以处理 nginx 配置以将 HTTP 流量重定向到 HTTPS。

重新加载 NGINX 服务

重新加载新配置:sudo service nginx reload

并验证服务状态:sudo service nginx status

现在一切正常。

【讨论】:

    猜你喜欢
    • 2014-10-30
    • 2016-07-21
    • 1970-01-01
    • 2014-01-28
    • 2015-08-26
    • 2011-08-05
    • 2015-05-08
    • 2016-09-05
    • 1970-01-01
    相关资源
    最近更新 更多