Nginx - 他の(ローカル)ウェブサイトにリンクされたサブドメインの作成

Nginx - 他の(ローカル)ウェブサイトにリンクされたサブドメインの作成

私は純粋なNginxを使用してUbuntu 16.04ベースのDigitalOcean VPSを持っています(私はDigitalOceanのDNS管理ツールを介してDNSを管理しています)。私のNginx環境には2つのサイトがあります/var/www/html/。 1つはexample.com(HTTPS)、もう1つはtestディレクトリ名(HTTP)を持つドメインフリーサイトです。

サブドメインの作成方法example.comこれによりテスト? (test.example.com)?


1)私のものnginx.conf

2)私のexample.comサイト構成:

server {
    root /var/www/html/example.com;
    server_name example.com www.example.com;

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|woff|pdf)$ {
        expires 365d;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80; # managed by Certbot

    listen 443 ssl http2; # 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

    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot
}

3)私のtestサイト構成:

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

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80;
}

修正する:

  • VPSホスティングプロバイダのDNS管理ツールは何も変更しませんでした。
  • 私は世界中の誰もがサブtestドメインを介してサイトにアクセスできることを願っています。

ベストアンサー1

Nginx および DNS 環境で別のドキュメント ルートを使用して新しいサブドメインを正しく作成するには、次の 2 つの作業を行う必要があります。

  1. これを処理するための追加server { }ブロックです(項目3に既にあるブロックに追加)。

  2. 他のサブドメインのDNSレコードが正しいWebサーバーを指すようにします。

提供された構成に応じて、次の2つの作業を実行する必要があります。

  1. サイト構成にディレクティブtest.example.comがありません。server_name test.example.com;1 つを追加し、nginxプロセスを再起動します。

  2. test.example.comホームドメインのDNSにDNSレコードを設定します(おそらくクラウドベースのDNS管理ツールを介して)。

どのサイトにどのサーバーブロックを使用するかを常にNGINXに教えてください。 Ubuntuのパッケージマネージャとして、nginx私は人々が遭遇するほとんどのユーザーレベルのトラップに精通しています。


あなたは私たちにこれを与えました:

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

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80;
}

だから文字通り次のroot行を前に追加してください。

server_name test.example.com;

...そして次の設定ファイルが得られます。

server {
    root /var/www/html/test;
    server_name test.example.com;

    location ~ /\.ht {
        deny all;
    }

    location / {
        index index.php index.html index.htm fastcgi_index;
        try_files $uri $uri =404 $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    listen 80;
}

おすすめ記事