简体   繁体   English

Nginx 子域配置

[英]Nginx subdomain configuration

I have nginx acting as a reverse proxy to apache.我有 nginx 作为 apache 的反向代理。 I now need to add a new subdomain that will serve files from another directory, but at the same time I want all location and proxy_pass directives that I have for the default host to apply to the subdomain also.我现在需要添加一个新的子域,它将为另一个目录中的文件提供服务,但同时我希望我拥有的默认主机的所有 location 和 proxy_pass 指令也应用于子域。

I know that if I copy the rules from the default host to the new subdomain it will work, but is there a way for the subdomain to inherit the rules?我知道如果我将规则从默认主机复制到新的子域,它会起作用,但是子域有没有办法继承规则? Below is a sample configuration下面是一个示例配置

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;
        }
}

Thanks谢谢

You could move the common parts to another configuration file and include from both server contexts.您可以将公共部分移动到另一个配置文件并从两个服务器上下文中include This should work:这应该有效:

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;
}

Edit: Here's an example that's actually copied from my running server.编辑:这是一个实际从我正在运行的服务器复制的示例。 I configure my basic server settings in /etc/nginx/sites-enabled (normal stuff for nginx on Ubuntu/Debian).我在/etc/nginx/sites-enabled (Ubuntu/Debian 上 nginx 的正常内容)中配置了我的基本服务器设置。 For example, my main server bunkus.org 's configuration file is /etc/nginx/sites-enabled and it looks like this:例如,我的主服务器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;
}

As an example here's the /etc/nginx/include.d/all-common file that's included from both server contexts:例如,这里是包含在两个server上下文中的/etc/nginx/include.d/all-common文件:

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;
}

Another type of solution would be to autogenerate the nginx conf files via Jinja2 templates from ansible .另一种解决方案是通过 ansible 的 Jinja2 模板自动生成 nginx conf 文件 The advantage of this is easy deployment to a cloud environment, and easy to replicate on multiple dev machines这样做的好处是易于部署到云环境,并且易于在多台开发机器上复制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM