Freedom
Freedom

Reputation: 833

nginx does not resolve upstream

I have two AP server, and I want to setup NGINX as a proxy server and load balancer.

here is my nginx.conf file:

   #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;
        large_client_header_buffers 8 1024k;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  650;

        send_timeout            2000;
        proxy_connect_timeout   2000;
        proxy_send_timeout      2000;
        proxy_read_timeout      2000;
        gzip  on;
        #
        # Load config files from the /etc/nginx/conf.d directory
        # The default server is in conf.d/default.conf

        map $http_upgrade $connection_upgrade {
          default Upgrade;
          ''      close;
        }

        upstream backend {
            server apserver1:8443;
            server apserver2:8443;
        }

        server {
          listen 8445 default ssl;
          server_name localhost;
          client_max_body_size 500M;
          client_body_buffer_size 128k;
          underscores_in_headers on;

          ssl on;
          ssl_certificate ./crt/server.crt;
          ssl_certificate_key ./crt/server.key;

          location / {
              proxy_pass https://backend;
              break;
          }
        }
    }

apserver1 and apserver2 are my AP server and in fact they are IP address.

when I visit the nginx via https://my.nginx.server:8445, I can get the AP container's default page. In my case, it is the JETTY server default page. that means the NGINX works.

if anything going correctly, user accessing to https://my.nginx.server:8445/myapp will get the log in page. if user has logged in, my app will redirect the user to https://my.nginx.server:8445/myapp/defaultResource.

when I visit via https://my.nginx.server:8445/myapp as a NOT-logged-in user, I can get the log in page correctly.

when I visit via https://my.nginx.server:8445/myapp/defaultResource directly as a logged-in user, I can get the correct page.

but when I visit the url https://my.nginx.server:8445/myapp as a logged-in user, (if correctly, the URL should be redirect to https://my.nginx.server:8445/myapp/defaultResource), but the nginx translate the URL to https://backend/myapp/defaultResource, and Chrome give me the following error:

The server at backend can't be found, because the DNS lookup failed....(omited)

nginx, seems not resolve the upstream backend. what's wrong with my configuration?

AND if I use http instead of https, everything goes well.

any help is appreciated.

Upvotes: 1

Views: 6296

Answers (1)

Andrei Belov
Andrei Belov

Reputation: 1231

Try to add the "resolver" directive to your configuration:

http://nginx.org/r/resolver

Upvotes: 1

Related Questions