I have following static files:
/home/nginx/js/a.js
/home/nginx/errors/403.html
nginx.conf:
[...]
location / {
root /home/nginx;
}
error_page 403 /errors/403.html;
location = /errors/403.html {
root /home/nginx;
}
[...]
403.html code:
[...]
<script src="js/a.js"></script>
[...]
However it causes an error, it cannot find wanted .js file.
I tried to change it with ../js/a.js, move 403.html to root directory (with changes in nginx.conf's location) but still no effect. Is it a problem with nginx configuration?
Related
I have configured nginx to serve static page as below.
location /home {
alias /home/username/files/home;
try_files $uri /home/index.html;
}
Only index.html loads on http://example.com/home. All other css, js and image files are getting 404 as request is still going for http://example.com/css/style.css instead of http://example.com/home/css/style.css. Is there any way I can configure /home prefix to all the static file without manually adding to all static files?
You can try to use variable root depending on HTTP Referer header (although it is a dirty hack):
map $http_referer $root {
~example\.com/home/ /home/username/files/home;
default <your default root here>;
}
server {
...
root $root;
location /home {
root /home/username/files;
try_files $uri /home/index.html;
}
}
Note that if any of your web pages under /home/ have a links to the main site, those links won't work correctly because of example.com/home/... HTTP Referer header value.
Why root /home/username/files; instead of alias /home/username/files/home;, you ask? Because as nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
I was trying to make a 500 error page for a situation when server behind proxy when is off and a 404 error page for not found static pages.
I already had working proxy_pass to localhost:8080 for messenger but while working on adding custom error pages I had to mess something up and it stopped working. Error pages are working all well but mydomain.d/messenger/ returns 404 19 (I know it from logs) but doesn't return the custom page.
erver {
server_name mydomain.d;
# newly added
location / {
proxy_pass "http://localhost:8081/";
}
# newly added
location /webapp/planner/ {
proxy_pass "http://localhost:8082/";
}
# was working for some time
location /messenger/ {
proxy_pass "http://localhost:8080/";
}
# newly added
location /static/ {
alias /home/static/;
}
# newly added
error_page 404 /err404.html;
location = /err404.html {
root /home/server/errors/err404;
}
# newly added
error_page 500 502 503 504 /err500.html;
location = /err500.html {
root /home/server/errors/err500;
}
...
I changed only this config while making the error pages.
curl localhost:8080/messenger/xxx gives expected output.
https://mydomain.d/messenger/xxx redirects me to:
https://mydomain.d/xxx with 404 error page.
So in summary: When configuring error pages for static file access and for situations when servers behind proxy is down I accidentally broke proxy_pass access to /messenger/ which now only returns an 404 19 error.
What is the fix?
I found an answer to my problem:
There was an unnecessary / at the end of proxy_pass address.
Wrong:
location /messenger/ {
proxy_pass "http://localhost:8080/";
}
Right:
location /messenger/ {
proxy_pass "http://localhost:8080"; #<-- here the '/' at end is gone
}
Here's a part of my Nginx configuration:
server {
location /f/ {
autoindex on;
alias /ftp/;
}
}
When I go to www.mywebsite.com/f/foo.txt, it returns the file if it exists in /ftp/ or a 404 error if it doesn't.
When I go to www.mywebsite.com/f/, it lists all files in /ftp/ directory.
Would there be any way return a 403 error instead of listing all files?
Thanks.
I have configuration file:
server {
listen 80;
root /path/to/file/;
location /api/ {
proxy_pass http://0.0.0.0:5000/api/;
}
location /docs/ {
index /path/to/another/index/filename.html;
}
}
But when i'm trying to call /docs/ in browser i see in error.log that server trying to find docs file in root /path/to/file/ instead of returning another html file from second location block.
How can i proxy request /docs/ to another docs.html that located on file system?
index just describes what file to serve per default. The keyword you need is root:
location /docs {
root /path/to/another/index;
index filename.html;
}
Now the web server will serve /path/to/another/index/filename.html when the route /docs/ is accessed.
How do you call get a file that outside of the root to the be processed. i was reading about the alias and couldnt get it working. so ive tried adding a new root within the location no luck.
this is a cutdown of my config file
server {
listen 443;
server_name domain.com;
---
root ../var/www/domain/public_html;
# works as being called form within the root
location /login {
rewrite ^/login /account/login permanent;
}
#need to
location /validation/code.png {
root /var/www/domain/include;
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
}
You are rewriting the png file to a php file. This will create a sub request for /captcha/display_captcha.php. Is there a location for php in your config? Assuming the php location uses the general root, when the sub request hits this, /captcha/display_captcha.php will not be found an you will get a 404 error.
Your best bet is to copy the php location and create a php location specifically for the php file.
server {
listen 443;
server_name domain.com;
root /var/www/domain/public_html;
...
location = /validation/code.png {
rewrite ^/validation/code.png /captcha/display_captcha.php;
}
location ~ ^/captcha/display_captcha.php {
root /var/www/domain/include
...
# copy php processing code from normal php location.
}
}
Better still, just use '/captcha/display_captcha.php' directly in your html and drop '/validation/code.png' altogether.