nginx子域配置

Tho*_*mas 76 subdomain nginx

我有nginx充当apache的反向代理.我现在需要添加一个新的子域来提供来自另一个目录的文件,但同时我希望我对默认主机的所有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;
        }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Mor*_*kus 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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个实际从我正在运行的服务器复制的示例.我配置我的基本服务器设置/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;
}
Run Code Online (Sandbox Code Playgroud)

作为一个例子,这里是/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;
}
Run Code Online (Sandbox Code Playgroud)

  • 工作良好.我的错.我把这个公共文件放在一个目录中,其中每个.conf文件都被自动加载并导致错误.非常感谢 (3认同)
  • 是的,我自己遇到了这个问题.这就是我选择将所有包含的文件放入`/ etc/nginx/include.d`的原因,这些文件不是由Debian/Ubuntu系统上常见的nginx配置文件提供的. (3认同)