大网数据广告活动
服务器运维问答

Nginx Vhost conflicting each other

· 52CE 我爱测

I wanted to use nginx as reverse proxy for the following sub domains api.example.com and monitor.example.com but the config settings of api.example.com is always overriding monitor.example.com . The api vhost has rate limiter and…

问题原文

原帖链接 · mboy · CC BY-SA 3.0

I wanted to use nginx as reverse proxy for the following sub domains api.example.com and monitor.example.com but the config settings of api.example.com is always overriding monitor.example.com . The api vhost has rate limiter and I don't want to apply that setting on monitor vhost.. access_log and access_log path are also not followed on the second vhost settings..

I can access the content of the backend servers properly but I don't understand why the config settings of api.example.com is also applied to monitor.example.com vhost.

api.example.com

limit_req_zone $binary_remote_addr zone=api:10m rate=90r/m;

log_format custom '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$request_body"';

upstream backend  {
   server web-06:80 max_fails=5  fail_timeout=20s;
   server web-01:80 max_fails=5  fail_timeout=20s;
}


server {
  listen 80;
  #logging
  #access_log off;
  #error_log off;
  access_log /var/log/nginx/access.log custom;

  location / {
    #rate limiting
    limit_req zone=api burst=1;

    proxy_pass  http://backend;

    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_max_temp_file_size 0;

    client_max_body_size 10m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;

    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
  }
}

monitor.example.com

upstream backend2  {
   server web-06:80 max_fails=5  fail_timeout=20s;
   server web-01:80 max_fails=5  fail_timeout=20s;
}


server {
  listen 80;
  #logging
  #access_log off;
  #error_log off;
  error_log  /var/log/nginx/monitor-error.log;
  access_log /var/log/nginx/monitor.log;

  location / {
    proxy_pass  http://backend2;
  }
}

已采纳解答

原帖链接 · Thomas Ward · CC BY-SA 3.0

Your two server blocks will always conflict such that the first observed server block in the configuration file will be taking precedent as the default server block for all requests, unless you specify a proper server_name for each config to apply to incoming requests for that hostname / subdomain. (This applies also for all requests of which a requested $HOST does not match any of the configurations provided, and there is not a default_server specified in the configurations). This is how NGINX handles requests by default when there are missing configuration on server blocks, or with unmatched $HOST requests.

NGINX will check what hostname is requested. If $HOST matches a server block with a matching server_name it will use that config for determining what to serve for the request. Without such configuration to define what server block handles what, the first config block loaded in the configuration will be used by default when there is no matching server_name (and you didn't specify a default_server). (server_name api.example.com; will specify the server block will match for api.example.com, while server_name monitor.example.com; specifies the server block that matches for monitor.example.com)

Proper reverse proxying of multiple backends by multiple host names through one NGINX instance can only be achieved this way, where each proxy block for handling different hostname requests to different backends has individually defined server_name directives for each server block.

Your backends will also have different configurations - if each backend only serves for that one host then you don't need to specify the server_name there - you do need to specify the server_name on the reverse proxy however to make sure it routes requests through the proper config blocks and then the proper backend. (However, similar configuration may be needed on the backends if they can serve both sites)

I have set up many systems in which one NGINX instance reverse proxies to multiple different backends and systems on a network, and they have always needed server_name to be defined to properly work. This is also the same rationale for getting one NGINX instance to serve many different websites from the same instance and configuration files. I also maintain the nginx package in Ubuntu, and this is a common issue I see brought up by people there when they set up their NGINX to do what you are trying to do.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。