Nginx different locations on one virtual host - configuration

I have the following nginx config for my virtual host:
server {
listen 80;
server_name myproject;
location / {
root /var/www/project1;
}
location /project2 {
root /var/www/project2;
}
}
But the web-server shows the following error:
location "/" is outside location "/project2"
I want to open first project via: http://myproject and second project via http://myproject/project2
What's wrong with my config?

You should only have one root in your server{} block. Read about the Nginx pitfalls here: http://wiki.nginx.org/Pitfalls and look at the part about 'Root inside location block'.
Put the root outside the location blocks and set it to /var/www. Place your index file (and other files) for project1 in /var/www. Create a folder (/var/www/project2) and place the index etc. for project2 in this folder.
You can leave out the location /project2 {} part unless you want to specify some other settings for this project. If the settings are completely different from project1 I recommend moving all the stuff for project2 to another server{} block and specify its root and other settings there.

Related

nginx 1.6.2 [Windows] won't find protected directory

as the headline said, my nginx isn't able to find .htpasswd-protected directories. Here are my settings, my .conf and the locations of the relevant files:
NOTE: due to my brothers wish, the nginx runs on a win8.1x32 virtual machine
I created a .htaccess file in my root/xx directory looking like this:
AuthName "Restricted Area"
AuthType Basic
AuthUserFile /b/.htpasswd
AuthGroupFile /dev/null
require valid-user
And I created a .htpasswd file inside the shown directory.
After that I modified my .conf in the conf-folder like this:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ^~ /b/ {
auth_basic "Restricted Area";
auth_basic_user_file b/htpasswd;
}
}
I deleted the comment-lines from the auto-generated file only here, in the original, they are still existent - thought this might clear your sight on the file.
Now, here comes the problem: the file which I am aiming for is called int.html and can be found in /b/int.html (stylesheets and scriptfiles included), but every time i want to get to this file, the browser asks my to type in password and username - as I wanted it - but then the browser shows a 404-error, which means, he couldn't find the named file. (I made sure everything is spelled correct in the directories and tried this in several browsers on different devices). I don't know why. (I already tried to set the int.html as index in the config like this:
location ^~ /b/ {
auth_basic "Restricted Area";
auth_basic_user_file b/htpasswd;
}
But it still doesn't work. - Ideas, anyone?
Greetz Dyarn

Catch-all configuration except for www.website.com?

Using Nginx, I'm trying to configure my server to accept all domains that point to the IP of my server, by showing them a specific website, but when accessing the www.example.com (main website), I'd show an other content.
Here's what I did so far:
server {
// Redirect www to non-www
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
// rest of the configuration
}
server {
// Catch all
listen 80 default_server;
// I also tried
// server_name _;
// Without any luck.
// Rest of the configuration
}
The problem with this configuration is that every request made to this server not being www.example.com or example.com is took under example.com server configuration, not the catch all.
I'd like to cath only www.example.com/example.com in the first two configurations, and all the others in the last configuration.
I suggest putting your server on top of the file :)
I think nginx wants default servers to be on top of -a- file.
I have really much files on my server, but there is one with a default server as first server declaration, and that works.

How to serve images with nginx

I am completely new to nginx and I am asked to find a way to serve Map Tiles that are separated according to the zoom levels. The image file structure is like ~/data/images/7/65/70.png where 7 is the zoom level, 65 and 70 are the lon-lat values. The folder 65 contains many files such as 71.png, 72.png and etc.
I have installed Nginx properly and I can get Welcome to nginx message. I have followed the instructions in http://nginx.org/en/docs/beginners_guide.html and created the /data/www and /data/images directories. I have placed index.html file under /data/www and tile images under /data/images. Then I modified the configuration file by adding following lines in http tags:
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
After reloading the config file and entering localhost on the browser I can neither get the index.html file nor see the images.
What I am trying to do is to display the image when I enter something as:
http://localhost/1.0.0/basemap/7/65/70.png
7: folder indicating 7th zoom level
65: folder indicating the latitude
70.png: file indicating the longitude (folder 65 includes many png files)
What am I missing?
Ok, let me explain something, you already have a localhost server, which is defined inside a file called default that is the file that causes the "Welcome to nginx" or something to appear, and I believe you can't create a new server with the same server_name, let's remove that and make your localhost serve only those images,
First we need to delete the default file from sites-enabled , it will still exist inside sites-available if you ever want to get it back. ( note that all files inside sites-enabled are simply symlinks from the files inside sites-available )
We create a new file inside sites-available and call it whatever you want, images-app for example
create the new server inside the images-app file, I'll assume that the root of the app is inside a folder called /data of course you will map that to your own server structure.
server {
server_name localhost;
root /data;
index index.html;
location / {
try_files $uri =404;
}
}
now we go to sites-enabled and enable this site we created inside sites-available
sudo ln -s /etc/nginx/sites-available/images-app /etc/nginx/sites-enabled/
make sure that all the nginx config are correct
sudo nginx -t
If nothing is wrong we can go ahead and reload nginx settings
sudo service nginx reload
For my case I just edited /etc/nginx/sites-enabled/default file.
I added following config:
location /images/ {
root /data;
}
and placed images under /data/images:
and url works: http://localhost/images/example.png
I use VS Code as SuperUser. (I know it is bad, but I accept risks)
It helps a lot with root access file editing:
I'm also new to nginx, Here is my solution that is similar with Mohammad AbuShady's answer :
delete sites-enabled/default
create the whatever.conf in /etc/nginx/conf.d/
The reason is:
sites-enabled/default has defined a server
that is listening on 80 rooting with /var/www/html
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
the nginx.conf file includes other conf files
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
pay attention on permission
the 1st edition of my.conf is root on /home/scotv/, but will get 403 Forbidden error, check the error.log:
2016/04/07 20:12:44 [error] 12466#0: *2 open() "/home/scotv/data/a" failed (13: Permission denied),
client: 127.0.0.1, server: , request: "GET /a HTTP/1.1", host: "localhost"

Nginx: can I use $server_name when specifying access log location?

I want to write a config file for an nginx virtual host that looks like this:
server {
listen 80;
server_name www.my-domain-name.com;
access_log /home/me/sites/$server_name/logs/access.log;
error_log /home/me/sites/$server_name/logs/error.log;
location /static {
alias /home/me/sites/$server_name/static;
}
location / {
proxy_pass http://localhost:8000;
}
}
Using $server_name seems to work find for the location /static, but it doesn't seem to work for the access_log and error_log -- am I doing something wrong? Or is this just not possible? Can I do it some other way?
[update] - this is the error message when trying to reload nginx:
nginx: [emerg] open() "/home/me/sites/$server_name/logs/error.log" failed (2: No such file or directory)
I wanted to do this too, but apparently by design nginx cannot expand variables in the error_log command, in case there are errors doing so and it cannot get a log filename to write them to.
Their suggestion is to use some program to generate your configuration files instead. You could use sed for this, to automatically search-and-replace your own variables and placing the output in the nginx configuration directory.

spread exits directly after starting

I just tried to start spread for communication of some of my tools that I use in for the integration for different sensor data processes.
Just after the startup spread exits with the following message:
Conf_load_conf_file: using file: spread.conf
Successfully configured Segment 0 [127.0.0.255:4803] with 2 procs:
localhost: 127.0.0.1
boron: 127.0.1.1
Finished configuration file.
Hash value for this configuration is: 913193717
Conf_load_conf_file: My proc id (129.70.129.5) is not in configuration
Exit caused by Alarm(EXIT)
As seen in the message I use the following spread.conf file to configure my local spread segment.
Spread_Segment 127.0.0.255:4803 {
localhost 127.0.0.1
boron 127.0.1.1
}
The problem seems to be that the local machine I'm working at is not appearing in the config file according to spread. Spread tries to find the acutal IP 129.70.129.5 and not localhost in the .conf file.
Changing my .conf file to:
Spread_Segment 127.0.0.255:4803 {
localhost 127.0.0.1
boron 129.70.129.5
}
or starting spread with
spread -n localhost
does the trick.