user2315104
user2315104

Reputation: 2730

How to configure Location directive for different context URI

what is happening:

Im using following nginx.conf file for load balancing. web application is up and running on nginx 8080 port and able to access the landing page. however, when moving from landing page to "signup" page, it is throwing error.

what is expected:

nginx load balancer should redirect the load to the page as mentioned in the Location directive. but that is not happening.

nginx file :

events {

}

http {

upstream 3.121.253.126 {
    server 3.121.253.126:8080;
    server 3.121.253.126:8080;
    server 3.121.253.126:8080;
 }
  error_log /etc/nginx/error_log.log warn;
  client_max_body_size 20m;

  proxy_cache_path /etc/nginx/cache keys_zone=one:500m max_size=1000m;

  server {
    listen 8080;
    server_name 3.121.253.126;
    root /etc/nginx/html;
    index index.html;

    location /signup {
      root /etc/nginx/html;
      index add-user.html;

    #  proxy_pass http://localhost:8080/signup;
    #  proxy_set_header Host $host;
   #   rewrite ^/welcome(.*)$ $1 break;
    }

  }

}

here is the error log:

2019/02/21 09:07:42 [error] 6#6: *510 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: 3.121.253.126, request: "GET /signup HTTP/1.0", upstream: "http://127.0.0.1:8080/signup", host: "localhost:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:07:42 [warn] 6#6: *510 upstream server temporarily disabled while reading response header from upstream, client: 127.0.0.1, server: 3.121.253.126, request: "GET /signup HTTP/1.0", upstream: "http://127.0.0.1:8080/signup", host: "localhost:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:13:10 [error] 6#6: *1 open() "/etc/nginx/html/signup" failed (2: No such file or directory), client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup HTTP/1.1", host: "3.121.253.126:8080", referrer: "http://3.121.253.126:8080/" 2019/02/21 09:15:57 [error] 6#6: *3 open() "/etc/nginx/html/signup" failed (2: No such file or directory), client: 157.33.175.127, server: 3.121.253.126, request: "GET /signup HTTP/1.1", host: "3.121.253.126:8080", referrer: "http://3.121.253.126:8080/"

as per log, it is expecting signup html file. however, i am instructing it to use the add-user.html file. not sure why this is not happening.

please suggest

Upvotes: 1

Views: 1039

Answers (1)

Richard Smith
Richard Smith

Reputation: 49752

You want to point the URI /signup to the file located at /etc/nginx/html/add-user.html

There are a number of ways to achieve that using Nginx, including the rewrite and try_files directives.

For example:

location /signup {
    try_files /add-user.html =404;
}

The root directive does not need to be repeated within this location block, as it will inherit the same value from the surrounding block.

The =404 does nothing as add-user.html always exists, but try_files requires two parameters. See this document for details.

The above location will process any request that begins with /signup (e.g. /signup/ or /signups).

To restrict it to the single URI /signup use the = modifier. See this document for details.

For example:

location = /signup {
    try_files /add-user.html =404;
}

Upvotes: 1

Related Questions