Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We are using nginx running on one host as a proxy to a java servlet running in tomcat on another host.

What we want to do is say for a request http://a.xyz.com/a/b?q1=v1&q2=v2, we want to serve an HTML code to client from nginx and also ping an upstream server http://ipaddress[colon]port/a/b?q1=v1&q2=v2. The uri path and query params remains same while pinging upstream server.

I've figured how to do both the tasks individually but I'm unable to figure how to do both of them together for the same request.

This is how my conf file is looking so far.I have written two if conditions below. On using if condition 1 alone, I'm able to serve the HTML code. On using if condition 2 alone, I'm able to ping upstream server and in that case upstream server responds back with 200 OK which is sent to client via nginx. Could some one please help in achieving both of them together ? Nginx should serve the html code and release the client and then ping upstream server which will return back 200 OK to nginx which nginx can just swallow.

upstream u_server {
    server <ip_address>:8080;
}
server {
    listen 80;
    server_name a.xyz.com;
    # if condition 1
    if ($host = "a.xyz.com") {
         rewrite ^ /pqr.html last;
    }
    # if condition 2
    if ($host = "a.xyz.com") {
        set $upserver="u_server";
    }
    location /pqr.html {
            root /opt/mydir/html;
    }
    location / {
            proxy_pass http://$upserver;
    }
}
share|improve this question
add comment

1 Answer

This сan be done by at least two methods (both with 3rd-party modules).

A) lua module.

upstream upserver {
    server <ip_address>:8080;
}

server {
    listen 80;
    server_name a.xyz.com;

    location / {

        root /opt/mydir/html;

        access_by_lua '
            local res = ngx.location.capture("/ping")
            return
        ';
    }

    location = /ping {
        proxy_pass http://upserver;
    }

}

B) auth_request module. Your ping backend MUST return "200 Ok" response ALWAYS, or the client will receive "403 Access denied" instead static file content.

upstream upserver {
    server <ip_address>:8080;
}

server {
    listen 80;
    server_name a.xyz.com;

    location / {
        root /opt/mydir/html;
        auth_request /ping;
    }

    location = /ping {
        proxy_pass http://upserver;
    }

}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.