Location block is ignored - nginx

I testing out nginx but I bumped into a severe problem. Somehow the location tag doesn't catch my uri's. When the browser, or I manually try to access /favicon.ico on my localhost, it just throws an 404 error. The configuration looks like the following code:
server {
listen 80;
server_name localhost;
root www;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.php;
}
location = /favicon.ico {
alias alias /assets/images/favicon.ico;
}
... and the error log looks like this:
2014/01/17 00:05:18 [error] 7408#10036: *82 CreateFile() "C:\Users\user\Webb\nginx/www/favicon.ico" failed (2: FormatMessage() error:(15105)), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost"
The log proves then that the location block was ignored, because the path nginx look for the file is not the one alias points to.

AFAIK nginx alias goes like this:
alias <the base path>
means that with it you tell the nginx the part to the left of your first "/" in the URI.
example. suppose I have my images in /var/www/html/images and my application refers to apic.jpg as http://app.com/pictures/apic.jpg , what I do then is this:
location ^/pictures {
alias /var/www/html/images;
}
hope this helps.

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!

NGINX mirror files uploaded with HTTP PUT

Need to mirror all newly uploaded files from oroginal server to new one.
Direct file upload (HTTP PUT) to new server works fine. When i upload file to original server, file appers only there. Logs on a new server access log:
10.31.181.61 - - [28/Dec/2018:13:08:30 +0300] "PUT / HTTP/1.0" 409 167 "-" "curl/7.47.0" "-"
error log:
2018/12/28 13:08:30 [error] 28983#0: *23 cannot PUT to a collection, client: 10.31.181.61, server: _, request: "PUT / HTTP/1.0", host: "10.31.181.60"
what does this error mean: cannot PUT to a collection
Is it possible to make this setup?
Thanks
Nginx config on original server
location / {
autoindex on;
mirror /mirror;
mirror_request_body on;
}
location /mirror {
proxy_pass http://newserver/;
proxy_pass_request_body on;
}
Config on new server:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /web;
dav_methods PUT;
location / {
autoindex on;
}
You should also add the filename to the url that you are using with PUT

django /admin not found

I try to setup Nginx+Gunicorn and when I go by my URL the Nginx redirects request to my app and handles it by itsels for static resource (static folder). Below my Nginx domain config:
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location = /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}
Gunicorn daemon:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ghostman
Group=www-data
WorkingDirectory=/home/ghostman/Projects/asknow/asknow
ExecStart=/home/ghostman/Projects/asknow/env/bin/gunicorn --access-logfile /home/ghostman/Projects/asknow/env/log/gunicorn.log --error-logfile /home/ghostman/Projects/asknow/env/log/gunicorn-error.log --workers 3 --bind unix:/home/ghostman/Projects/asknow/asknow/asknow.sock asknow.wsgi:application
[Install]
WantedBy=multi-user.target
The problem that I need to Nginx handles request by itself for static only (www.asknow.local/static) but it tries to handle other URLs too. So when I go to www.asknow.local/admin now Nginx tries to find a resource by path (my_project/admin). But if I go to www.asknow.local Nginx proxies a request to the Gunicorn. Gunicorn error log is empty, so it fails on Nginx side.
Nginx log
2017/11/01 04:27:22 [error] 13451#13451: *1 open() "/usr/share/nginx/html/static/img/search.svg" failed (2: No such file or directory), client: 127.0.0.1, server: asknow.local, request: "GET /static/img/search.svg HTTP/1.1", host: "www.asknow.local"
How to fix it?
Your issue is using =, that is use for absolute locations. You don't want that (only for favicon.ico) you want that
server {
listen 80;
server_name asknow.local www.asknow.local;
root /home/ghostman/Projects/asknow/asknow;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ghostman/Projects/asknow/asknow;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ghostman/Projects/asknow/asknow/asknow.sock;
}
}

nginx.conf rewrite not getting triggered

http{
server {
listen 80;
server_name example.com;
rewrite_log on;
root /etc/nginx/html/mysite;
location ^~ /me/ {
rewrite ^/me/(.*)$ /etc/nginx/html/mysite/$1.html;
}
}
}
The above is not working when I make the request http://example.com/me/all
I want it to be served by /etc/nginx/html/mysite/all.html
But the request goes to /etc/nginx/html/mysite/me/all ignoring the rewrite and nothing about a rewrite error in the logs (just a 404 not found).
You are specifying the root of your virtual host to /etc/nginx/html/mysite (line 6). In your rewrite rule you again specify the whole path /etc/nginx/html/mysite/$1.html (line 8). What nginx does is appending the target of your rewrite rule (/etc/nginx/html/mysite/$1.html) to the root of your virtual host (/etc/nginx/html/mysite). In you example this results in searching for the file:
/etc/nginx/html/mysite/etc/nginx/html/mysite/all.html
Such file does not exist and causes the 404.
Correct the rule to make the target a relative path with respect to root, as:
http{
server {
listen 80;
server_name example.com;
rewrite_log on;
root /etc/nginx/html/mysite;
location ^~ /me/ {
rewrite ^/me/(.*)$ /$1.html;
}
}
}
Remember that you can get more information on these errors by reading the nginx error log (by default placed at /var/log/nginx/error.log). In you case it would have stated something like:
"/usr/share/nginx/html/mysite/usr/share/nginx/html/mysite/all.html" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /me/all HTTP/1.1", host: "localhost"

Resources