Nginx의 와일드 카드 가상 호스트 원하는 [디렉토리] 구조입니다

방금 서버에 Nginx를 설치했으며 결과에 매우 만족하지만 여전히 와일드 카드 가상 호스트를 삽입하는 방법을 알 수 없습니다.

이것은 내가 원하는 [디렉토리] 구조입니다 :

-- public_html (example.com)
---subdoamin 1 (x.example.com)
---subdomain 2 (y.example.com)

보시다시피, 매우 기본적이지만, 새 하위 도메인에 대한 A 레코드를 추가하여 도메인을 추가하는 기능을 원합니다. public_html에서 동일한 이름의 하위 디렉토리를 즉시 가리 킵니다.

웹에는 물건이 있지만 정확히 이와 같은 것을 보지 못했습니다.

도움을 주시면 감사하겠습니다.



답변

보여 줄게

구성 파일

server {
  server_name example.com www.example.com;
  root www/pub;
}

server {
  server_name ~^(.*)\.example\.com$ ;
  root www/pub/$1;
}

테스트 파일

두 개의 테스트 파일이 있습니다 :

$ cat www/pub/index.html
COMMON

$ cat www/pub/t/index.html
T

테스팅

정적 서버 이름 :

$ curl -i -H 'Host: example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:42 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Wed, 23 Mar 2011 07:56:24 GMT
Connection: keep-alive
Accept-Ranges: bytes

COMMON

$ curl -i -H 'Host: www.example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:48 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Wed, 23 Mar 2011 07:56:24 GMT
Connection: keep-alive
Accept-Ranges: bytes

COMMON

그리고 정규식 서버 이름 :

$ curl -i -H 'Host: t.example.com' http://localhost/
HTTP/1.1 200 OK
Server: nginx/0.8.54
Date: Wed, 23 Mar 2011 08:00:54 GMT
Content-Type: text/html
Content-Length: 2
Last-Modified: Wed, 23 Mar 2011 07:56:40 GMT
Connection: keep-alive
Accept-Ranges: bytes

T

답변

아래의이 Nginx 구성 파일을 사용 /var/www/vhost/하면 해당 로그 파일을 동적으로 생성하면서 해당 폴더로 동적으로 라우팅하는 와일드 카드 호스트 이름을 사용할 수 있습니다.

http://test1.wildcard.com/var/www/vhost/test1
                                                   /var/log/nginx/test1.wildcard.com-access.log
                                                   /var/log/nginx/test1.wildcard.com-error.log

http://test2.wildcard.com/var/www/vhost/test2
                                                   /var/log/nginx/test2.wildcard.com-access.log
                                                   /var/log/nginx/test2.wildcard.com-error.log

wildcard.conf

server {
  listen 80;
  listen [::]:80;

  #  Match everything except dot and store in $subdomain variable
  #  Matches test1.wildcard.com, test1-demo.wildcard.com
  #  Ignores sub2.test1.wildcard.com
  server_name ~^(?<subdomain>[^.]+).wildcard.com;

  root /var/www/vhost/$subdomain;

  access_log /var/log/nginx/$host-access.log;
  error_log  /var/log/nginx/$host-error.log;
}

답변

이것이 내가 Nginx로 가상 호스트를 처리 한 방법입니다 .

server_name ~^(?<vhost>.*)$;
root /srv/www/$vhost;
access_log /var/log/nginx/$vhost.access.log;

부모 폴더의 와일드 카드 하위 도메인이 왜 그렇게 잘못 / 오해의 소지가 있는지 잘 모르겠습니다 .