Forward Requests Based on URL Matches
The Nginx location
directive in the ngnix.conf file enables you to route
requests to a location in the file system.
While Nginx matches or searches a location
block against the requested
URL, the location
directive tells Nginx where to search for a specific path
by including all files and directories.
The location
directive has the following syntax:
location [modifier] [URL-match] {
...
}
For more information about how to use regular expressions (regex) to forward requests to specific locations, see https://www.nginx.com/blog/regular-expression-tester-nginx/.
The following example shows how to redirect requests to specific Universal Messaging servers by using regex:
server {
listen 80;
server_name proxy_server;
location / {
#Important: Nginx must continuously send data to Universal Messaging clients
#rather than buffering it.
proxy_buffering off;
#Important: Configure proxy http 1.1 protocol version to enable the connection
#keepalive and rewrite the Connection header.
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://umhost:port;
}
location /umserver1/ {
#Important: Nginx must continuously send data to Universal Messaging clients
#rather than buffering it.
proxy_buffering off;
#Important: Configure proxy http 1.1 protocol version to enable the connection
#keepalive and rewrite the Connection header.
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://umserver1host:port;
}
location = /umserver2 {
#Important: Nginx must continuously send data to Universal Messaging clients
#rather than buffering it.
proxy_buffering off;
#Important: Configure proxy http 1.1 protocol version to enable the connection
#keepalive and rewrite the Connection header.
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://umserver2host:port;
}
}
In the example, traffic is redirected as follows:
location /umserver1/
- requests that start withumserver1
are redirected tohttp://umserver1host:port
.location = /umserver2
- requests that match theumserver2
block are redirected tohttp://umserver2host:port
.- Requests that do not match
location /umserver1/
orlocation = /umserver2
are served by the defaultlocation
directive.
Client Code Example
public void createSessionAndCreateChannel(String arg) throws Exception {
// The request initiated with nhp://localhost:80/ without any endpoint is redirected to
// http://umhost:port; in the code sample above.
nSessionAttributes defaultAttr = new nSessionAttributes(“nhp://locahost:80/”);
defaultAttr.setName("defaultAttr-client");
nSession defautServerSession= nSessionFactory.create(defaultAttr);
defautServerSession.init();
nChannel chan = session.createChannel(new nChannelAttributes(“defaultServerChannel”));
defautServerSession.close();
// The request initiated with nhp://localhost:80/umserver1 without any endpoint is
// redirected to http://umserver1host:port; in the code sample above.
nSessionAttributes attr1 = new nSessionAttributes(“nhp://locahost:80/umserver1”);
attr1.setName("umserver1-client");
nSession session1 = nSessionFactory.create(attr1);
session1.init();
nChannel chan1 = session.createChannel(new nChannelAttributes(“umserver1Channel”));
session1.close();
// The request initiated with nhp://localhost:80/umserver2 without any endpoint is
// redirected to http://umserver2host:port; in the code sample above.
nSessionAttributes attr2 = new nSessionAttributes(“nhp://locahost:80/umserver2”);
attr2.setName("umserver2-client");
nSession session2 = nSessionFactory.create(attr2);
session2.init();
nChannel chan2 = session.createChannel(new nChannelAttributes(“umserver2Channel”));
session2.close();
}