nginx无法解决上游问题

时间:2014-01-24 08:48:48

标签: nginx

我有两个AP服务器,我想将NGINX设置为代理服务器和负载均衡器。

这是我的nginx.conf文件:

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

apserver1apserver2是我的AP服务器,实际上它们是IP地址。

当我通过https://my.nginx.server:8445访问nginx时,我可以获得AP容器的默认页面。在我的例子中,它是JETTY服务器默认页面。这意味着NGINX可以工作。

如果一切正常,访问https://my.nginx.server:8445/myapp的用户将获得登录页面。如果用户已登录,我的应用会将用户重定向到https://my.nginx.server:8445/myapp/defaultResource

当我作为未登录用户通过https://my.nginx.server:8445/myapp访问时,我可以正确登录页面。

当我作为登录用户直接通过https://my.nginx.server:8445/myapp/defaultResource访问时,我可以获得正确的页面。

但是当我以登录用户身份访问网址https://my.nginx.server:8445/myapp时(如果正确,网址应该重定向到https://my.nginx.server:8445/myapp/defaultResource),但是nginx会将网址转换为https://backend/myapp/defaultResource ,Chrome给我以下错误:

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

nginx,似乎无法解析上游backend。我的配置有什么问题?

如果我使用http代替https,一切顺利。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试将“resolver”指令添加到您的配置中:

http://nginx.org/r/resolver