私は純粋な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 つの作業を行う必要があります。
これを処理するための追加
server { }
ブロックです(項目3に既にあるブロックに追加)。他のサブドメインのDNSレコードが正しいWebサーバーを指すようにします。
提供された構成に応じて、次の2つの作業を実行する必要があります。
サイト構成にディレクティブ
test.example.com
がありません。server_name test.example.com;
1 つを追加し、nginx
プロセスを再起動します。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;
}