IT TIP

nginx 하위 도메인 구성

itqueen 2020. 10. 19. 13:46
반응형

nginx 하위 도메인 구성


nginx가 아파치의 역방향 프록시 역할을합니다. 이제 다른 디렉토리의 파일을 제공 할 새 하위 도메인을 추가해야하지만 동시에 기본 호스트에 대해 가지고있는 모든 위치 및 proxy_pass 지시문이 하위 도메인에도 적용되기를 원합니다.

기본 호스트에서 새 하위 도메인으로 규칙을 복사하면 작동하지만 하위 도메인이 규칙을 상속 할 수있는 방법이 있습니까? 다음은 샘플 구성입니다.

server {
    listen       80;
    server_name  www.somesite.com;
    access_log  logs/access.log;
    error_log  logs/error.log error;


   location /mvc {
      proxy_pass  http://localhost:8080/mvc;
   }


   location /assets {
      alias   /var/www/html/assets;
      expires     max;
   }

   ... a lot more locations
}

server {
    listen       80;
    server_name  subdomain.somesite.com;

    location / {
                root   /var/www/some_dir;
                index  index.html index.htm;
        }
}

감사


공통 부분을 다른 구성 파일과 include두 서버 컨텍스트 모두에서 이동할 수 있습니다. 이것은 작동합니다.

server {
  listen 80;
  server_name server1.example;
  ...
  include /etc/nginx/include.d/your-common-stuff.conf;
}

server {
  listen 80;
  server_name another-one.example;
  ...
  include /etc/nginx/include.d/your-common-stuff.conf;
}

편집 : 다음은 실행중인 서버에서 실제로 복사 한 예입니다. /etc/nginx/sites-enabled(Ubuntu / Debian의 nginx에 대한 일반적인 항목) 에서 기본 서버 설정을 구성합니다 . 예를 들어, 내 주 서버 bunkus.org의 구성 파일은 /etc/nginx/sites-enabled다음과 같습니다.

server {
  listen   80 default_server;
  listen   [2a01:4f8:120:3105::101:1]:80 default_server;

  include /etc/nginx/include.d/all-common;
  include /etc/nginx/include.d/bunkus.org-common;
  include /etc/nginx/include.d/bunkus.org-80;
}

server {
  listen   443 default_server;
  listen   [2a01:4f8:120:3105::101:1]:443 default_server;

  include /etc/nginx/include.d/all-common;
  include /etc/nginx/include.d/ssl-common;
  include /etc/nginx/include.d/bunkus.org-common;
  include /etc/nginx/include.d/bunkus.org-443;
}

예를 들어 다음 /etc/nginx/include.d/all-common은 두 server컨텍스트 에서 모두 포함 된 파일입니다 .

index index.html index.htm index.php .dirindex.php;
try_files $uri $uri/ =404;

location ~ /\.ht {
  deny all;
}

location = /favicon.ico {
  log_not_found off;
  access_log off;
}

location ~ /(README|ChangeLog)$ {
  types { }
  default_type text/plain;
}

또 다른 유형의 솔루션은 ansible에서 Jinja2 템플릿을 통해 nginx conf 파일자동 생성하는입니다. 이것의 장점은 클라우드 환경에 쉽게 배포하고 여러 개발 컴퓨터에 쉽게 복제 할 수 있다는 것입니다.

참고 URL : https://stackoverflow.com/questions/9905378/nginx-subdomain-configuration

반응형