【问题标题】:How do I fix this nginx configuration with almost-duplicate proxied locations?如何使用几乎重复的代理位置修复此 nginx 配置?
【发布时间】:2015-05-07 12:53:34
【问题描述】:

我的一台虚拟服务器具有以下 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 有两个几乎相同的 Location 块。这是我可以使此配置工作的唯一方法,其中两个特定文件夹(root 和 /static)从 S3 提供,其他所有内容都转到应用程序服务器。

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

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

【问题讨论】:

  • 将重复部分放入外部文件并include它。
  • 有趣。这会起作用并使它看起来更好,但它仍然需要几个 Location 块。我更喜欢只用两个 Location 块(一个用于 S3,一个用于应用服务器)的东西。
  • 那是个坏主意。几个简单的位置比 e.g. 更容易理解(也更有效)。奇怪的正则表达式位置。
  • 很公平。如果您想将此作为问题的答案发布,我会在几天后接受,除非出现更好的情况。

标签: nginx amazon-s3 proxy


【解决方案1】:

您可以将重复部分放入外部文件并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
}

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多