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!
Related
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/;
}
}
}
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";
}
}
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"
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
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.