Nginx Location Directive fail - nginx

I'm trying this nginx.conf:
http {
server {
listen 80;
location / {
root /usr/share/nginx/html/;
}
location /pages/ {
root /usr/share/nginx/html/static/;
}
}
}
Inside /usr/share/nginx/html/static/pages/ there is a file page1.html.
But, when I do GET /pages/page1.html, I receive this error: [error] 31#31: *2 open() "/usr/share/nginx/html/pages/page1.html" failed (2: No such file or directory), client: 172.17.0.1, server: , request: "GET /pages/page1.html HTTP/1.1", host: "localhost:8080".
Nginx is a dockerized container with port 8080 mapped to 80.
Why doesn't Nginx concatenate /static/ in the path?

Based on recommendation made by #palindromeotter33, this is the answer:
http {
server {
listen 80;
location / {
root /usr/share/nginx/html/;
}
location /pages/ {
alias /usr/share/nginx/html/static/pages/;
}
}
}

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 No such file or directory (webroot redefine

I want to provide two static build files on one server.
then my nginx code
map $http_referer $webroot {
default "/html";
"~/build2" " /home/ubuntu/build_files/build2";
}
server {
listen 80;
server_name localhost;
location / {
root /home/ubuntu/build_files/build/;
index index.html index.htm;
}
location /build2 {
alias /home/ubuntu/build_files/build2/;
try_files $uri $uri/ /index.html;
}
location /static {
root $webroot;
}
}
}
I have redefine webroot.
I think I did well.
But I saw an error.
2022/05/06 02:01:30 [error] 400241#400241: *514 open() "/etc/nginx/ /home/ubuntu/build_files/build2/static/js/main.ff7ad0eb.js" failed (2: No such file or directory), client: localhost, server: localhost, request: "GET /static/js/main.ff7ad0eb.js HTTP/1.1", host: "localhost", referrer: "http://localhost/build1/"
The file exists in that path.
What did I do wrong?
I'd appreciate it if you could let me know what I did wrong.
And it would be of great help to me if you could tell me which part to study.
thank you so much for reading this question!

how can I use external URI with the nginx's auth_request module

I'm trying to use nginx's ngx_http_auth_request_module in such way:
server {
location / {
auth_request http://external.url;
proxy_pass http://protected.resource;
}
}
It doesn't work, the error is:
2017/02/21 02:45:36 [error] 17917#0: *17 open() "/usr/local/htmlhttp://external.url" failed (2: No such file or directory), ...
Or in this way with named location:
server {
location / {
auth_request #auth;
proxy_pass http://protected.resource;
}
location #auth {
proxy_pass http://external.url;
}
}
In this case the error is almost the same:
2017/02/22 03:13:25 [error] 25476#0: *34 open() "/usr/local/html#auth" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", subrequest: "#auth", host: "127.0.0.1"
I know there is a way like this:
server {
location / {
auth_request /_auth_check;
proxy_pass http://protected.resource;
}
location /_auth_check {
internal;
proxy_pass http://external.url;
}
}
But in this case the http://protected.resource can not use the /_auth_check path.
Is there a way to use an external URI as a parameter for the auth_request directive without overlapping the http://protected.resource routing?
If not, why?
It looks a little bit strange to look for the auth_request's URI through static files (/usr/local/html).
There is a little known fact that you don't have to start location with / or #.
So this would work:
location / {
auth_request .auth;
proxy_pass http://protected.resource;
}
location .auth {
internal;
proxy_pass http://external.url/auth;
# you must use path part here ^
# otherwise it would be invalid request to external.url
}

xml sitemap forbidden for access via nginx

My nginx site configuraton:
upstream nodeName {
server serverIp:7050;
server serverIp:7049 backup;
}
server {
listen 80;
server_name domain.com;
gzip_types application/x-javascript text/css;
access_log /pathToLogs/log.access.log;
location / {
proxy_pass http://nodeName/;
}
location ~ ^/(min/|images/|bootstrap/|ckeditor/|img/|javascripts/|apple-touch-icon-ipad.png|apple-touch-icon-ipad3.png|apple-touch-icon-iphone.png|apple-touch-icon-iphone4.png|generated/|js/|css/|stylesheets/|robots.txt|humans.txt|favicon.ico|xml/) {
root /pathToSite/appdirectory-build;
access_log off;
expires max;
}
}
I want to get sitemap.xml by url http://example.com/xml/sitemap.xml but it gives me
403 Forbidden
nginx/1.4.6 (Ubuntu)
Update
In nginx error log file:
2014/10/09 [error] 16094#0: *49762240 open() "/pathToSite/appdirectory-build/xml/sitemapCallEn.xml" failed (13: Permission denied), client: Ip, server: domain.com, request: "GET /xml/sitemapCallEn.txt HTTP/1.1", host: "domain"
How to change permission and allow to open xml file?
Look into nginx error log. You will find answer there. It's file permission issue likely

Alias uses --prefix instead of server block's root

I'm trying to set up a nginx server and alias my static files.
server {
# Listen on localhost:8000;
listen 8000;
# Should be the root
root /Users/rouvenherzog/Documents/projects/nd;
# host matches localhost
server_name localhost;
location / {
proxy_pass http://localhost:5000;
}
location /favicon.ico {
root /Users/rouvenherzog/Documents/projects/nd/n/n/static/img;
}
location ~ /([\w]+)/n/static/(.*) {
alias n/n/static/$2;
}
location ~ /([\w]+)/nb/static/(.*) {
alias nb/nb/static/$2;
}
}
When request static files, it looks for them in the nginx --prefix folder ( which is /usr/local/Cellar/nginx/1.6.0_1 ), instead of the root folder.
For instance:
open() "/usr/local/Cellar/nginx/1.6.0_1/n/n/static/neb/js/javascript.js" failed
(2: No such file or directory),
client: 127.0.0.1,
server: localhost,
request: "GET /pages/n/static/neb/js/javascript.js HTTP/1.1",
host: "localhost:8000",
referrer: "http://localhost:8000/pages/n/"
Any idea why ?
Thank you very much!
As #akawhy suggested, using a rewrite instead of alias works and respects the root path.
server {
# Listen on localhost:8000;
listen 8000;
# Should be the root
root /Users/rouvenherzog/Documents/projects/nd;
# host matches localhost
server_name localhost;
location / {
proxy_pass http://localhost:5000;
}
location /favicon.ico {
root /Users/rouvenherzog/Documents/projects/nd/n/n/static/img;
}
location ~ /([\w]+)/n/static/(.*) {
rewrite ^([\w]+)/n/static/(.*)$ /n/n/static/$2 break;
}
location ~ /([\w]+)/nb/static/(.*) {
rewrite ^([\w]+)/nb/static/(.*)$ /nb/nb/static/$2 break;
}
}
Because your alias directive used a relative path. I think you should use a absolute path instead.
You may check this nginx alias+location directive

Resources