【问题标题】:nginx - shorter proxy_pass configurationnginx - 更短的 proxy_pass 配置
【发布时间】:2012-03-22 06:26:44
【问题描述】:

我正在使用 nginx 设置代理。想象一下我想将一堆 server_name 添加到相同的配置中:

server {
    listen 1.2.3.4:443 ssl;
    server_name 1.abc.org;
    access_log off;
    error_log off;
    ssl_certificate /etc/nginx/ssl/test.crt;
    ssl_certificate_key /etc/nginx/ssl/test.key;
    keepalive_timeout 60;
    location / {
           proxy_pass https://1.abc.org;
           include /etc/nginx/conf.d/proxy.conf;
           proxy_set_header X-Forwarded-Proto https;
    }
}

我想添加一堆 server_name:

应该是

server {
listen 1.2.3.4:443 ssl;
server_name $server_name;
access_log off;
error_log off;
ssl_certificate /etc/nginx/ssl/test.crt;
ssl_certificate_key /etc/nginx/ssl/test.key;
keepalive_timeout 60;
location / {
    proxy_pass https://$server_name;
    include /etc/nginx/conf.d/proxy.conf;
           proxy_set_header X-Forwarded-Proto https;
    }
}

和 server_name = 1.abc.org, 2.abc.org, 3.abc.org, 4.abc.org, aish.abc.org...

然后我可以像上面那样添加 server_name 来做更简单的工作,不需要复制配置块。我该怎么做?

谢谢。

【问题讨论】:

    标签: proxy nginx


    【解决方案1】:

    看看使用上游块

    upstream backend  {
      server backend1.example.com weight=5;
      server backend2.example.com:8080;
      server unix:/tmp/backend3;
    }
    
    server {
      location / {
        proxy_pass  http://backend;
      }
    }
    

    http://wiki.nginx.org/HttpUpstreamModule

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 2017-08-25
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多