하위 도메인이 있는 Nginx 두 개의 가상 호스트는 항상 default_server로 연결됩니다.

하위 도메인이 있는 Nginx 두 개의 가상 호스트는 항상 default_server로 연결됩니다.

나는 다음과 같은 설정을 가지고 있습니다 :

  • 두 개의 가상 호스트가 있는 Nginx
  • 두 개의 하위 도메인: sub1.domain.com 및 sub2.domain.com

sub1.domain.com잘 작동합니다. 입력하면 브라우저에 403이 반환됩니다. 두 도메인에 대해 구성된 디렉터리를 ( ) sub2.domain.com이 소유하도록 설정했습니다 .www-datachown -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구성을 실행하는 것 같다는 것입니다 . nginx 문서에 따르면:sub1.domain.comdefault_server

해당 값이 서버 이름과 일치하지 않거나 요청에 이 헤더 필드가 전혀 포함되어 있지 않으면 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-lts그리고 심볼릭 링크를 설정하세요. 따라서 /etc/nginx/sites-available구성이 포함되어 있으며 /etc/nginx/sites-enabled해당 구성에 대한 기호 링크가 포함되어 있습니다.

또한 이는 sub1.domain.comPHP 백엔드를 제공하는 반면 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/*;. 따라서 위에 게시한 구성이 포함되어야 합니다. nginx -t구성 중 하나에 오류가 있으면 최소한 오류가 발생합니다.

편집 2: tl;dr (의견 요약): DNS가 잘못 구성되었습니다. 두 하위 도메인 모두에 대해 올바른 A레코드를 설정하면 문제가 해결되었습니다. nginx 구성이 예상대로 작동합니다.

관련 정보