nginx的子站点配置

问题描述 投票:75回答:2

我作为一个反向代理到Apache 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;
        }
}

谢谢

nginx subdomain
2个回答
88
投票

您可以在共用部分移动到另一个配置文件,并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;
}

0
投票

的解决方案的另一个类型将autogenerate the nginx conf files via Jinja2 templates from ansible。这样做的优点是容易部署到云环境,并且容易复制在多个开发机

© www.soinside.com 2019 - 2024. All rights reserved.