Nginx サブドメイン設定 質問する

Nginx サブドメイン設定 質問する

Apache へのリバース プロキシとして nginx を使用しています。別のディレクトリからファイルを提供する新しいサブドメインを追加する必要がありますが、同時に、デフォルト ホストのすべての location および 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;
        }
}

ありがとう

ベストアンサー1

共通部分を別の構成ファイルに移動したり、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;
}

おすすめ記事