How to configure Location directive for different context URI - nginx

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

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;
}

Related

nginx proxy_pass working but static content doesn't

I can set up nginx as a reverse proxy with no major issues, but if I do a simple static page test like this, the server doesn't serve pages:
server {
server_name localhost;
listen 12345;
location / {
root /Volumes/E/static/;
index index.html index.htm;
}
}
error.log says:
2023/02/09 22:39:10 [crit] 53512#0: *18710 open() "/Volumes/E/static/index.html" failed (1: Operation not permitted), client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:12345"
...and I get a "500 Internal Server Error" in the browser when entering http://localhost:12345/.
I've tried chmod -R 755 /Volumes/E/static, no effect.
Why is that?

Try_files directive keeps looking for files in /usr/local/nginx/html

I can't get my head round why my nginx config keeps looking for files in the wrong directory.
I'm trying to map the /files location to /home/user/toto. For that I'm using a regex capture group to remove the /files prefix and search for the files in the directory specified in the root directive.
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
root /home/user/toto;
try_files /$1/ /$1 ;
autoindex off;
}
}
The problem is I only get 404s. And that's pretty normal because here is what we can find in error.log :
2022/07/17 11:27:49 [error] 40358#0: *40 open() "/usr/local/nginx/html/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
As we can see the root directive seems to be completely ignored.
I tried an alternative configuration such as :
server {
listen 80;
server_name localhost;
location ~^\/files\/(.*)$ {
# root /home/user/toto;
try_files /home/user/toto/$1/ /home/user/toto/$1 ;
autoindex off;
}
}
But the problem is still the same. The only change is that the path now appears in the logs as a prefix for the file name :
2022/07/17 11:12:02 [error] 40288#0: *36 open() "/usr/local/nginx/html/home/user/toto/test.txt" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /files/test.txt HTTP/1.1", host: "localhost"
What am I missing ?
Your missing the last parameter from the try_files directive, so that the /$1 is being interpreted as an internal redirect instead of a file term.
Use:
try_files /$1/ /$1 =404;
See this document for details.

How to configure nginx proxy?

Here is what I need,
when its localhost:80 I need Nginx welcome page to be served, but when its localhost:80/pass, my application(react app) to be served, here is my Nginx.conf looks like,
server {
listen 80;
server_name localhost;
location /pass {
root build;
index index.html index.htm;
proxy_pass http://localhost:3000;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
am I missing something, because it's serving me this
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.If you are the system administrator of this resource then you should check the error log for details.Faithfully yours, nginx.
here is my error.log
2020/09/19 21:54:00 [error] 10888#1388: *35 connect() failed (10061: No connection could be made because the target machine actively refused it) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /pass HTTP/1.1", upstream: "http://[::1]:3000/", host: "localhost"
2020/09/19 21:54:39 [error] 10888#1388: *35 connect() failed (10061: No connection could be made because the target machine actively refused it) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /pass HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost"

How to serve folder using location

I would like to match a specific location URL to serve a static file on my website
Here is the two behaviors that I would like to implement :
(WORKING but an error appears on the logs)
http://mywebsite.net serves /var/www/html/portfolio/index.html
(NOT WORKING) http://mywebsite.net/project serves /var/www/html/project/build/index.html
Here is my actual code :
server {
listen 80 default_server;
listen [::]:80 default_server;
error_log /home/florian/nginx_www.error.log;
root /var/www/html/portfolio;
index index.html;
location = /project {
root /var/www/html/project/build;
index index.html;
}
}
Here is the actual error I get :
2019/01/19 20:38:47 [error] 7780#7780: *4 open()
"/var/www/html/project/build/project" failed (2: No such file or
directory), client: XX.XX.XX.XX, server: , request: "GET /project
HTTP/1.1", host: "mywebsite.xx.net"
This behavior is actually normal because this URL doesn't give anything correct.
I would like that the URL served:
/var/www/html/project/build/project
becomes
/var/www/html/project/build/
Additionnaly, I also have an issue when I reach the my website using this URL http://mywebsite.net . However the files are still being served
2019/01/19 20:37:46 [error] 7780#7780: *5 open()
"/var/www/html/portfolio/undefined" failed (2: No such file or
directory), client: XX.XX.XX.XX, server: , request: "GET /undefined
HTTP/1.1", host: "mywebsite.xx.net", referrer:
"http://mywebsite.xx.net/"

nginx fails to use proxy_pass when handling error

I am trying to configure nginx to serve up error pages from an s3 bucket.
To that end my configuration looks like this:
location / {
error_page 404 = #fallback;
}
location #fallback {
rewrite ^ /my-s3-bucket/404.html;
proxy_pass https://s3.ap-northeast-2.amazonaws.com;
}
My expectation is that anything that hits the website and is not found is then sent to the #fallback location. I then want to rewrite the URL with the actual location of my 404 page and send on to the s3 bucket. I don't want to just 302 redirect to the 404 page.
The problem is that the proxy_pass directive is not executed. Instead, it just looks for my rewritten URL locally.
See my access logs below:
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/sdfd" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/my-s3-bucket/404.html" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
I made a request to www.dev.mywebsite.com.au/sdfd which wasn't found. 'sdfs' was rewritten to 'my-s3-bucket/404.html' but instead of then proxy passing that to https://s3.ap-northeast-2.amazonaws.com it looks for it in the local /etc/nginx/html directory.
My nginx version is 1.15.2
Use rewrite...break if you want the rewritten URI to be processed within the same location block. See this document for more.
For example:
location #fallback {
rewrite ^ /error/404.html break;
proxy_pass https://example.com;
}

Resources