I am setting up nginx as a sort of static file server. For some reason it is only working when I go to 123.123.123.123/ or 123.123.123.123. However, when I go to 123.123.123.123/static/content/ or 123.123.123.123/static/content/another.mp3 it returns a 404 not found. Here is the config file that is located in /etc/nginx/sites-available and linked to /etc/nginx/sites-enabled. I am really stumped as to why it is not working.
Any pointers or tips would be appreciated.
server {
listen 123.123.123.123:80;
server_name "";
location / {
root /srv/homepage;
index index.html;
}
location /static/content/ {
root /srv/static/content;
index song.mp3;
}
}
Look into logs.
Seems like nginx tries to open paths like /srv/static/content/static/content/file.mp3
You need to rewrite url here, try this:
rewrite /static/content/(.*) /$1 break;
Related
I have simple nginx server configuration which works and looks like this:
server {
listen 80;
server_name _;
location /cert {
alias /aaa.txt;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Proxy pass works like it should do but what doesn't work is getting this /aaa.txt file.
I tried changing alias... into:
return 200 'Hi' - This indeed return and download file named Hi
root / > As I read, root just adds to link before so it is not suitable for this situation but tried it
try_files /aaa.txt =404 - Not working
I tried changing permission of /aaa.txt with command chown www-data:www-data aaa.txt and still not working.
I have tried creating folder /wwwroot/ and put it inside that folder and do everything the same but still not working.
I do not know what else to try.
Pointing to answers provided in suggested question Nginx return file for path I edited my setup file and (this time I put it in folder wwwroot) and it is still not working
GNU nano 6.2 /etc/nginx/sites-available/reverse-proxy
server {
listen 80;
server_name _;
location /cert {
index aaa.txt;
alias /wwwroot;
}
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Solution was quite simple. I was all time putting path relative to ~ which is not where nginx starts. In terminal you see it as ~ but its full path is /home/[user]/ so when i created folder wwwroot it was not located at /wwwroot but /home/ubuntu/wwwroot and that is the reason why it coulnd't find it.
creating a website for my self and need to host projects.
Basically, i hhave different projects with different framework. ie, Flask, Django, Node.JS and some html file projects. I would like to host them at projects.domain.com/<project name>
I tried to set server_name projects.domain.com/asdf but in error.log it says, server name "projects.domain.com/asdf" has suspicious symbols
Next up, i tried to nest location blocks (which i presume isn't how its supposed to be)
location /asdf {
location /static/ {
root blah blah;
}
location / {
..
proxy_pass http://localhost:3000 ;
}
}
But, this errors out saying location static is outside asdf
Some suggested to alias instead of root in the location /static/ block, but that doesnt work too.
Any help is appreciated :)
First of all a server_name can not contain URI segments. So a hostname or IP should be used as a value.
If you want to mix different local directories and proxy-locations a configuration could look like this.
Notice: Your location URI (/one, /two) will be appended to the root path.
The root directive can be used in every location block to set the document root.
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
This is the reason why alias exists. With alias the location will not be part of the directory path. Check this out:
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
server {
server_name project.domain.com;
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /one/ {
alias/var/www/html/project1/;
index index.html;
}
location /two/ {
alias/var/www/html/project2/;
index index.html;
}
}
Id like to serve to different websites based on url user enter.
For example if user will go to
my-domain.pl/ the content will be served from desktop folder
my-domain.pl/desktop the content will be served from desktop folder
my-domain.pl/mobile the content will be served from mobile folder
root
|
|---mobile
| |--- assets
| |---js,css,img
|
|---desktop
|--- assets
|---js,css,img
I tried that in nginx setup file:
server {
root /desktop
location /mobile {
root /mobile
}
location /desktop{
root /desktop
}
}
but it works only for / path, remaining paths return 404
I tried to add try_files $uri index.html but it seems that it returns index.html files for all request for this location e.g. it returns index.html file instead javascript too.
I am absolutely new in setting up nginx so any help will be appreciated.
You need to make use of the alias directive instead of root: see documentation.
server {
root /;
location /desktop {
}
location /mobile {
alias /mobile;
}
}
(don't forget trailing semicolons)
You should avoid specifying root inside location blocks (cf. nginx pitfalls).
Have you tried the following:
have only one root
use rewrite to serve /desktop by default
The config would look like:
server {
root /;
## this should take care of the redirect to /desktop by default:
location = / {
rewrite ^ /desktop/ redirect;
}
## this one below probably doesn't work:
# rewrite ^/$ /desktop/;
}
P.S. I have no access to a machine with nginx right now so I'm not able to check the rewrite syntax. See also this answer.
I am using Nginx Server. I have some images and video in /video directory. I am using these video and images into my site, but I want to show an 403 error, if someone try to access them directly like this examples shows:
http://xysz.com/video/abc.png
I know it's possible in Apache by changing the .htaccess config, but not sure how to do the same in Nginx. How can I achieve this with Nginx?
You can use this,
server {
listen 80;
server_name xysz.com;
root /var/www/xysz.com/html ;
location /assets/ {
valid_referers xysz.com/ xysz.com/video/index.html;
if $invalid_referer {
deny all;
}
}
After adding rewrite block, the Ubuntu 12.04 server hosting Rails 3.2.12 app throws out 404 Not Found error when entering mysite.com/nbhy.
Here nbhy is a symlink under root /var/www/ pointing to /var/www/nbhyop/current/public and it is for hosting rails app. The purpose of the rewrite is to rewrite to /nbhy/authentify/sigin when user entering /nbhy or /nbhy/
Here is server block in nginx.conf:
server {
listen 80;
server_name mysite.com;
root /var/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
}
location / {
rewrite "/nbhy" /nbhy/authentify/signin last;
rewrite "/nbhy/" /nbhy/authentify/signin last;
}
}
The error.log on nginx for the error is:
2013/06/09 21:36:31 [error] 32505#0: *1 open() "/var/www/nbhy/authentify/signin" failed (2: No such file or directory), client: 67.173.143.107, server: mysite.com, request: "GET /nbhy HTTP/1.1", host: "mysite.com"
Before adding rewrite location block, the system could bring up login page with url mysite.com/nbhy/authentify/signin. But now it throws out error after adding the rewrite block. What's wrong with the rewrite?
The location block must be within the server block:
server {
listen 80;
server_name mysite.com;
root /var/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
location / {
# Matches /nbhy /nbhy/ /nbhy////////////...
location ~ ^/nbhy/*$ {
return 301 /nbhy/authentify/signin;
}
# Other rules (regex) goes here
}
}
The symbolic link is from no concern at this stage of processing.
You are using rewrite, were the first pattern is always a regular expression. Mohammad AbuShady was absolutely right with his answer of using a more specific location. But using a rewrite is not necessary, a return is better because we want nginx to abort execution and simply—well—return.
Also note how I enclosed the regular expression within the generic block location / {} which matches all locations. This is the proper way to write location blocks according to Igor Sysoev. See this forum post for more info.
My configuration above is also catching URLs with more than one slash (hence the *) and is meant as a more user friendly matching because a user might simply type too many slashes. No need to punish them, instead answer with a proper redirect.
I hope this helps!
Related Links
How nginx processes a request
If I understand correctly, you have a URI your-host/nbhy which you want to rewrite to your-host/nbhy/authentify/signin, which should be linked to /var/www/nbhy/authentify/signin, which is symlinked to /var/www/nbhyop/current/public/authentify/signin?
It looks like nginx is complaining about not finding /var/www/nbhy/authentify/signin. Since you have a symlink from /var/www/nbhy to /var/www/nbhyop/current/public, there has to be a folder /var/www/nbhyop/current/public/authentify/signin. Are you sure there is one, and that your www-data user (or whatever user you're using) has rights to the directories above it?
Also, nginx has an option for disabling symlinks. Try setting that to off. That's the default, I know, but there might be another file that sets it to on.
It might also be that nginx doesn't follow the symlink because you added last to your rewrites. Try removing that, and see if it works then.
why not try adding a more specific location block
location ~ /nbhy/?$ {
rewrite ^ /nbhy/authentify/signin last;
}
or you could remove the regex but it would match longer urls like /nbhy/one/two
location /nbhy {
rewrite ^ /nbhy/authentify/signin last;
}