Dreadnaught1905
Dreadnaught1905

Reputation: 11

Nginx Reverse proxy to different server

We're trying to setup a reverse proxy to redirect traffic to an internal network from a DMZ webserver, using nginx.

The traffic comes in on 443, hits an apache page user logs in against an ldap server, then we are redirecting the traffic from apache to nginx (which is listening on 8089). Nginx is then to proxy that traffic back to the internal network.

The apache redirect works, and at least some of the nginx proxy is working. The page on the internal server loads, but all of the resources on the page are double appending the URL. For example:

https ://webserver.us.com ==redirects==> http ://webserver.us.com:8089 ==Nginx Proxy==> http ://internal.server.com:8080/page

http: //internal.server.com/page loads, but all of the resources on the page are trying to load as:

http: //internal.server.com/page/page/resource.

when hitting the internal page directly, they are of course http: //internal.server.com:8080/page/resource

I am sure that there is an error in our setup of the proxy_pass, proxy_redirect and root / location, but I can't figure it out...

Our Nginx.conf is as follows: (FYI this is a solaris server)

worker_processes  1;
pid        /var/run/nginx.pid;

    events {
        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;      
        keepalive_timeout  65;




    server {
        listen       8089;
        server_name  <internal.server.com>;

        access_log  /var/opt/csw/nginx/logs/access.log;
        error_log  /var/opt/csw/nginx/logs/error.log;
        root   http://<internal.server.com>:8080;
        index  index.html index.htm;

        location  / {
         proxy_pass  http://<internal.server.com>:8080/<page>/;
         proxy_redirect http://<internal.server.com>:8080/<page> http://<internal.server.com>:8080/<page>;
         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
         proxy_buffering off;
         proxy_set_header        Host            $host;
         proxy_set_header        X-Real-IP       $remote_addr;
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
       }
    }

    }

Whenever we set anything in the "location" parameter other than "/" it returns an nginx 404, and the error log shows: "/var/opt/csw/nginx//RES_NOT_FOUND"

Upvotes: 1

Views: 2941

Answers (1)

Richard Smith
Richard Smith

Reputation: 49792

The client requests webserver.us.com/ which is translated to internal.server.com:8080/page/ through the reverse proxy.

The document specifies resource URIs like /page/resource, so the client requests these as webserver.us.com/page/resource which is translated to internal.server.com:8080/page/page/resource through the reverse proxy.

The simplest solution is to make the reverse proxy transparent. You can add an exact match location to map / to your application's entry point:

location = / {
    rewrite ^ /page/ last;
}

Upvotes: 1

Related Questions