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

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

Related

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 error rewrite or internal redirection cycle while internally redirecting - what's wrong?

I'm getting this:
2020/06/12 20:43:58 [error] 3266#3266: *39 rewrite or internal redirection cycle while internally redirecting to "/home/panel/front/qcnet24-front/index.html", client: 195.x.x.136, server: my-app, request: "GET /register HTTP/1.1", host: "vps-866691.vps.ovh.net"
My configuration of nginx.conf is:
server{
listen 0.0.0.0:80;
server_name my-app;
root /home/panel/front/qcnet24-front;
#error_page 404 /home/panel/front/qcnet24-front/index.html;
location / {
index index.html;
try_files $uri $uri/ /home/panel/front/qcnet24-front/index.html;
}
location /images {
alias /home/panel/Pictures;
}
location /api {
proxy_pass http://127.0.0.1:8080/api;
}
location /login {
proxy_pass http://127.0.0.1:8080/login;
}
}
}

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 reverse proxy return 404

My Nginx installed and running, below is the config from /etc/nginx/nginx.conf , I want to forward all /api/* to my tomcat server, which is running on the same server at port 9100(type http://myhost:9100/api/apps works) , otherwise, serve static file under '/usr/share/nginx/html'. Now I type http://myhost/api/apps give an 404. What's the problem here?
upstream myserver {
server localhost:9100 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location ^~ /api/ {
proxy_pass http://myserver/;
}
location / {
}
}
The proxy_pass statement may optionally modify the URI before passing it upstream. See this document for details.
In this form:
location ^~ /api/ {
proxy_pass http://myserver/;
}
The URI /api/foo is passed to http://myserver/foo.
By deleting the trailing / from the proxy_pass statement:
location ^~ /api/ {
proxy_pass http://myserver;
}
The URI /api/foo is now passed to http://myserver/api/foo.

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