如何使用几乎重复的代理位置修复此 nginx 配置?

How do I fix this nginx configuration with almost-duplicate proxied locations?

我的一个虚拟服务器有以下 nginx 配置:

upstream app_example_https {
  server 127.0.0.1:1340;
}

proxy_cache_path /Users/jaanus/dev/nginxcache levels=1:2 keys_zone=S3CACHE:10m;
proxy_cache_key "$scheme$request_method$host$request_uri";

server {
  listen 0.0.0.0:1338;
  server_name localhost;

  ssl on;
  ssl_certificate /Users/jaanus/dev/devHttpsCert.pem;
  ssl_certificate_key /Users/jaanus/dev/devHttpsKey.pem;

  location = / {

    proxy_http_version     1.1;
    proxy_set_header       Connection "";
    proxy_set_header       Host 'something.s3-website-us-east-1.amazonaws.com';
    proxy_set_header       Authorization '';
    proxy_hide_header      x-amz-id-2;
    proxy_hide_header      x-amz-request-id;
    proxy_hide_header      Set-Cookie;
    proxy_ignore_headers   Set-Cookie;

    proxy_cache            S3CACHE;
    proxy_cache_valid      any 60m;
    add_header             X-Cached $upstream_cache_status;

    proxy_pass             http://something.s3-website-us-east-1.amazonaws.com/;

  }

  location /static/ {

    proxy_http_version     1.1;
    proxy_set_header       Connection "";
    proxy_set_header       Host 'something.s3-website-us-east-1.amazonaws.com';
    proxy_set_header       Authorization '';
    proxy_hide_header      x-amz-id-2;
    proxy_hide_header      x-amz-request-id;
    proxy_hide_header      Set-Cookie;
    proxy_ignore_headers   Set-Cookie;

    proxy_cache            S3CACHE;
    proxy_cache_valid      any 60m;
    add_header             X-Cached $upstream_cache_status;

    proxy_pass             http://something.s3-website-us-east-1.amazonaws.com/static/;

  }


  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://app_example_https/;
    proxy_redirect off;
  }
}

这在英语中的作用:

有一个 nginx 前端可以处理来自静态 Amazon S3 站点或应用程序服务器的请求。

所有对 /(站点根目录)和 /static 的请求都从 Amazon S3 反向代理。所有其他请求都从应用程序服务器反向代理。

现在,问题来了:S3 有两个几乎相同的位置块。这是我使此配置工作的唯一方法,其中两个特定文件夹(root 和 /static)从 S3 提供,其他所有内容都转到应用程序服务器。

两个几乎相同的块看起来很笨而且不可扩展。当我添加这样的文件夹时,我不想一直复制这些块。

如何将两个位置合并到一个位置块中,同时保持一切以相同的方式工作?

您可以将重复部分放入外部文件并 include 它。

amazon.inc

proxy_http_version     1.1;
proxy_set_header       Connection "";
proxy_set_header       Host 'something.s3-website-us-east-1.amazonaws.com';
proxy_set_header       Authorization '';
proxy_hide_header      x-amz-id-2;
proxy_hide_header      x-amz-request-id;
proxy_hide_header      Set-Cookie;
proxy_ignore_headers   Set-Cookie;

proxy_cache            S3CACHE;
proxy_cache_valid      any 60m;
add_header             X-Cached $upstream_cache_status;

proxy_pass             http://something.s3-website-us-east-1.amazonaws.com;

你的配置

location = / {
    include amazon.inc;
}

location /static/ {
    include amazon.inc;
}

location / {
    # proxy to you app
}

如果您更喜欢将所有内容保存在一个文件中,您可以使用这个技巧:

error_page 470 = @amazon;
location = / {
    return 470;
}

location /static/ {
    return 470;
}

location @amazon {
    # proxy to amazon
}

您可以使用正则表达式将多个 location 合并在一起,但我不建议这样做,因为它难以阅读和理解,而且效率低于简单的前缀位置。但是,举个例子:

# NOT RECOMMENDED
location ~ ^/($|static/) {
    # proxy to amazon
}