
次のような設定になっています:
- 2つの仮想ホストを持つNginx
- 2 つのサブドメイン: sub1.domain.com と sub2.domain.com
sub1.domain.com
正常に動作しています。 を入力すると、sub2.domain.com
ブラウザは 403 を返します。 両方のドメインのディレクトリをwww-data
( chown -R www-data:www-data /var/www/
) が所有するように設定しました。
エラーログを見ると、は/var/log/nginx/sub2.domain.com/error.log
空であるのに/var/log/nginx/sub1.domain.com/error.log
、
2018/03/09 11:29:00 [error] 19024#0: *13009 directory index of "/var/www/sub1.domain.com" is forbidden, client: ip, server: sub1.domain.com, request: "GET / HTTP/1.1", host: "my-host"
ブラウザでsub1.domain.com
入力したのにサーバーがそれを含めるのはなぜか知っている人はいませんか?sub2.domain.com
問題は、sub2.domain.com
常に の設定sub1.domain.com
( default_server
) が実行されてしまうことにあるのではないかと思います。nginx のドキュメントによると、次のようになります。
値がどのサーバー名とも一致しない場合、またはリクエストにこのヘッダー フィールドがまったく含まれていない場合、nginx はリクエストをこのポートのデフォルト サーバーにルーティングします。
sub1.domain.com の Nginx 設定
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sub1.domain.com;
index index.php index.html index.htm;
server_name sub1.domain.com;
access_log /var/log/nginx/sub1.domain.com/access.log;
error_log /var/log/nginx/sub1.domain.com/error.log;
location / {
try_files $uri $uri/ =404;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param AUTH_SECRET "my-secret";
include fastcgi_params;
}
}
sub2.domain.com の Nginx 設定
upstream backend{
server 127.0.0.1:3000;
}
server {
listen 80;
listen [::]:80;
access_log /var/log/nginx/sub2.domain.com/access.log;
error_log /var/log/nginx/sub2.domain.com/error.log;
root /var/www/sub2.domain.com;
server_name sub2.domain.com;
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
# route /api requests to backend
location /api/v1 {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
}
location / {
try_files $uri $uri/ /index.html =404;
}
}
私はここのチュートリアルに従いましたhttps://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-14-04-ltssym リンクを設定します。つまり、/etc/nginx/sites-available
構成が含まれ、/etc/nginx/sites-enabled
それらの構成に sym リンクが含まれます。
また、これはsub1.domain.com
PHP バックエンドを提供しますが、sub2.domain.com
一部の静的コンテンツ (シングル ページ アプリケーション) を提供し、API リクエストを Node.js バックエンドに送信する必要があります。
編集:
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
私のnginx.conf
ファイルには が含まれていますinclude /etc/nginx/sites-enabled/*;
。したがって、上記で投稿した構成を含める必要があります。構成の 1 つにエラーがある場合は、少なくともnginx -t
エラーがスローされます。
編集 2: tl;dr (コメントの要約): DNS の設定が間違っていました。A
両方のサブドメインに正しいレコードを設定すると、問題は解決しました。nginx の設定は期待どおりに動作しています。