Re: nginx configured as loadbalancer returning 404 not found

2024-05-17 Thread Kaushal Shriyan
On Fri, May 17, 2024 at 7:39 PM Sergey A. Osokin  wrote:

> Hi Kaushal,
>
> On Fri, May 17, 2024 at 04:49:59PM +0530, Kaushal Shriyan wrote:
> >
> > I am running nginx version 1.26 on "Ubuntu 22.04.4 LTS" I have configured
> > the nginx as load balancer and the configuration details are as follows
> >
> > # nginx -v
> > nginx version: nginx/1.26.0
> > #
> >
> > server {
> [...]
> >
> > location / {
> > # Define the upstream servers for load balancing
> > proxy_pass http://backend/;
>
> Could you please explain a reason why did you decide to use `/' after
> the backend's name in the proxy_pass directive.
>
> > # Set HTTP headers
> > proxy_set_header Host $host;
> > proxy_set_header X-Real-IP $remote_addr;
> > proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> > proxy_set_header X-Forwarded-Proto $scheme;
> > }
> >
> > location /api/docs/ {
> > proxy_pass http://backend/api/docs/;
>
> It seems like '/api/docs/' can be safely removed, so
> I'd recommend to read the documentation for the proxy_pass directive, [1]
>
> 
>
> If proxy_pass is specified without a URI, the request URI is passed to the
> server in the same form as sent by a client when the original request is
> processed, or the full normalized request URI is passed when processing
> the changed URI:
>
> location /some/path/ {
> proxy_pass http://127.0.0.1;
> }
>
> 
>
> [...]
>
> > When i hit http://tead-local.com:80/api/docs/ I get http 200 response
> from
> > the backend server whereas when I try to hit using public IP :-
> > http://210.11.1.110:8085/api/docs/ I encounter http 404 not found.
> >
> > 101.0.62.200 - - [17/May/2024:16:38:24 +0530] "GET /api/docs/ HTTP/1.1"
> 404
> > 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
> > AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15
> > Ddg/17.5" "-"
>
> To see the whole picture of processing a request by nginx, I'd
> also recommend to enable a debugging log, [2].
>
> Hope that helps.
>
> References
> --
> 1. https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
> 2. https://nginx.org/en/docs/debugging_log.html
>
> --
> Sergey A. Osokin
>

Thanks Sergey for the detailed explanation. I have modified the
/etc/nginx/conf.d/loadbalancer.conf file (nginx server running in
loadbalancer mode). The upstream backend -> tead-local.com:80 is hosted on
docker based container running nginx service (version :- 1.21.6)

##loadbalancer.conf###
server {
listen 80;
server_name testbe.mydomain.com;
error_log /var/log/nginx/nginxdebug.log debug;

location / {
# Define the upstream servers for load balancing
proxy_pass http://backend;
# Set HTTP headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
   error_log /var/log/nginx/nginxlocationdebug.log debug;
}
}

upstream backend {
server tead-local.com:80;
}

##

[image: image.png]
# ll
total 12
drwxr-xr-x  2 root adm  93 May 18 01:05 ./
drwxrwxr-x 15 root syslog 4096 May 16 16:33 ../
-rw-r--r--  1 root root621 May 18 01:05 access.log
-rw-r--r--  1 root root594 May 18 01:05 error.log
-rw-r--r--  1 root root  0 May 18 01:05 nginxdebug.log
-rw-r--r--  1 root root  0 May 18 01:05 nginxlocationdebug.log
#

root@lb-01:/var/log/nginx# cat error.log
2024/05/18 01:05:15 [notice] 539625#539625: using the "epoll" event method
2024/05/18 01:05:15 [notice] 539625#539625: nginx/1.26.0
2024/05/18 01:05:15 [notice] 539625#539625: built by gcc 11.4.0 (Ubuntu
11.4.0-1ubuntu1~22.04)
2024/05/18 01:05:15 [notice] 539625#539625: OS: Linux 5.15.0-105-generic
2024/05/18 01:05:15 [notice] 539625#539625: getrlimit(RLIMIT_NOFILE):
1024:524288
2024/05/18 01:05:15 [notice] 539626#539626: start worker processes
2024/05/18 01:05:15 [notice] 539626#539626: start worker process 539627
2024/05/18 01:05:15 [notice] 539626#539626: start worker process 539628
root@lb-01:/var/log/nginx# ll

# cat access.log
101.0.62.200 - - [18/May/2024:01:05:19 +0530] "GET /api/docs HTTP/1.1" 404
555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-"
101.0.62.200 - - [18/May/2024:01:05:20 +0530] "GET /api/docs HTTP/1.1" 404
555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36" "-"
101.0.62.200 - - [18/May/2024:01:05:21 +0530] "GET /api/docs HTTP/1.1" 404
555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36
(KHTML, like 

Re: nginx configured as loadbalancer returning 404 not found

2024-05-17 Thread Sergey A. Osokin
Hi Kaushal,

On Fri, May 17, 2024 at 04:49:59PM +0530, Kaushal Shriyan wrote:
> 
> I am running nginx version 1.26 on "Ubuntu 22.04.4 LTS" I have configured
> the nginx as load balancer and the configuration details are as follows
> 
> # nginx -v
> nginx version: nginx/1.26.0
> #
> 
> server {
[...]
> 
> location / {
> # Define the upstream servers for load balancing
> proxy_pass http://backend/;

Could you please explain a reason why did you decide to use `/' after
the backend's name in the proxy_pass directive.

> # Set HTTP headers
> proxy_set_header Host $host;
> proxy_set_header X-Real-IP $remote_addr;
> proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> proxy_set_header X-Forwarded-Proto $scheme;
> }
> 
> location /api/docs/ {
> proxy_pass http://backend/api/docs/;

It seems like '/api/docs/' can be safely removed, so
I'd recommend to read the documentation for the proxy_pass directive, [1]



If proxy_pass is specified without a URI, the request URI is passed to the
server in the same form as sent by a client when the original request is
processed, or the full normalized request URI is passed when processing
the changed URI:

location /some/path/ {
proxy_pass http://127.0.0.1;
}



[...]

> When i hit http://tead-local.com:80/api/docs/ I get http 200 response from
> the backend server whereas when I try to hit using public IP :-
> http://210.11.1.110:8085/api/docs/ I encounter http 404 not found.
> 
> 101.0.62.200 - - [17/May/2024:16:38:24 +0530] "GET /api/docs/ HTTP/1.1" 404
> 153 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
> AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Safari/605.1.15
> Ddg/17.5" "-"

To see the whole picture of processing a request by nginx, I'd
also recommend to enable a debugging log, [2].

Hope that helps.

References
--
1. https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
2. https://nginx.org/en/docs/debugging_log.html

-- 
Sergey A. Osokin
___
nginx mailing list
nginx@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx