0

我有一个运行 NGINX 的 CentOS 服务器,监听 80 和一个在 8080 上为应用程序服务的数据库。我希望能够键入

http://example.com/dev/abc

并让它实际访问

http://example.com:8080/apex/abchttp://localhost:8080/apex/abc

我已经使用了这个位置配置

location /dev {
       proxy_pass http://example.com:8080/apex;
    }

但是,当我尝试时,显示的 url 是

http://example.com/apex/apex

该页面未找到,日志显示:

2018/06/14 12:51:33 [error] 7209#0: *2067 open()
"/usr/share/nginx/html/apex/apex" failed (2: No such file or directory), 
client: 124.157.113.187, server: _, request: "GET /apex/apex HTTP/1.1", host: "example.com"

看起来有两件奇怪的事情正在发生

1) 尽管使用了 proxy_pass,但仍在使用端口 80 而不是 8080

2)为什么apex是两次“/apex/apex/”

请帮忙 :)

从配置文件中添加整个服务器块:

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  example.com;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

   location /dev {
       proxy_pass http://example.com:8080/apex;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

更新 - 有关可能有帮助的应用程序的更多信息

该应用程序是 Oracle Application Express (APEX),它在端口 8080 上进行侦听。URL 的工作方式如下:

HTTP://example.com:8080/apex/f?p=[APP]:[Page]:[Session] etc

其中[APP]、[Page]、[Session]都是对应的数字

开发环境 url 实际上是:

http://example.com:8080/apex/f?p=4550

这是默认设置,所以如果我尝试http://example.com:8080/apex/它默认为http://example.com:8080/apex/f?p=4550并带您进入登录页面

应用程序编号之后的所有内容都不会改变,所以我想用 /dev/ http://example.com:8080/apex/f?p=4550:1 -> http://example.com/dev/替换:1

一旦我了解了它的工作原理,我计划设置三个 proxy_pass

example.com/dev -> http://example.com:8080/apex/f?p=4550

example.com/desktop -> http://example.com:8080/apex/f?p=1001

example.com/mobile -> http://example.com:8080/apex/f?p=201

唯一改变的是应用程序编号。

重写对所有三个都很好,但我不希望重写在 URL 中可见

以下是重写:

   location ~ /dev {
       rewrite ^/dev(.*) http://smallblockpro.com:8080/apex$1 last;
    }
   location ~ /desktop/ {
       rewrite ^/desktop/(.*) http://smallblockpro.com:8080/apex/f?p=1001:$1 last;
    }

    location ~ /desktop {
       rewrite ^/desktop(.*) http://smallblockpro.com:8080/apex/f?p=1001:$1 last;
    }

    location ~ /mobile/ {
       rewrite ^/mobile/(.*) http://smallblockpro.com:8080/apex/f?p=201:$1 last;
    }

    location ~ /mobile {
       rewrite ^/mobile(.*) http://smallblockpro.com:8080/apex/f?p=201:$1 last;
    }
4

3 回答 3

0
location ~ /desktop/ {
    rewrite ^/desktop/(.*) http://smallblockpro.com:8080/apex/f?p=1001:$1 last;
 }

您将:8080端口号显示给用户的原因是因为您在重写指令中使用绝对 URL,这导致 NGINX301 Moved直接向用户产生响应——您假定它proxy_pass在重写后仍会通过,例如这是不正确的,请参阅http://nginx.org/r/rewrite

如果替换字符串以“http://”、“https://”或“$scheme”开头,则处理停止并将重定向返回给客户端。

如果您只想在Oracle Application Express (APEX)/desktop/$1的前端和后端创建映射/apex/f?p=1001:$1,那么最好的方法是在您的 nginx 前端服务器上使用以下代码:

location /desktop/ {
    rewrite ^/desktop/?(.*)$ /apex/f?p=1001:$1 break;
    return 400;
    proxy_pass http://smallblockpro.com:8080;
}

我建议为每个/dev/,/mobile//desktop/;复制粘贴它 另外,我不建议保留无斜线版本,根据ServerFault 的 nginx-reverse-proxy-url-rewritehow-to-remove-the-path-with-an-nginx-proxy-pass,因为 nginx 已经在您使用我上面建议的代码的情况下,处理不带斜杠的请求。

于 2018-06-20T22:26:34.273 回答
0

这是我在我们的 ORDS / SDW ( sqldev-web ) 开发服务器上使用的复制粘贴。

这是一个用于房屋 REST 端的 ORDS 的基本示例。

访问权限是:

 https://xyz.oraclecorp.com/sdw/klrice/metadata-catalog/

然后它被代理到:

 https://xyz.oraclecorp.com:8083/ords/klrice/metadata-catalog/

有了这个配置。除了不要重写为绝对 URI,因为这将执行完整的浏览器重定向,而不是仅重写代理传递的 url。

   location /sdw/ {
       rewrite /sdw/(.*) /ords/$1 break;
       proxy_pass https://xyz.oraclecorp.com:8083/ords/;
       proxy_redirect    off;
       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;

    }

您将面临的问题是

   rewrite ^/desktop/(.*) http://smallblockpro.com:8080/apex/f?p=1001:$1 last;

APEX 将看到并写入链接/重定向/包含(javascript/css/...),因为.../apex/XYZ这将访问 nginx 服务器并且不知道如何处理/apex/

这是基于我上述设置的示例。请注意,我对 /sdw/ 的请求变成了对 /ords/ 的位置重定向

 wget  -S https://xyz.oraclecorp.com/sdw/
--2018-06-21 17:10:28--  https://xyz.oraclecorp.com/sdw/
Resolving xyz.oraclecorp.com... 123.456.789.123
Connecting to xyz.oraclecorp.com|123.456.789.123|:443... connected.
HTTP request sent, awaiting response... 
  HTTP/1.1 302 Found
  Server: nginx/1.12.1


  Location: https://xyz.oraclecorp.com/ords/f?p=4550:1:375440000433:::::
Location: https://xyz.oraclecorp.com/ords/f?p=4550:1:375440000433::::: [following]

因此,最简单的做法是将 ords 部署 ( /apex/ ) 与重写/重定向的内容相匹配,并使用代理传递来内部化 :8080 内容。所以

location ~ /desktop/ {
       rewrite ^/desktop/(.*) http://smallblockpro.com/apex/f?p=1001:$1 last;
    }
location ~ /apex/ {
       proxy_pass http://smallblockpro.com:8080/apex/;
       proxy_redirect    off;
       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;
    }

此选项将使您的用户有一个很好的 /desktop/ 入口点,但随后将 /apex/ 重定向到应用程序本身。

ORDS url-mappings.xml 有另一个选项来保留/desktop/,也可以像这样将映射添加到ords,以便它知道/desktop/。然后 nginx 可以为每个入口 url 执行相同的代理传递。

url-mapping.xml 文件内容

 <pool-config xmlns="http://xmlns.oracle.com/apex/pool-config">
    <pool name="mypool" base-path="/desktop" />
   </pool-config>

然后在 nginx

location ~ /desktop/ {
       proxy_pass http://smallblockpro.com:8080/desktop/;
       proxy_redirect    off;
       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;
    }
于 2018-06-21T16:11:05.133 回答
-1

在您进一步阅读之前,请先阅读下面的 SO 线程,该线程解释了额外的/apex/

Nginx proxy_pass 只能部分工作

您的配置中有两个问题

  1. 您需要将正确的 URL 传递给后端服务
  2. 您需要确保处理任何重定向并正确替换 url

以下是我认为应该为您工作的配置

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  example.com;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

   location /dev/ {
       proxy_pass http://example.com:8080/apex/;
       proxy_redirect http://example.com:8080/apex/ $scheme://$host/dev/;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
于 2018-06-18T08:03:34.790 回答