Nginx在代理之前重写

时间:2016-03-18 22:37:46

标签: regex nginx url-rewriting proxy rewrite

我想重写一个URL,将其发送到另一个vhost。

这是我的第一位主持人:

upstream splunk {
    server 127.0.0.1:8000;
}

upstream test{
     server 127.0.0.1:88;
}
server {
    listen 88;
    root /var/www/errors/;
    index index.html;
    location ~* ^.+\.(jpeg|gif|png|jpg)
    {
        root /var/www/images/;
    }
}

在/ var / www / errors /中: 495.html 496.html 404.html

这是我的代理人:

server {
    listen 443 ssl spdy default_server;
    error_page 404 @404;
    error_page 495 @495;
    error_page 495 @496;
    location @404
        {
        rewrite ^ /404.html break;
            proxy_pass http://test;
        }
    location @495
        {
        rewrite ^ /495.html break;
            proxy_pass http://test;
        }
    location @496
        {
        rewrite ^ /496.html break;
            proxy_pass http://test;
        }
  location /
    {
        if ($ssl_client_verify = NONE)
        {
            return 496;
        }
        if ($ssl_client_verify != SUCCESS) {
            return 495;
        }
    proxy_pass http://splunk;
    proxy_set_header  X-Remote-User         $username;
    proxy_set_header  X_Remote_User         $username;
    proxy_set_header  X-SSL-Client-Serial   $ssl_client_serial;
    proxy_set_header  X-SSL-Client-Verify   $ssl_client_verify;
    proxy_set_header  X-SSL-Client-S-DN     $ssl_client_s_dn;
    proxy_set_header  X-SSL-Client-S-DN-CN  $username;
    }
}

第一台服务器运行良好,我可以毫无问题地访问index.html,404.html等。 第二个应该重写URI以发送到第一台服务器上的相应页面。使用我当前的配置,我不断收到400 Bad Request。

提前多多感谢

编辑:使用break关键字,页面会被正确重定向。遗憾的是,目标页面中的图像(实例为404.html)未加载,服务器发送400 Bad Request。这很奇怪,因为当我直接连接到这个vhost时我能够正确地看到页面(所以HTML是正确的)。

EDIT2:从第二个vhost访问日志:

172.20.175.133 - - [19/Mar/2016:16:09:31 -0700] "GET / HTTP/1.1" 400 728 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
172.20.175.133 - - [19/Mar/2016:16:09:32 -0700] "GET /logo.jpg HTTP/1.1" 400 728 "https://secondvhost.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
172.20.175.133 - - [19/Mar/2016:16:09:32 -0700] "GET /images/logo.jpg HTTP/1.1" 400 728 "https://secondvhost.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
172.20.175.133 - - [19/Mar/2016:16:09:32 -0700] "GET /var/www/images/logo.jpg HTTP/1.1" 400 728 "https://secondvhost.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
172.20.175.133 - - [19/Mar/2016:16:09:32 -0700] "GET /favicon.ico HTTP/1.1" 400 728 "https://secondvhost.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

第一个vhost(登陆版)没有400个。

2 个答案:

答案 0 :(得分:0)

您需要使用break来处理同一位置的rewrite

location @404 {
    rewrite ^ /404.html break;
    proxy_pass http://err;
}

有关详细信息,请参阅this document

答案 1 :(得分:0)

尝试更改

location ~* ^.+\.(jpeg|gif|png|jpg) { root /var/www/images/; }

location ~* \.(?:jpeg|gif|png|jpg)$ { root /var/www/images/; }

正则表达式应该在该位置的末尾匹配($,而不是开头)