简体   繁体   English

Nginx在代理之前重写

[英]Nginx Rewrite before Proxy

I would like to rewrite an URL to send it to a different vhost. 我想重写一个URL,以将其发送到其他虚拟主机。

Here is my first Host: 这是我的第一位主持人:

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/;
    }
}

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

Here is my proxy: 这是我的代理:

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;
    }
}

The first server is working well and I can access without any problem to index.html, 404.html and so on. 第一个服务器运行良好,我可以毫无问题地访问index.html,404.html等。 The second one should rewrite the URI to send to the appropriate page on the first server. 第二个应该重写URI以发送到第一个服务器上的相应页面。 With my current configuration, I keep getting 400 Bad Request. 使用当前配置,我不断收到400错误请求。

Thanks a lot in advance 提前谢谢

EDIT: With the break keyword, the pages are redirected properly. 编辑:使用break关键字,页面将正确重定向。 Unfortunately, the images in the target pages (404.html for instances) are not loaded and the server sends a 400 Bad Request. 不幸的是,目标页面(实例为404.html)中的图像未加载,服务器发送了400 Bad Request。 This is strange since I am able to see the page properly when I directly connect to this vhost (so the HTML is correct). 这很奇怪,因为当我直接连接到该虚拟主机时,我能够正确看到页面(因此HTML是正确的)。

EDIT2: Access logs from second vhost: EDIT2:从第二个虚拟主机访问日志:

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"

There is no 400 from the first vhost (the landing one). 第一个虚拟主机(登陆的虚拟主机)没有400。

You need to use break to process the rewrite in the same location. 您需要使用break才能在同一位置处理rewrite

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

See this document for details. 有关详细信息,请参见此文档

Try changing 尝试改变

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

to

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

the regex should match on the end of the location (the $ , not the beginning) 正则表达式应该在位置的末尾匹配( $ ,而不是开头)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM