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"
Related
I have simple setup of 3 servers (in containers) - 2 "app" servers (whoami services - so by response I can acknowledge server) and nginx server.
I've launched nginx with simple load-balancing configuration:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
upstream myapp1 {
server w1:8000 weight=1;
server w2:8000 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://myapp1/;
}
}
}
The problem is that it doesn't work in Chrome - it always loads only first server. I've tried to turn off cache in Dev console + reload via CTRL+F5 but nothing helped.
If I try to curl nginx server - I get responses in round robin manner (as expected).
Here is my containers setup:
docker network create testnw
docker run -dit --name w1 --network testnw jwilder/whoami # app1
docker run -dit --name w2 --network testnw jwilder/whoami # app2
docker run -dit --name ng --network testnw -p 8989:80 -v ${PWD}/my.conf:/etc/nginx/nginx.conf nginx # LB server
curl localhost:8989 # will get response from w1
curl localhost:8989 # will get response from w2
curl localhost:8989 # will get response from w1
...
Edit 3: Found out an interesting issue.
In chrome every time I access my website it makes two calls no matter what they are called to/of my website and /favicon.ico of my website.
I don't have a /favicon.ico.
What I think is happening
when Nginx is getting requests for/of my website, it is loading the first server upstream.
when chrome loads / from my website it also calls /favicon.ico of my website which results in making a new call to Nginx so it loads the .ico files from the next server upstream.
this happens so that servers 1,2,3 are loaded in order 1(ico file from 2),3(ico file from 1),2(ico file from 3). and cycle repeats.
once I stopped the loading of /favicon.ico in Nginx, my three upstreams servers 1,2,3 are loading in order 1,2,3 of round-robin.
I put this in the server with upstream to disable loading favicon.ico from Nginx.
location = /favicon.ico {
log_not_found off;
}
Hope anyone having this problem find this useful.
Edit 2: Figured out the issue, the load balancing is working fine with static files and static servers inside the Nginx conf file.
but my applications are being loaded by node, so had to start Nginx after starting all the node servers.
Issue reappears when I restart the application server while Nginx is running.
Now no issue will update soon
Edit 1: This is not working for me anymore, this worked yesterday, today continued working on the same configuration, the issue reappeared.
Had this same issue with my setup.
What worked for me after a lot of proxy setup and VirtualBox setup and network editing.
Add an extra server block in the HTTP block.
server{
}
and reload the Nginx service.
It worked for me, after reloading once both chrome and firefox loads the servers in the given order, I deleted the server block and it is still working.
Don't know why the issue raised in the first place.
Hope this helps to solve your issue.
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
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.
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.
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.