NGINX-将子域指向另一个目录,该目录不是域目录的子目录

拉胡尔

我将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
}
拉胡尔

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

NGINX配置

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

CNAME记录添加到DNS条目

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

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

扩展SSL证书

我使用certbotSSL证书,并且必须重新生成它以将域包括在证书中。

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

现在一切都很好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Nginx将一个子目录的所有内容重定向到另一个子目录

NGINX:子域到子目录,无需重定向

如何将Nginx中的一个域转发到不同的子目录(php,rails)?

Nginx及其子目录

将子域指向子目录

Nginx:一个域中子目录上有多个PHP fpm

Nginx - 域上的 WordPress 和子目录中的 Node.js 应用程序

将一个域重定向到另一个域子目录

使用PHP的Nginx子目录根

Nginx-子目录的单独WebRoot

配置Nginx语言子目录

使用NGINX将Gatsby托管在子目录上

将主域重定向到子域,但保留一个子目录

将旧的Wordpress永久链接站点从一个域上的子目录重定向到另一个域的根目录

所有请求都到同一个目录,而不管域是什么-NGINX

.htaccess使用代理从子目录重定向到另一个域

如何仅将域重定向到另一个域而不是 nginx 中的子域和文件夹

将另一个域重定向到现有的drupal安装的子目录

nginx获取URL的子域并确定文档根目录

将一个域重定向到另一个域,同时从uri中删除子目录

如何检查一个目录是否是另一个目录的子目录

将子目录中的文件移动到另一个目录

nginx通过uWSGI在子目录中服务Django

nginx-子目录的php返回404

带有 PHP 的子目录的 Nginx 配置返回 404

子目录中的 Laravel 使用 NGINX 并保留路由

设置nginx以提供子目录中的文件

使用 nginx 在 rails 子目录上运行 Wordpress

子目录路径上停靠的linuxserver / dokuwiki的Nginx规则