Open resty lua script in localhost:8082 not resolving http://localhost/foo/hostname - nginx

I am trying to access a localhost service (http://localhost/foo/hostname) using an open resty lua script using proxy_pass in the wsl-Ubuntu running in a windows 10 machine.
This is the content of nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
server {
listen 8082;
location /{
resolver 8.8.4.4;
set $target 'localhost/foo/hostname';
proxy_pass http://$target;
}
}
}
I got a '502 Bad gateway' response when doing a curl to localhost:8082
This is the error found in the error.log file
2023/01/27 09:31:17 [error] 331#331: *1 localhost could not be resolved (110: Operation timed out), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8082"
I tried out various solution including modifying the resolver option.

This is issue was solved by specifying the localhost IP address (127.0.0.1) instead of localhost in the specific url.
worker_processes 2;
events {
worker_connections 1024;
}
http {
server {
listen 8082;
location /{
resolver 8.8.4.4;
set $target '127.0.0.1/foo/hostname';
proxy_pass http://$target;
}
}
}
If anyone know what the actual scenario or reason behind being unable to resolve 'localhost' but possible with '127.0.0.1', kindly share your knowledge.

Related

nginx execute incorrect path with alias

I don't understand my nginx not serve file from '/var/www/html/foo/' when I execute 'w3m http://localhost/foo/test.html'?
I get 404 error, despite of existing test.html in '/var/www/html/foo/'. When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'.
user nobody nogroup;
worker_processes 2;
events {
worker_connections 512;
}
http {
server {
listen *:80;
listen *:1026;
server_name "test";
root /var/www/html/nginx/ ;
location foo/ {
alias /var/www/html/foo/ ;
}
}
}
arek#127:~$ ls /var/www/html/nginx
test.html
arek#127:~$ ls /var/www/html/foo
index.html test_bigger.html test.html text.html
arek#127:~$
When I checked log I see that it looking for page in '/var/www/html/nginx/foo/test.html'
2022/09/03 02:36:05 [error] 139475#139475: *2 open() "/var/www/html/nginx/foo/test.html" failed (2: No such file or directory), client: 127.0.0.1, server: test, request: "GET /foo/test.html HTTP/1.0", host: "localhost"
when I change my root path on '/var/www/html/' its works.
Try it with the server configuration block like this instead, which is working on my server. Removed the trailing slash from the root, and added a slash before foo/. Also added a default_type for the location. If you still get an error, comment with the log showing the query path.
server {
listen 80;
listen 1026;
server_name "test";
root /var/www/html/nginx;
location /foo/ {
alias /var/www/html/foo/;
default_type "text/html";
}
}

Nginx proxy_pass through 2 servers and custom headers getting "lost"

I am having a problem with my Nginx configuration.
I have an Nginx server(A) that adds custom headers and then that proxy_passes to another server(B) which then proxy passes to my flask app(C) that reads the headers. If I go from A -> C the flask app can read the headers that are set but if I go through B (A -> B -> C) the headers seem to be removed.
Config
events {
worker_connections 512;
}
http {
# Server B
server {
listen 127.0.0.1:5001;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
# Server A
server {
listen 4999;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:5001;
proxy_set_header X-Forwarded-User 'username';
}
}
}
Flask app running on 127.0.0.1:5000
If I change the server A config to proxy_pass http://127.0.0.1:5000 then the Flask app can see the X-Forwarded-User but if I go through server B the headers are "lost"
I am not sure what I am doing wrong. Any suggestions?
Thanks
I can not reproduce the issue, sending the custom header X-custom-header: custom in my netcat server i get:
nc -l -vvv -p 5000
Listening on [0.0.0.0] (family 0, port 5000)
Connection from localhost 41368 received!
GET / HTTP/1.0
Host: 127.0.0.1:5000
Connection: close
X-Forwarded-User: username
User-Agent: curl/7.58.0
Accept: */*
X-custom-header: custom
(see? the X-custom-header is on the last line)
when i run this curl command:
curl -H "X-custom-header: custom" http://127.0.0.1:4999/
against an nginx server running this exact config:
events {
worker_connections 512;
}
http {
# Server B
server {
listen 127.0.0.1:5001;
server_name 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:5000;
}
}
# Server A
server {
listen 4999;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:5001;
proxy_set_header X-Forwarded-User 'username';
}
}
}
thus i can only assume that the problem is in the part of your config that you isn't showing us. (you said it yourself, it's not the real config you're showing us, but a replica. specifically, a replica that isn't showing the problem)
thus i have voted to close this question as "can not reproduce" - at least i can't.

Setting up Nginx - Nginx placing upstream name in URL

Why is nginx is nginx placing the upstream name in the redirected URL?
This is my nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
upstream servs {
server facebook.com;
}
server {
listen 80;
location / {
proxy_pass http://servs;
}
}
}
When I access the port 80, I get:
This site can’t be reached
servs.facebook.com’s server DNS address could not be found.
Why is it placing "servs." before facebook.com?
You are not setting the Host header in the upstream request, so nginx constructs a value from the proxy_pass directive. As you are using an upstream block, this value is the name of the upstream block, rather than the name of the server you are trying to access.
If you are using an upstream block, it may be advisable to set the Host header explicitly:
proxy_set_header Host example.com;
See this document for more.

nginx not caching response from reverse proxy

http://nginx.org/en/docs/http/ngx_http_memcached_module.html
Basic config is here:
worker_processes 2;
events {
worker_connections 1024;
}
error_log /var/log/nginx/nginx_error.log warn;
error_log /var/log/nginx/nginx_error.log info;
http {
upstream backend {
server localhost:3000;
}
server {
listen 80;
location / {
set $memcached_key $uri;
memcached_pass 127.0.0.1:11211;
error_page 404 = #fallback;
}
location #fallback {
proxy_pass http://backend;
}
}
}
It reverse proxy's the request when hitting port 80, but the logs always say:
2016/08/23 15:25:19 [info] 68964#0: *4 key: "/users/12" was not found by memcached while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /users/12 HTTP/1.1", upstream: "memcached://127.0.0.1:11211", host: "localhost"
Nginx Memcached module does not write to the Memcached server. You should do this in your backend (for example PHP) using the $memcached_key

nginx blancer config upstream

I have 2 nodes files and user anther server blancer of them.
My config:
worker_processes 40;
events {
worker_connections 2000;
}
http {
upstream backend {
server 192.168.1.44:80;
}
server {
listen *:80;
server_name 5.9.XX.XX ;
location / {
proxy_pass http://backend;
}
}
}
My problem is when I try to work it not get any data but when I try to use:
proxy_pass http://192.168.1.44:80;
it's working good.
I am confused. Where is the problem?
it's fixed when i try use port 8080 with upstream working but i have problem some urls 404 redirect with port 8080 not 80

Resources