nginx - 无法弄清楚位置重写

时间:2012-10-13 05:55:24

标签: regex nginx

我试图将不同的URI请求重定向到不同的EC2容器,我多年来一直使用nginx作为apache的一个反向代理,但现在我想做一些重写nginx级别。

这是我想要完成的事情:

server {
    listen       80;
    server_name  _;

    gzip              on;
    gzip_static       on;
    gzip_buffers      16 8k;
    gzip_comp_level   9;
    gzip_http_version 1.0;
    gzip_min_length   0;
    gzip_types        text/plain text/css application/x-javascript;
    gzip_vary         on;

    location / { 
        # catch the following URI's including homepage: /contact.html, /terms.html, /
        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_read_timeout 240;
        proxy_connect_timeout 240;
        proxy_send_timeout 240;

        send_timeout 240;
        proxy_pass http://servers_static;
    }

    location / { 
        # catch everything not matched above
        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_read_timeout 240;
        proxy_connect_timeout 240;
        proxy_send_timeout 240;

        send_timeout 240;
        proxy_pass http://servers_dynamic;
    }
}

我确定这只是一个简单的正则表达式问题,但我从来没有理解过那些东西。有人可以帮助我吗?

3 个答案:

答案 0 :(得分:2)

使用通用代理设置创建文件/ etc / nginx / EC2:

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_read_timeout 240;
proxy_connect_timeout 240;
proxy_send_timeout 240;
send_timeout 240;

然后您的主配置变为:

server {
  listen       80;
  server_name  _;

  #gzip settings cut for brevity, add them back in     

  # static content
  location = / {include /etc/nginx/EC2; proxy_pass http://servers_static;}
  location = /contact.html {include /etc/nginx/EC2; proxy_pass http://servers_static;}
  location = /terms.html {include /etc/nginx/EC2; proxy_pass http://servers_static;}

  # dynamic content
  location / { include /etc/nginx/EC2; proxy_pass http://servers_dynamic; }
}

您也可以组合.html静态内容页面的位置,如下所示:

  location ~ (contact|terms).html {
    include /etc/nginx/EC2; proxy_pass http://servers_static;}

拥有完全匹配的位置可能稍微有点效率,并且只要您不需要很多,所以复制不应该使配置变得笨拙

答案 1 :(得分:0)

看看try_files。它会连续尝试你给它的路径。在此示例中,将返回/var/www/sites/foo/current/public/$uri处的任何静态文件,仅在没有静态文件时将请求路由到应用程序。

upstream app {
  server unix:/tmp/.sock_my_app;
}

server {
  # path for static files
  root /var/www/sites/foo/current/public;

  # Prefer to serve static files directly from nginx to avoid unnecessary
  # requests to the application server.
  try_files $uri/index.html $uri.html $uri @app;

  location @app {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # timeouts

    # reverse proxy to an upstream
    proxy_pass http://app;
  }
}

答案 2 :(得分:0)

经过一些搜索后,我在堆栈溢出中发现了一个类似的问题,帮助我解决了我的问题:

nginx multiple locations for one rule regex