如何将子域指向同一服务器上的另一个“文件夹”?

时间:2019-01-21 08:49:28

标签: nginx laravel-5.7 laravel-forge

因此,我在使用伪造的一台服务器上建立了两个域,每个域都有一个存储库。看起来像这样:

DomainA.com(179.x.x.x) DomainB.com(179.x.x.x)

在我的DNS中,我已经将它们都指向了相同的IP,并且伪造已经处理了其他所有内容,并且效果很好。

在服务器上有两个文件夹。 DomainA.com DomainB.com

所以现在我想在DomainB上创建一个子域,该子域“加载” DomainA的代码,例如:code.domainb.com将加载并显示domaina.com上的内容。

我不确定该怎么做?到目前为止,我了解到我需要在Nginx配置中进行一些更改,希望有人可以给我一些提示:)

domainb的Nginx配置:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/domainb.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .domainb.com;
    root /home/forge/domainb.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/domainb.com/471043/server.crt;
    ssl_certificate_key /etc/nginx/ssl/domainb.com/471043/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers XXX;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/domainb.com/server/*;

    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; }

    access_log off;
    error_log  /var/log/nginx/domainb.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/domainb.com/after/*;

域A的Nginx conf:

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/domaina.com/before/*;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .domaina.com;
    root /home/forge/domaina.com/public;

    # FORGE SSL (DO NOT REMOVE!)
    ssl_certificate /etc/nginx/ssl/domaina.com/470443/server.crt;
    ssl_certificate_key /etc/nginx/ssl/domaina.com/470443/server.key;

    ssl_protocols TLSv1.2;
    ssl_ciphers XXX;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    # FORGE CONFIG (DO NOT REMOVE!)
    include forge-conf/domaina.com/server/*;

    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; }

    access_log off;
    error_log  /var/log/nginx/domaina.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/domaina.com/after/*;

1 个答案:

答案 0 :(得分:1)

server_name指令可以具有多个值。完全匹配的主机名称优先。有关详细信息,请参见this document

如果code.domainb.com使用与.domaina.com的服务器块相同的文档根目录,只需将其名称添加到server_name指令中。

例如:

server {
    ...
    server_name .domainb.com;
    root /home/forge/domainb.com/public;
    ...
}
server {
    ...
    server_name .domaina.com code.domainb.com;
    root /home/forge/domaina.com/public;
    ...
}

有关更多信息,请参见this document