微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx proxypass重写基本URL

我正在尝试设置Nginx以使用proxy_pass将请求转发到多个后端服务.

其中一些不支持在子文件夹下访问,因此我必须添加重写以剥离附加的子文件夹,以允许从同一端口访问它们.

有关改进重写的任何提示吗?

卷曲输出;

:~$curl -I -k https://example.net/internal
HTTP/1.1 404 Not Found
Server: Nginx/1.0.5
Date: Thu,19 Jan 2012 22:30:46 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Content-Length: 145

:~$curl -I -k https://example.net/internal/
HTTP/1.1 200 OK
Server: Nginx/1.0.5
Date: Thu,19 Jan 2012 22:31:12 GMT
Content-Type: text/html
Connection: keep-alive
Content-Length: 1285
Accept-Ranges: bytes
Last-Modified: Wed,18 Jan 2012 01:35:21 GMT

配置文件;

proxy.conf

location /internal {
    rewrite           ^/internal/(.*) /$1 break;
    proxy_pass        http://localhost:8081/internal;
    include proxy.inc;
}
.... more entries ....

/主启用的站点

server {
    listen 443;

    server_name example.com;
    server_name_in_redirect off;

    include proxy.conf;

    ssl on;
}

proxy.inc

proxy_connect_timeout   59s;
proxy_send_timeout      600;
proxy_read_timeout      600;
proxy_buffer_size       64k;
proxy_buffers           16 32k;
proxy_pass_header       Set-Cookie;
proxy_redirect          off;
proxy_hide_header       vary;

proxy_busy_buffers_size         64k;
proxy_temp_file_write_size      64k;

proxy_set_header        Accept-Encoding         '';
proxy_ignore_headers    Cache-Control           Expires;
proxy_set_header        Referer                 $http_referer;
proxy_set_header        Host                    $host;
proxy_set_header        Cookie                  $http_cookie;
proxy_set_header        X-Real-IP               $remote_addr;
proxy_set_header        X-Forwarded-Host        $host;
proxy_set_header        X-Forwarded-Server      $host;
proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
proxy_set_header        X-Forwarded-Ssl         on;
proxy_set_header        X-Forwarded-Proto       https;
最佳答案
我们可以将你的重写变得更加丑陋,以便在不允许意外匹配的情况下考虑该字符串(/ internal).(如果你倾向于,你需要((?:/.* |))你的斜线是什么或者类似的野兽)但丑陋的可维护性较差.

我倾向于说这样做:

location /internal {
    rewrite           ^/internal$https://example.net/internal/ permanent;
    rewrite           ^/internal/(.*) /$1 break;
    proxy_pass        http://localhost:8081/internal;
    include proxy.inc;
}

原文地址:https://www.jb51.cc/nginx/435496.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐